IDL Integration - integral

I'm looking to integrate a function I am building, but the function would change each iteration based on a given input. For instance:
y=4e^(mx/4)
I would want to integrate with respect to x with a lower and upper bound, but the value of m would change. I know all my values of m.
Can I work with this? My initial assumption would be to use QROMB but that seems limited and unable to handle my issue.

QROMB (and other integrators) want a function of one variable, so you have to get the m in there through the back door. One way is with a common block:
function integrand,x
common int_common,int_m
return,4*exp(int_m*x/4)
end
function integrator,m,xlow,xhigh
common int_common,int_m
int_m=m
return,qromb('integrand',xlow,xhigh)
end
integrator(m,xlow,xhigh) will return the integral you want.

Related

Can I use self.math.random on 2 Npcs?

I have this script on 2 npcs but both choose the same random number (7) how do I make the Npcs choose their own number? I did try with self.math.random and it gave an error
so what would the solution be? will I have to create different variables for each npc related to the random function?
function Behavior:Awake()
math.randomseed (os.time())
self.destino = math.random( 1, 7 )
In the engine I work with it says to put self. for independence...
The fact that you call math.randomseed inside a function suggests to me that you're calling it every time you want a random number. The purpose of math.randomseed is to initialize the RNG, which means you're initializing the RNG multiple times, hence the repetition. Usually, you need to call math.randomseed exactly once in the entire program.
it says to put self. for independence...
That's not a great explanation of how self works. self is a function parameter that gets automatically declared when you declare a function with colon notation.

A views implementation for cg! on matrices

I am trying to use the cg! function from IterativeSolvers.jl on for solving matrix-matrix linear systems, i.e. AX=B for A,X,B appropriately sized matrices. Given the way the indices work, X[:,i] is independent of all but B[:,i], and so this actually boils down to n different linear solves. Direct solving via \ works automatically in this case, but iterative solvers like CG don't. I can easily do this with a loop on the outside, but I haven't been able to get the in-place op working. For now, my code looks like this:
for j=1:size(u,2)
u[freenode,j],ch = cg!(u[freenode,j],lhs,Dinv.*rhs(u,i)[:,j]) # Requires Vector, need to change rhs
end
which gives solves CG with the appropriate lefthand side and righthand side. But the reason why it's not in-place boils down to this simple example throwing an error:
using IterativeSolvers
y = view(ones(4,2),:,2)
A=rand(4,4)
cg!(y,A,view(zeros(4,2),:,2))
which is:
ERROR: MethodError: no method matching init!
(::IterativeSolvers.KrylovSubspace{Float64,Array{Float64,2}}, ::SubArray{Float64,1,Array{Float64,2},Tuple{Colon,Int64},true})
Closest candidates are:
init!{T}(::IterativeSolvers.KrylovSubspace{T,OpT}, ::Array{T,1}) at C:\Users\Chris\.julia\v0.5\IterativeSolvers\src\krylov.jl:66
in #cg!#23 at C:\Users\Chris\.julia\v0.5\IterativeSolvers\src\cg.jl:7 [inlined]
in cg!(::SubArray{Float64,1,Array{Float64,2},Tuple{Colon,Int64},true}, ::Array{Float64,2}, ::SubArray{Float64,1,Array{Float64,2},Tuple{Colon,Int64},true}, ::Int64) at C:\Users\Chris\.julia\v0.5\IterativeSolvers\src\cg.jl:6 (repeats 2 times)
The problem seems to not be with the views, given the results of a previous SE question
I have doubts that you'll be able to avoid allocations, because the init! function is implemented as
function init!{T}(K::KrylovSubspace{T}, v::Vector{T})
# K.v = Vector{T}[all(v.==zero(T)) ? v : v/norm(v)]
K.v = Vector{T}[copy(v)]
end
and hence there's a copy anyway. Nevertheless, if you want this function to accept views, it should not be an issue to just modify the Vector to AbstractVector. (The function is simple enough that if you don't like to modify the package, you can just add a more general method yourself.)
You're right, it's not a "view" problem per se, the problem seems to be that the init! method seems to not have been defined in a way that can accept a 'view' (i.e. a SubArray type). The error suggests that "the closest candidate is"
init!{T}(::IterativeSolvers.KrylovSubspace{T,OpT}, ::Array{T,1})
(i.e. there's no method definition for a SubArray, only for a normal Array, and it appears there's no generic function / base case available to fall back on)
If you 'collect' the array first, then it works (presumably) as expected:
julia> cg!( collect(y), A, view(zeros(4,2),:,2))
([0.0752658,-0.693794,0.330172,0.437474],IterativeSolvers.ConvergenceHistory{Float64,Array{Float64,1}}(false,0.0,5,[0.249856,0.392572,0.401496,0.463142]))
except obviously that's of no use to you because you don't really get to change y as intended this way.
The only way I see around it if you're intent on keeping y as a view until that point, is to temporarily 'hack' it into a normal array inside cg! using a compound statement:
cg!((y = collect(y); y),A,view(zeros(4,2),:,2))
but, obviously, it's no longer a view at this point, so you'd have to update the original array it was a view into manually ...

Why does the rand() return always the same number?

I am using
rand(200)
in my Rails application.
When I run it in console it always returns random number, but if I use it in application line:
index = rand(200)
index is always the same number.
Why is that and how to overcome this?
Simple pseudo-random number generators actually generate a fixed sequence of numbers. The particular sequence you get is determined by the initial "seed" value. My suspicion is that you are always getting the first number in the same sequence. Therefore I suggest we try to change the sequence by calling srand every time before calling rand, thus changing the seed value every time. The docs explain that, when called without a parameter, srand generates a new seed based on current circumstances (e.g. the time on the clock). Thus you should get a difference sequence and hence a different random number:
srand
rand(200)
Now, you may ask - why are you always getting the same sequence? I have no idea! As someone else suggested in one of the comments, the behavior you are seeing is the behavior one would expect if you had other code, anywhere, that calls srand with the same, fixed value every time. So it might be good to look for that.
Try Random.rand(). For example
Random.rand(200)
Or if you're working with an array you could use sample.
[*1..200].sample
rand(200) is run once and that value is assigned to your index variable. So 'index' will always be that number.
If you want index to change, you will need to continually run rand on it.
Here's a simple way to do that:
def randomIndex(num)
index = rand(num)
return index
end
randomIndex(200)
=> // this value will change

excel performance using Range

I am new to VBA and I'm now working on a project where speed is absolutely everything. So as I'm writing the code, I noticed a lot of the cells in the sheet are named ranges and are referenced in the functions explicitly like this:
function a()
if range("x") > range("y") then
end if
... (just imagine a lot of named ranges)
end function
My question is, should i modify these functions so that the values in these named ranges are passed in as parameters like this:
'i can pass in the correct cells when i call the function
function a(x as int, y as int)
if x > y then
end if
...
end function
Will that speed things up a little bit? These functions are called almost constantly (except when the process is put to sleep on purpose) to communicate with a RTD server.
VBA is much slower at making "connections" to your worksheet than it is at dealing with its own variables. If your function refers to the same cell (or range) more than once then it would be advantageous to load those into memory before VBA interacts with them. For example if range("x")>range("y") is the only time in the function that either x or y are referred to then it won't matter. If you have if range("x")>range("a") and if range("x")>range("b") and so on then you'd be much better off starting your function with
varX=range("x")
varY=range("y")
and then working with the VBA variables.
It might seem that by parameterizing the function as your second example shows accomplishes my recommendation. This may or may not be the case because Excel might just treat those variables as references to the worksheet and not as values (I'm not sure). Just to be safe you should specifically define new variables at the beginning of your function and then only refer to those variables in the rest of your function.
To sum up the above wall of text, your goal should be to minimize the number of times VBA "connects" to the worksheet.

Mathematica - can I define a block of code using a single variable?

It has been a while since I've used Mathematica, and I looked all throughout the help menu. I think one problem I'm having is that I do not know what exactly to look up. I have a block of code, with things like appending lists and doing basic math, that I want to define as a single variable.
My goal is to loop through a sequence and when needed I wanted to call a block of code that I will be using several times throughout the loop. I am guessing I should just put it all in a loop anyway, but I would like to be able to define it all as one function.
It seems like this should be an easy and straightforward procedure. Am I missing something simple?
This is the basic format for a function definition in Mathematica.
myFunc[par1_,par2_]:=Module[{localVar1,localVar2},
statement1; statement2; returnStatement ]
Your question is not entirely clear, but I interpret that you want something like this:
facRand[] :=
({b, x} = Last#FactorInteger[RandomInteger[1*^12]]; Print[b])
Now every time facRand[] is called a new random integer is factored, global variables b and x are assigned, and the value of b is printed. This could also be done with Function:
Clear[facRand]
facRand =
({b, x} = Last#FactorInteger[RandomInteger[1*^12]]; Print[b]) &
This is also called with facRand[]. This form is standard, and allows addressing or passing the symbol facRand without triggering evaluation.

Resources