ktye / i

interpret
99 stars 16 forks source link

power of complex numbers #43

Open TheFausap opened 1 year ago

TheFausap commented 1 year ago

I was trying to get an integer power of a complex number and I created a function:

pN:{t:y#x;$[y=0;1a;*/t]}

using it in this function (it generates a matrix representing a quantum Fourier transform of log2(x) qubits):

QFT:{(1%%x)*(pN[om x]''((!x)*\:!x))}

where om function is:

om:{angle[1;360%x]}

Can some rounding be applied for the angle part? For example, the matrix for 3 qubits (QFT 8) is:

0.353553a 0.353553a 0.353553a 0.353553a 0.353553a 0.353553a 0.353553..
0.353553a 0.353553a44.999999 0.353553a89.999999 0.353553a134.999999 ..
0.353553a 0.353553a89.999999 0.353553a179.999999 0.353553a270 0.3535..
0.353553a 0.353553a134.999999 0.353553a270 0.353553a44.999999 0.3535..
0.353553a 0.353553a179.999999 0.353553a359.999999 0.353553a179.99999..
0.353553a 0.353553a224.999999 0.353553a89.999999 0.353553a314.999999..
0.353553a 0.353553a270 0.353553a179.999999 0.353553a89.999999 0.3535..
0.353553a 0.353553a315 0.353553a269.999999 0.353553a224.999999 0.353..

thanks Fausto

ktye commented 1 year ago

you could calculate integer powers like that:

pN:{angle[(abs x)^y;y*angle x]}

float^int is power. angle has a monadic and a dyadic form (get, construct). but improves some numbers but not all.

or you can round the angle of the result z (complex matrix)

angle'[abs z;_0.5+angle z]

but that does not fix all numbers as well.

the right way would be better floating point formatting. function sf formats floats: https://github.com/ktye/i/blob/master/string.go#L107 it's rather minimalistic but everything else needs much more code. ngn has correct rounding (ryu's algorithm), but no complex.

in the interpreter i usually live with that. for a real application, i would embed the interpreter and display the result including reformatting the numbers. you usually want more control, like number of digits. all of that is not included.

TheFausap commented 1 year ago

the matrix rounding is quite good for my purpose, but I don't understand why it's missing some values :)

z:QFT 8
 angle'[abs z;_0.5+angle z]
0.353553a 0.353553a 0.353553a 0.353553a 0.353553a 0.353553a 0.353553..
0.353553a 0.353553a44.999999 0.353553a90 0.353553a135 0.353553a180 0..
0.353553a 0.353553a90 0.353553a180 0.353553a270 0.353553a360 0.35355..
0.353553a 0.353553a135 0.353553a270 0.353553a45 0.353553a180 0.35355..
0.353553a 0.353553a180 0.353553a360 0.353553a180 0.353553a360 0.3535..
0.353553a 0.353553a225 0.353553a90 0.353553a315 0.353553a180 0.35355..
0.353553a 0.353553a270 0.353553a180 0.353553a90 0.353553a360 0.35355..
0.353553a 0.353553a315 0.353553a270 0.353553a225 0.353553a180 0.3535..
ktye commented 1 year ago

because angle[r;a] regenerates a complex number with sin/cos on the rounded a. complex is stored internally as real,image. printing converts back to polar form.

TheFausap commented 1 year ago

is there a way to access the internal form?

ktye commented 1 year ago

you can get the real/imag part with _z, imag z or you can convert the underlying memory to floats (of twice the size), e.g.

 `f 1a45 2a30
0.707106 0.707106 1.73205 0.999999
 `f@,1a45 /atoms must be enlisted
0.707106 0.707106