This is more of a theoretical question, as I haven't written any code just yet.
I'm trying to iterate on a function that is supposed to output some value.
What I need is to somehow record the iteration # along with the inputs and result of the iteration function.
For example, if I am iterating on the Pythagorean function, I would like the following to output:
Iteration # | Inputs | Output |
---|---|---|
1 | 0, 1 | 1 |
2 | 1, 1 | 2 |
3 | 1, 2 | 3 |
4 | 2, 3 | 5 |
5 | 3, 5 | 8 |
I wonder how I can store these 3 different values in kdb, I know Python and other languages have many options but I don't know if this is possible in this language.
Also, if the input variable is a table, would that impact the ability to store in any way? What about if there would be 100,000 iterations?
To iterate the inputs, you can use an accumulator:
q)0N!inputs:{last[x],sum x}\[5;0 1];
(0 1;1 1;1 2;2 3;3 5;5 8)
q)([] Iteration:1+til count inputs; Inputs:inputs; Output:sum each inputs)
Iteration Inputs Output
-----------------------
1 0 1 1
2 1 1 2
3 1 2 3
4 2 3 5
5 3 5 8
6 5 8 13
Another way is to accumulate a dict which forms a table:
q)i:`iter`in`out!(1;0 1;1)
q){x[`iter]+:1;x[`in]:x[`in;1],sum x`in;x[`out]:sum x`in;x}\[5;i]
iter in out
------------
1 0 1 1
2 1 1 2
3 1 2 3
4 2 3 5
5 3 5 8
6 5 8 13