Richard Kubina wrote:
>
> Hi, first time poster, new J user.
> I cannot figure out how to do a certain sorta algorithm in J without a for
> loop.
> I have an array of data:
> a1=: 10 7 8 3 5
>
> and the other to be calculated iteratively, except for the first value
> which
> is already known.
> a2=: 6
>
> So I need to take the 6 and pair it with 10, do some calculations append
> the
> new number to the end of a2.
> I used this function to do this
>
> GetEWMAData =: monad : 'for_x. }.SubGroupedData do. EWMAData=:EWMAData,
> (lambda*x)+((1-lambda)*({:EWMAData)) end.'
>
> where lambda is 0.2, SubGroupedData is a1 here and EWMAData is a2, getting
> updated by appending the new number to the end of the list.
> Is there a way to do this without having the reassigning EWMAData itself
> with something appended to it, and using a for loop?
> Thanks!
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
>
Hi Richard,
That is an interesting problem. One solution is:
creducel =: adverb : '([,{...@] x {:@[)~/@:|.@,'
F =: adverb : '(m * [) + (1 - m) * ]'
GetEWMADataA =: F creducel
6 (0.2 GetEWMADataA) a1
6 6.8 6.84 7.072 6.2576 6.00608
If you're going for a one-liner, GetEWMADataA can be re-expressed as:
GetEWMADataA1 =: adverb : '([ , {...@] ((x*[) + (1-x)*]) {:@[)~/@:|.@,'
Explanation:
Computation you're trying to describe is to collect all of
the following values:
x
x f y1
(x f y1) f y2
...
(...((x f y1)f y2)...)f yn
where x is a1, yi are elements of a2 and f is the expression
from you post.
This can be modeled after reduce/fold function from functional
languages, which is similar to and different from the Insert (/) of J.
Assuming that both arguments of f are of the same type, we
can solve the problem by the following collect-reduce-from-left:
creducel =: adverb : '([,{...@] x {:@[)~/@:|.@,'
GetEWMAData =: f creducel
f=:dyad : '(lambda * x) + y * 1-lambda'
lambda =: 0.2
6 GetEWMAData a1
6 6.8 6.84 7.072 6.2576 6.00608
lambda =: 0.25
6 GetEWMAData a1
6 7 7 7.25 6.1875 5.89062
lambda =: 0.15
6 GetEWMAData a1
6 6.6 6.66 6.861 6.28185 6.08957
Now, to get rid of the implicit dependence of f on the value
of lambda, we should define f as an adverb (Raul also suggested
this, but for some reason I couldn't get his solution to work):
F =: adverb : '(m * [) + (1 - m) * ]'
F is like a function that generates f for a given value of lambda:
0.25 F
(0.25 * [) + 0.75 * ]
Now we can do without explicit mention of lambda:
GetEWMADataA =: F creducel
6 (0.2 GetEWMADataA) a1
6 6.8 6.84 7.072 6.2576 6.00608
6 (0.25 GetEWMADataA) a1
6 7 7 7.25 6.1875 5.89062
GetEWMADataA is an adverb that produces a verb solving
the problem for a given value of lambda:
0.25 GetEWMADataA
([ , {...@] ((0.25 * [) + 0.75 * ]) {:@[)~/@:|.@,
We can also further simplify this by considering
collect-reduce-from-right, which is slightly simpler to
define in J due to the J's assumed right-to-left order
of evaluation.
Cheers,
Viktor
--
View this message in context:
http://old.nabble.com/Iterative-Updating-of-an-Array--tp26372897s24193p26376918.html
Sent from the J Programming mailing list archive at Nabble.com.
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm