help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Vectorize a function that depends on previous function value


From: Juan Pablo Carbajal
Subject: Re: Vectorize a function that depends on previous function value
Date: Mon, 19 May 2014 16:43:37 +0200

On Mon, May 19, 2014 at 3:52 PM, gergo <address@hidden> wrote:
> Hi Kai,
>
> thanks for your reply. I suspect, my snippet was somehow misleading.
>
>
> siko1056 wrote
>> From your code snippet I conclude, that you don't have data dependencies
>> from the left hand side (lhs) to the rhs, like a(n) = a(n) + a(n -1),
>> right?
>
> That is correct, only the lhs depends on the rhs (for the current time
> step).
>
>
> siko1056 wrote
>> In your loop, your previous a(n) will be dropped and a(1) is
>> generated by a different rule, still right?
>
> No, what I need is a series of responses of my model to the data. Each of
> the responses (the a(n)) depend on the current data(n) and the last response
> a(n-1) (and on a hidden state b(n)).
>
> The way you tried it in your code was also my first attempt. However, in
> your code the values depend on some preliminary values in a. The following
> snippet hopefully points out my problem more clearly. Imagine data was
> constantly 2, and the nonlinear terms were gone:
>
> a = ones(10,1);
> a(2:end) = a(1:end-1) *2;
>
> This yields a = [1 2 2 2 ...]';
> What I needed was: a = [1 2 4 8 ...]';
>
> My first snippet, what was intended to be rather a formula than octave code
> would be:
>
> function [a,b] = dynamicalSystmem(data, params, a1, b1)
>   a = zeros(N,1);
>   b = zeros(N,1);
>   a(1) = a1; b(1) = b1;
>   for n = 2:N
>     a(n) = a(n-1) + data(n) + b(n-1) + someNonLinearFunction(a(n-1));
>     b(n) = b(n-1) + someOtherNonlinearFctn(a(n-1));
>   end
> end
>
> The goal is, to remove that for loop there.... Ah, and yes, the
> nonLinearFunctions are vectorized.
>
> Any idea?
>
> Thanks
>
> Gerald
>
>
>
>
>
>
>
> --
> View this message in context: 
> http://octave.1599824.n4.nabble.com/Vectorize-a-function-that-depends-on-previous-function-value-tp4664169p4664177.html
> Sent from the Octave - General mailing list archive at Nabble.com.
>
> _______________________________________________
> Help-octave mailing list
> address@hidden
> https://lists.gnu.org/mailman/listinfo/help-octave

I do not think you can vectorize a time dependence relation easily,
just try to vectorize
a(n) = a(n-1)+f(a(n-1));
This is exactly what integrators like lsode and ode45 do. What you can
do if your code is slow is to pass
that loop to C/C++ code.
https://www.gnu.org/software/octave/doc/interpreter/Getting-Started-with-Oct_002dFiles.html

You can also try these tricks
https://www.gnu.org/software/octave/doc/interpreter/Vectorization-and-Faster-Code-Execution.html
Specially "Function Application" and "Accumulation"



reply via email to

[Prev in Thread] Current Thread [Next in Thread]