RobinHankin / onion

R functionality to deal with quaternions and octonions
6 stars 1 forks source link

`onion_cumprod` in the other direction #24

Open stla opened 1 year ago

stla commented 1 year ago

Hello Robin,

I did a mistake in my program: I wanted q[1], q[2]*q[1], q[3]*q[2]*q[1], etc, and I used onion_cumprod, which operates in the other direction. Maybe an argument direction=left/right would be nice. Or maybe you could just write in the Note that to get the cumulative products in the other direction, one can do Conj(cumprod(Conj(q)).

RobinHankin commented 1 year ago

that's a great idea! I wonder if there is equivalent functionality for cumulative products of matrices.
I guess rev(cumprod(rev(q))) might work. But for cumulative products of octonions we would have to be careful about associativity rules.

But I like this suggestion. I like it a lot.

stla commented 1 year ago

Hey, rev(cumprod(rev(q))) was my first idea, it is totally wrong!

Instead of direction="left"/"right" one could use right=FALSE/TRUE, as in the Reduce function.

RobinHankin commented 1 year ago

You are quite right:

library(onion)
(q <- rquat(3))
#>           [1]        [2]        [3]
#> Re  1.4105141 -0.5452220 -0.1020393
#> i  -2.1535560  1.0663027  0.7723835
#> j  -0.2215671  0.7452401 -1.0888253
#> k  -1.5097004 -2.1043198 -0.2329590
c(q[1],q[2]*q[1],q[3]*q[2]*q[1])
#>           [1]        [2]      [3]
#> Re  1.4105141 -1.4844724 7.094302
#> i  -2.1535560  1.0868640 1.291626
#> j  -0.2215671  7.3135432 1.216540
#> k  -1.5097004 -0.7763921 7.257309
Conj(cumprod(Conj(q)))
#>           [1]        [2]      [3]
#> Re  1.4105141 -1.4844724 7.094302
#> i  -2.1535560  1.0868640 1.291626
#> j  -0.2215671  7.3135432 1.216540
#> k  -1.5097004 -0.7763921 7.257309
rev(cumprod(rev(q)))
#>         [1]        [2]        [3]
#> Re 7.094302 -0.4467444 -0.1020393
#> i  1.291626  1.9349217  0.7723835
#> j  1.216540  1.8945449 -1.0888253
#> k  7.257309  2.0783663 -0.2329590
c(q[3]*q[2]*q[1],q[3]*q[2],q[3])
#>         [1]        [2]        [3]
#> Re 7.094302 -0.4467444 -0.1020393
#> i  1.291626  1.9349217  0.7723835
#> j  1.216540  1.8945449 -1.0888253
#> k  7.257309  2.0783663 -0.2329590

Created on 2023-06-26 with reprex v2.0.2