How to compute with Octonion numbers in Z3 - z3
In a previous post quaternions, a code was presented to compute with quaternions numbers using Z3. In this post is presented a code Octonion to compute with octonion numbers in Z3.
Using the "Octonion code" the following example is solved:
Code:
x = Octonion("x")
s = Tactic('qfnra-nlsat').solver()
s.add(x*x + 2 == 0)
print(s.check())
m = s.model()
print m
Output:
sat
[x.i3 = 1/2,
x.i7 = -1/2,
x.i2 = -1/2,
x.i5 = 1,
x.i1 = 0,
x.r = 0,
x.i6 = -1/2,
x.i4 = 0]
This result was verified using Maple.
Other example:
Code :
x = Octonion("x")
y = Octonion("y")
s = Tactic('qfnra-nlsat').solver()
s.add(x*y + 30 == 0, x - y - y == 10)
print(s.check())
m = s.model()
print m
Output:
sat
[y.i3 = 1/2,
y.i7 = -1/2,
y.i2 = -1/2,
y.i5 = 1,
y.i1 = -1,
y.r = -5/2,
y.i6 = 1/2,
y.i4 = -2.3979157616?,
x.i7 = -1,
x.i6 = 1,
x.i5 = 2,
x.i4 = -4.7958315233?,
x.i3 = 1,
x.i2 = -1,
x.i1 = -2,
x.r = 5]
Other example:
Proving that
x * y != y * x
Code:
x = Octonion("x")
y = Octonion("y")
a1, b1, c1, d1, e1, f1, g1, h1 = Reals('a1 b1 c1 d1 e1 f1 g1 h1')
a2, b2, c2, d2, e2, f2, g2, h2 = Reals('a2 b2 c2 d2 e2 f2 g2 h2')
x.r = a1
x.i1 = b1
x.i2 = c1
x.i3 = d1
x.i4 = e1
x.i5 = f1
x.i6 = g1
x.i7 = h1
y.r = a2
y.i1 = b2
y.i2 = c2
y.i3 = d2
y.i4 = e2
y.i5 = f2
y.i6 = g2
y.i7 = h2
print simplify((x * y - y * x).r)
print simplify((x * y - y * x).i1)
print simplify((x * y - y * x).i2)
print simplify((x * y - y * x).i3)
print simplify((x * y - y * x).i4)
print simplify((x * y - y * x).i5)
print simplify((x * y - y * x).i6)
print simplify((x * y - y * x).i7)
Output:
0
-2·c1·e2 + -2·f1·g2 + 2·f2·g1 + 2·c2·e1 + 2·d2·h1 + -2·d1·h2
2·d2·f1 + -2·g1·h2 + 2·g2·h1 + -2·b2·e1 + -2·d1·f2 + 2·b1·e2
2·c1·f2 + 2·b1·h2 + -2·b2·h1 + 2·e2·g1 + -2·e1·g2 + -2·c2·f1
-2·d2·g1 + 2·b2·c1 + 2·d1·g2 + -2·b1·c2 + -2·f1·h2 + 2·f2·h1
-2·b2·g1 + 2·e1·h2 + -2·c1·d2 + 2·b1·g2 + -2·e2·h1 + 2·c2·d1
2·d2·e1 + 2·b2·f1 + -2·d1·e2 + -2·c2·h1 + -2·b1·f2 + 2·c1·h2
-2·b1·d2 + -2·e1·f2 + -2·c1·g2 + 2·c2·g1 + 2·e2·f1 + 2·b2·d1
Other example : Proving that
(x * y) * z != x * (y * z)
Code:
x = Octonion("x")
y = Octonion("y")
z = Octonion("z")
a1, b1, c1, d1, e1, f1, g1, h1 = Reals('a1 b1 c1 d1 e1 f1 g1 h1')
a2, b2, c2, d2, e2, f2, g2, h2 = Reals('a2 b2 c2 d2 e2 f2 g2 h2')
a3, b3, c3, d3, e3, f3, g3, h3 = Reals('a3 b3 c3 d3 e3 f3 g3 h3')
x.r = a1
x.i1 = b1
x.i2 = c1
x.i3 = d1
x.i4 = e1
x.i5 = f1
x.i6 = g1
x.i7 = h1
y.r = a2
y.i1 = b2
y.i2 = c2
y.i3 = d2
y.i4 = e2
y.i5 = f2
y.i6 = g2
y.i7 = h2
z.r = a3
z.i1 = b3
z.i2 = c3
z.i3 = d3
z.i4 = e3
z.i5 = f3
z.i6 = g3
z.i7 = h3
print simplify(((x * y) * z - x * (y * z)).r, som = True)
print "end r"
print simplify(((x * y) * z - x * (y * z)).i1, som = True)
print "end i1"
print simplify(((x * y) * z - x * (y * z)).i2, som = True)
print "end i2"
print simplify(((x * y) * z - x * (y * z)).i3, som = True)
print "end i3"
print simplify(((x * y) * z - x * (y * z)).i4, som = True)
print "end i4"
print simplify(((x * y) * z - x * (y * z)).i5, som = True)
print "end i5"
print simplify(((x * y) * z - x * (y * z)).i6, som = True)
print "end i6"
print simplify(((x * y) * z - x * (y * z)).i7, som = True)
print "end i7"
Output:
0
end r
-2·d2·e3·f1 + 2·e3·g1·h2 + -2·e3·g2·h1 + 2·d1·e3·f2 +-2·e1·g3·h2 + 2·c1·d2·g3 +
2·e2·g3·h1 + -2·c2·d1·g3 + 2·d2·e1·f3 + -2·d1·e2·f3 +-2·c2·f3·h1 + 2·c1·f3·h2 +
-2·c3·d2·g1 + 2·c3·d1·g2 + -2·c3·f1·h2 + 2·c3·f2·h1 +-2·d3·e1·f2 + -2·c1·d3·g2 +
2·c2·d3·g1 + 2·d3·e2·f1 + -2·c1·f2·h3 + -2·e2·g1·h3 + 2·e1·g2·h3 + 2·c2·f1·h3
end i1
-2·b2·d3·g1 + 2·d3·e1·h2 + 2·b1·d3·g2 + -2·d3·e2·h1 + -2·d2·e1·h3 + -2·b2·f1·h3 +
2·d1·e2·h3 + 2·b1·f2·h3 +-2·b1·d2·g3 + -2·e1·f2·g3 + 2·e2·f1·g3 + 2·b2·d1·g3 +
2·b3·d2·g1 + -2·b3·d1·g2 + 2·b3·f1·h2 + -2·b3·f2·h1 + -2·b1·f3·h2 + 2·b2·f3·h1 +
-2·e2·f3·g1 + 2·e1·f3·g2 + -2·e3·f1·g2 + 2·e3·f2·g1 + 2·d2·e3·h1 +-2·d1·e3·h2
end i2
-2·f3·g1·h2 + 2·f3·g2·h1 + -2·b2·e1·f3 + 2·b1·e2·f3 + -2·c1·e2·h3 +-2·f1·g2·h3 +
2·f2·g1·h3 + 2·c2·e1·h3 + 2·b3·e1·f2 + 2·b3·c1·g2 +-2·b3·c2·g1 + -2·b3·e2·f1 +
2·b2·e3·f1 +-2·c2·e3·h1 + -2·b1·e3·f2 + 2·c1·e3·h2 + -2·b2·c1·g3 + 2·b1·c2·g3 +
2·f1·g3·h2 + -2·f2·g3·h1 + 2·b2·c3·g1 + -2·c3·e1·h2 + -2·b1·c3·g2 + 2·c3·e2·h1
end i3
-2·b2·d3·f1 + 2·c2·d3·h1 +2·b1·d3·f2 + -2·c1·d3·h2 + 2·b3·d2·f1 +-2·b3·g1·h2 +
2·b3·g2·h1 +-2·b3·d1·f2 +2·c1·f2·g3 +2·b1·g3·h2 +-2·b2·g3·h1 + -2·c2·f1·g3 +
2·c3·f1·g2 + -2·c3·f2·g1 +-2·c3·d2·h1 +2·c3·d1·h2 +2·b2·g1·h3 +2·c1·d2·h3 +
-2·b1·g2·h3 +-2·c2·d1·h3 +-2·b1·d2·f3 +-2·c1·f3·g2 +2·c2·f3·g1 +2·b2·d1·f3
end i4
-2·b3·d2·e1 + 2·b3·d1·e2 + 2·b3·c2·h1 + -2·b3·c1·h2 + -2·d2·g1·h3 + 2·b2·c1·h3 +
2·d1·g2·h3 +-2·b1·c2·h3 +2·d3·g1·h2 +-2·d3·g2·h1 +2·b2·d3·e1 +-2·b1·d3·e2 +
-2·c1·e2·g3 + 2·c2·e1·g3 +2·d2·g3·h1 +-2·d1·g3·h2 +2·b1·d2·e3 + 2·c1·e3·g2 +
-2·c2·e3·g1 +-2·b2·d1·e3 +2·b1·c3·h2 +-2·b2·c3·h1 +2·c3·e2·g1 +-2·c3·e1·g2
end i5
2·b2·c1·d3 +-2·b1·c2·d3 +-2·d3·f1·h2 +2·d3·f2·h1 +2·b3·e1·h2 +-2·b3·c1·d2 +
-2·b3·e2·h1 +2·b3·c2·d1 +-2·c1·e3·f2 +-2·b1·e3·h2 +2·b2·e3·h1 +2·c2·e3·f1 +
2·b1·c3·d2 +2·c3·e1·f2 +-2·c3·e2·f1 +-2·b2·c3·d1 +2·c1·e2·f3 +-2·c2·e1·f3 +
-2·d2·f3·h1 +2·d1·f3·h2 +2·d2·f1·h3 +-2·b2·e1·h3 +-2·d1·f2·h3 + 2·b1·e2·h3
end i6
2·c1·d3·e2 +2·d3·f1·g2 +-2·d3·f2·g1 +-2·c2·d3·e1 +2·d2·f3·g1 +-2·b2·c1·f3 +
-2·d1·f3·g2 +2·b1·c2·f3 +-2·d2·f1·g3 +2·b2·e1·g3 +2·d1·f2·g3 +-2·b1·e2·g3 +
2·c3·d2·e1 +2·b2·c3·f1 +-2·c3·d1·e2 +-2·b1·c3·f2 +-2·b2·e3·g1 +-2·c1·d2·e3 +
2·b1·e3·g2 +2·c2·d1·e3 +2·b3·c1·f2 +2·b3·e2·g1 +-2·b3·e1·g2 +-2·b3·c2·f1
end i7
Run this example online here
Other example: Proving that the octonions form an alternative algebra, it is to say:
x * (x * y) = (x * x) * y
(y * x) * x = y * (x * x)
Code:
x = Octonion("x")
y = Octonion("y")
a1, b1, c1, d1, e1, f1, g1, h1 = Reals('a1 b1 c1 d1 e1 f1 g1 h1')
a2, b2, c2, d2, e2, f2, g2, h2 = Reals('a2 b2 c2 d2 e2 f2 g2 h2')
x.r = a1
x.i1 = b1
x.i2 = c1
x.i3 = d1
x.i4 = e1
x.i5 = f1
x.i6 = g1
x.i7 = h1
y.r = a2
y.i1 = b2
y.i2 = c2
y.i3 = d2
y.i4 = e2
y.i5 = f2
y.i6 = g2
y.i7 = h2
print simplify(((x * x) * y - x * (x * y)).r, som = True)
print simplify(((x * x) * y - x * (x * y)).i1, som = True)
print simplify(((x * x) * y - x * (x * y)).i2, som = True)
print simplify(((x * x) * y - x * (x * y)).i3, som = True)
print simplify(((x * x) * y - x * (x * y)).i4, som = True)
print simplify(((x * x) * y - x * (x * y)).i5, som = True)
print simplify(((x * x) * y - x * (x * y)).i6, som = True)
print simplify(((x * x) * y - x * (x * y)).i7, som = True)
print " Proved that x(xy) = (xx)y"
print simplify(((y * x) * x - y * (x * x)).r, som = True)
print simplify(((y * x) * x - y * (x * x)).i1, som = True)
print simplify(((y * x) * x - y * (x * x)).i2, som = True)
print simplify(((y * x) * x - y * (x * x)).i3, som = True)
print simplify(((y * x) * x - y * (x * x)).i4, som = True)
print simplify(((y * x) * x - y * (x * x)).i5, som = True)
print simplify(((y * x) * x - y * (x * x)).i6, som = True)
print simplify(((y * x) * x - y * (x * x)).i7, som = True)
print " Proved that (yx)x = y(xx)"
Output:
0
0
0
0
0
0
0
0
Proved that x(xy) = (xx)y
0
0
0
0
0
0
0
0
Proved that (yx)x = y(xx)
Run this example online here
Other example: Proving that the octonions satisfy the Moufang identities:
z * (x * (z * y)) = ((z * x) * z) * y
x * (z * (y * z)) = ((x * z) * y) * z
(z * x) * (y * z) = (z * (x * y)) * z
(z * x) * (y * z) = z * ((x * y) * z)
Code:
x = Octonion("x")
y = Octonion("y")
z = Octonion("z")
a1, b1, c1, d1, e1, f1, g1, h1 = Reals('a1 b1 c1 d1 e1 f1 g1 h1')
a2, b2, c2, d2, e2, f2, g2, h2 = Reals('a2 b2 c2 d2 e2 f2 g2 h2')
a3, b3, c3, d3, e3, f3, g3, h3 = Reals('a3 b3 c3 d3 e3 f3 g3 h3')
x.r = a1
x.i1 = b1
x.i2 = c1
x.i3 = d1
x.i4 = e1
x.i5 = f1
x.i6 = g1
x.i7 = h1
y.r = a2
y.i1 = b2
y.i2 = c2
y.i3 = d2
y.i4 = e2
y.i5 = f2
y.i6 = g2
y.i7 = h2
z.r = a3
z.i1 = b3
z.i2 = c3
z.i3 = d3
z.i4 = e3
z.i5 = f3
z.i6 = g3
z.i7 = h3
print simplify((z * (x * (z * y)) -((z * x) * z) * y).r, som = True)
print simplify((z * (x * (z * y)) -((z * x) * z) * y).i1, som = True)
print simplify((z * (x * (z * y)) -((z * x) * z) * y).i2, som = True)
print simplify((z * (x * (z * y)) -((z * x) * z) * y).i3, som = True)
print simplify((z * (x * (z * y)) -((z * x) * z) * y).i4, som = True)
print simplify((z * (x * (z * y)) -((z * x) * z) * y).i5, som = True)
print simplify((z * (x * (z * y)) -((z * x) * z) * y).i6, som = True)
print simplify((z * (x * (z * y)) -((z * x) * z) * y).i7, som = True)
print "Proved that z(x(zy)) = ((zx)z)y"
print simplify((x * (z * (y * z)) -((x * z) * y) * z).r, som = True)
print simplify((x * (z * (y * z)) -((x * z) * y) * z).i1, som = True)
print simplify((x * (z * (y * z)) -((x * z) * y) * z).i2, som = True)
print simplify((x * (z * (y * z)) -((x * z) * y) * z).i3, som = True)
print simplify((x * (z * (y * z)) -((x * z) * y) * z).i4, som = True)
print simplify((x * (z * (y * z)) -((x * z) * y) * z).i5, som = True)
print simplify((x * (z * (y * z)) -((x * z) * y) * z).i6, som = True)
print simplify((x * (z * (y * z)) -((x * z) * y) * z).i7, som = True)
print "Proved that x(z(yz)) = ((xz)y)z"
print simplify(((z * x) * (y * z) - (z * (x * y))*z).r, som = True)
print simplify(((z * x) * (y * z) - (z * (x * y))*z).i1, som = True)
print simplify(((z * x) * (y * z) - (z * (x * y))*z).i2, som = True)
print simplify(((z * x) * (y * z) - (z * (x * y))*z).i3, som = True)
print simplify(((z * x) * (y * z) - (z * (x * y))*z).i4, som = True)
print simplify(((z * x) * (y * z) - (z * (x * y))*z).i5, som = True)
print simplify(((z * x) * (y * z) - (z * (x * y))*z).i6, som = True)
print simplify(((z * x) * (y * z) - (z * (x * y))*z).i7, som = True)
print "Proved that (zx)(yz) = (z(xy))z"
print simplify(((z * x) * (y * z) - z * ((x * y) * z)).r, som = True)
print simplify(((z * x) * (y * z) - z * ((x * y) * z)).i1, som = True)
print simplify(((z * x) * (y * z) - z * ((x * y) * z)).i2, som = True)
print simplify(((z * x) * (y * z) - z * ((x * y) * z)).i3, som = True)
print simplify(((z * x) * (y * z) - z * ((x * y) * z)).i4, som = True)
print simplify(((z * x) * (y * z) - z * ((x * y) * z)).i5, som = True)
print simplify(((z * x) * (y * z) - z * ((x * y) * z)).i6, som = True)
print simplify(((z * x) * (y * z) - z * ((x * y) * z)).i7, som = True)
print "Proved that (zx)(yz) = z((xy)z)"
Output:
0
0
0
0
0
0
0
0
Proved that z(x(zy)) = ((zx)z)y
0
0
0
0
0
0
0
0
Proved that x(z(yz)) = ((xz)y)z
0
0
0
0
0
0
0
0
Proved that (zx)(yz) = (z(xy))z
0
0
0
0
0
0
0
0
Proved that (zx)(yz) = z((xy)z)
Run this example online here
Other example : Proving that the octonions
A = (1 + e1)/sqrt(2)
B = (1 + e2)/sqrt(2)
C = (1 + e3)/sqrt(2)
generate a representation of the Braid Group, it is to say, we have that
ABA = BAB
ACA = CAC
BCB = CBC.
Code:
A = Octonion('A')
B = Octonion('B')
C = Octonion('C')
A.r = 1/Sqrt(2)
A.i1 = 1/Sqrt(2)
A.i2 = 0
A.i3 = 0
A.i4 = 0
A.i5 = 0
A.i6 = 0
A.i7 = 0
B.r = 1/Sqrt(2)
B.i1 = 0
B.i2 = 1/Sqrt(2)
B.i3 = 0
B.i4 = 0
B.i5 = 0
B.i6 = 0
B.i7 = 0
C.r = 1/Sqrt(2)
C.i1 = 0
C.i2 = 0
C.i3 = 1/Sqrt(2)
C.i4 = 0
C.i5 = 0
C.i6 = 0
C.i7 = 0
print simplify((A*B*A-B*A*B).r)
print simplify((A*B*A-B*A*B).i1)
print simplify((A*B*A-B*A*B).i2)
print simplify((A*B*A-B*A*B).i3)
print simplify((A*B*A-B*A*B).i4)
print simplify((A*B*A-B*A*B).i5)
print simplify((A*B*A-B*A*B).i6)
print simplify((A*B*A-B*A*B).i7)
print "Proved : ABA = BAB:"
print simplify((A*C*A-C*A*C).r)
print simplify((A*C*A-C*A*C).i1)
print simplify((A*C*A-C*A*C).i2)
print simplify((A*C*A-C*A*C).i3)
print simplify((A*C*A-C*A*C).i4)
print simplify((A*C*A-C*A*C).i5)
print simplify((A*C*A-C*A*C).i6)
print simplify((A*C*A-C*A*C).i7)
print "Proved : ACA = CAC:"
print simplify((B*C*B-C*B*C).r)
print simplify((B*C*B-C*B*C).i1)
print simplify((B*C*B-C*B*C).i2)
print simplify((B*C*B-C*B*C).i3)
print simplify((B*C*B-C*B*C).i4)
print simplify((B*C*B-C*B*C).i5)
print simplify((B*C*B-C*B*C).i6)
print simplify((B*C*B-C*B*C).i7)
print "Proved : BCB = CBC:"
Output:
0
0
0
0
0
0
0
0
Proved : ABA = BAB:
0
0
0
0
0
0
0
0
Proved : ACA = CAC:
0
0
0
0
0
0
0
0
Proved : BCB = CBC:
Run this example online here
Other example: Proving that the octonions
A = (1 + e4)/sqrt(2)
B = (1 + e5)/sqrt(2)
C = (1 + e6)/sqrt(2)
generate a representation of the Braid Group, it is to say, we have that
ABA = BAB
ACA = CAC
BCB = CBC
Code:
A = Octonion('A')
B = Octonion('B')
C = Octonion('C')
A.r = 1/Sqrt(2)
A.i1 = 0
A.i2 = 0
A.i3 = 0
A.i4 = 1/Sqrt(2)
A.i5 = 0
A.i6 = 0
A.i7 = 0
B.r = 1/Sqrt(2)
B.i1 = 0
B.i2 = 0
B.i3 = 0
B.i4 = 0
B.i5 = 1/Sqrt(2)
B.i6 = 0
B.i7 = 0
C.r = 1/Sqrt(2)
C.i1 = 0
C.i2 = 0
C.i3 = 0
C.i4 = 0
C.i5 = 0
C.i6 = 1/Sqrt(2)
C.i7 = 0
print simplify((A*B*A-B*A*B).r)
print simplify((A*B*A-B*A*B).i1)
print simplify((A*B*A-B*A*B).i2)
print simplify((A*B*A-B*A*B).i3)
print simplify((A*B*A-B*A*B).i4)
print simplify((A*B*A-B*A*B).i5)
print simplify((A*B*A-B*A*B).i6)
print simplify((A*B*A-B*A*B).i7)
print "Proved : ABA = BAB:"
print simplify((A*C*A-C*A*C).r)
print simplify((A*C*A-C*A*C).i1)
print simplify((A*C*A-C*A*C).i2)
print simplify((A*C*A-C*A*C).i3)
print simplify((A*C*A-C*A*C).i4)
print simplify((A*C*A-C*A*C).i5)
print simplify((A*C*A-C*A*C).i6)
print simplify((A*C*A-C*A*C).i7)
print "Proved : ACA = CAC:"
print simplify((B*C*B-C*B*C).r)
print simplify((B*C*B-C*B*C).i1)
print simplify((B*C*B-C*B*C).i2)
print simplify((B*C*B-C*B*C).i3)
print simplify((B*C*B-C*B*C).i4)
print simplify((B*C*B-C*B*C).i5)
print simplify((B*C*B-C*B*C).i6)
print simplify((B*C*B-C*B*C).i7)
print "Proved : BCB = CBC:"
Output:
0
0
0
0
0
0
0
0
Proved : ABA = BAB:
0
0
0
0
0
0
0
0
Proved : ACA = CAC:
0
0
0
0
0
0
0
0
Proved : BCB = CBC:
Run this example online here
Other example: Proving that the octonions
A = (1 + e5)/sqrt(2)
B = (1 + e6)/sqrt(2)
C = (1 + e7)/sqrt(2)
generate a representation of the Braid Group, it is to say, we have that
ABA = BAB
ACA = CAC
BCB = CBC
Code:
A = Octonion('A')
B = Octonion('B')
C = Octonion('C')
A.r = 1/Sqrt(2)
A.i1 = 0
A.i2 = 0
A.i3 = 0
A.i4 = 0
A.i5 = 1/Sqrt(2)
A.i6 = 0
A.i7 = 0
B.r = 1/Sqrt(2)
B.i1 = 0
B.i2 = 0
B.i3 = 0
B.i4 = 0
B.i5 = 0
B.i6 = 1/Sqrt(2)
B.i7 = 0
C.r = 1/Sqrt(2)
C.i1 = 0
C.i2 = 0
C.i3 = 0
C.i4 = 0
C.i5 = 0
C.i6 = 0
C.i7 = 1/Sqrt(2)
print simplify((A*B*A-B*A*B).r)
print simplify((A*B*A-B*A*B).i1)
print simplify((A*B*A-B*A*B).i2)
print simplify((A*B*A-B*A*B).i3)
print simplify((A*B*A-B*A*B).i4)
print simplify((A*B*A-B*A*B).i5)
print simplify((A*B*A-B*A*B).i6)
print simplify((A*B*A-B*A*B).i7)
print "Proved : ABA = BAB:"
print simplify((A*C*A-C*A*C).r)
print simplify((A*C*A-C*A*C).i1)
print simplify((A*C*A-C*A*C).i2)
print simplify((A*C*A-C*A*C).i3)
print simplify((A*C*A-C*A*C).i4)
print simplify((A*C*A-C*A*C).i5)
print simplify((A*C*A-C*A*C).i6)
print simplify((A*C*A-C*A*C).i7)
print "Proved : ACA = CAC:"
print simplify((B*C*B-C*B*C).r)
print simplify((B*C*B-C*B*C).i1)
print simplify((B*C*B-C*B*C).i2)
print simplify((B*C*B-C*B*C).i3)
print simplify((B*C*B-C*B*C).i4)
print simplify((B*C*B-C*B*C).i5)
print simplify((B*C*B-C*B*C).i6)
print simplify((B*C*B-C*B*C).i7)
print "Proved : BCB = CBC:"
Output:
0
0
0
0
0
0
0
0
Proved : ABA = BAB:
0
0
0
0
0
0
0
0
Proved : ACA = CAC:
0
0
0
0
0
0
0
0
Proved : BCB = CBC:
Run this example online here
Other example : Proving that the octonions
A = (1 + e1)/sqrt(2) E = (1 + e5)/sqrt(2)
B = (1 + e2)/sqrt(2) F = (1 + e6)/sqrt(2)
C = (1 + e3)/sqrt(2) G = (1 + e7)/sqrt(2)
D = (1 + e4)/sqrt(2)
generate a representation of the Braid Group, it is to say, we have that
ABA = BAB ACA = CAC BCB = CBC ADA = DAD BDB = DBD
CDC = DCD AEA = EAE BEB = EBE CEC = ECE DED = EDE
AFA = FAF BFB = FBF CFC = FCF DFD = FDF EFE = FEF
AGA = GAG BGB = GBG CGC = GCG DGD = GDg EGE = GEG FGF = GFG
Code:
A = Octonion('A')
B = Octonion('B')
C = Octonion('C')
D = Octonion('D')
E = Octonion('E')
F = Octonion('F')
G = Octonion('G')
A.r = 1/Sqrt(2)
A.i1 = 1/Sqrt(2)
A.i2 = 0
A.i3 = 0
A.i4 = 0
A.i5 = 0
A.i6 = 0
A.i7 = 0
B.r = 1/Sqrt(2)
B.i1 = 0
B.i2 = 1/Sqrt(2)
B.i3 = 0
B.i4 = 0
B.i5 = 0
B.i6 = 0
B.i7 = 0
C.r = 1/Sqrt(2)
C.i1 = 0
C.i2 = 0
C.i3 = 1/Sqrt(2)
C.i4 = 0
C.i5 = 0
C.i6 = 0
C.i7 = 0
D.r = 1/Sqrt(2)
D.i1 = 0
D.i2 = 0
D.i3 = 0
D.i4 = 1/Sqrt(2)
D.i5 = 0
D.i6 = 0
D.i7 = 0
E.r = 1/Sqrt(2)
E.i1 = 0
E.i2 = 0
E.i3 = 0
E.i4 = 0
E.i5 = 1/Sqrt(2)
E.i6 = 0
E.i7 = 0
F.r = 1/Sqrt(2)
F.i1 = 0
F.i2 = 0
F.i3 = 0
F.i4 = 0
F.i5 = 0
F.i6 = 1/Sqrt(2)
F.i7 = 0
G.r = 1/Sqrt(2)
G.i1 = 0
G.i2 = 0
G.i3 = 0
G.i4 = 0
G.i5 = 0
G.i6 = 0
G.i7 = 1/Sqrt(2)
print simplify((A*B*A-B*A*B).r)
print simplify((A*B*A-B*A*B).i1)
print simplify((A*B*A-B*A*B).i2)
print simplify((A*B*A-B*A*B).i3)
print simplify((A*B*A-B*A*B).i4)
print simplify((A*B*A-B*A*B).i5)
print simplify((A*B*A-B*A*B).i6)
print simplify((A*B*A-B*A*B).i7)
print "Proved : ABA = BAB:"
print simplify((A*C*A-C*A*C).r)
print simplify((A*C*A-C*A*C).i1)
print simplify((A*C*A-C*A*C).i2)
print simplify((A*C*A-C*A*C).i3)
print simplify((A*C*A-C*A*C).i4)
print simplify((A*C*A-C*A*C).i5)
print simplify((A*C*A-C*A*C).i6)
print simplify((A*C*A-C*A*C).i7)
print "Proved : ACA = CAC:"
print simplify((B*C*B-C*B*C).r)
print simplify((B*C*B-C*B*C).i1)
print simplify((B*C*B-C*B*C).i2)
print simplify((B*C*B-C*B*C).i3)
print simplify((B*C*B-C*B*C).i4)
print simplify((B*C*B-C*B*C).i5)
print simplify((B*C*B-C*B*C).i6)
print simplify((B*C*B-C*B*C).i7)
print "Proved : BCB = CBC:"
print simplify((A*D*A-D*A*D).r)
print simplify((A*D*A-D*A*D).i1)
print simplify((A*D*A-D*A*D).i2)
print simplify((A*D*A-D*A*D).i3)
print simplify((A*D*A-D*A*D).i4)
print simplify((A*D*A-D*A*D).i5)
print simplify((A*D*A-D*A*D).i6)
print simplify((A*D*A-D*A*D).i7)
print "Proved : ADA = DAD:"
print simplify((B*D*B-D*B*D).r)
print simplify((B*D*B-D*B*D).i1)
print simplify((B*D*B-D*B*D).i2)
print simplify((B*D*B-D*B*D).i3)
print simplify((B*D*B-D*B*D).i4)
print simplify((B*D*B-D*B*D).i5)
print simplify((B*D*B-D*B*D).i6)
print simplify((B*D*B-D*B*D).i7)
print "Proved : BDB = DBD:"
print simplify((C*D*C-D*C*D).r)
print simplify((C*D*C-D*C*D).i1)
print simplify((C*D*C-D*C*D).i2)
print simplify((C*D*C-D*C*D).i3)
print simplify((C*D*C-D*C*D).i4)
print simplify((C*D*C-D*C*D).i5)
print simplify((C*D*C-D*C*D).i6)
print simplify((C*D*C-D*C*D).i7)
print "Proved : CDC = DCD:"
print simplify((A*E*A-E*A*E).r)
print simplify((A*E*A-E*A*E).i1)
print simplify((A*E*A-E*A*E).i2)
print simplify((A*E*A-E*A*E).i3)
print simplify((A*E*A-E*A*E).i4)
print simplify((A*E*A-E*A*E).i5)
print simplify((A*E*A-E*A*E).i6)
print simplify((A*E*A-E*A*E).i7)
print "Proved : AEA = EAE:"
print simplify((B*E*B-E*B*E).r)
print simplify((B*E*B-E*B*E).i1)
print simplify((B*E*B-E*B*E).i2)
print simplify((B*E*B-E*B*E).i3)
print simplify((B*E*B-E*B*E).i4)
print simplify((B*E*B-E*B*E).i5)
print simplify((B*E*B-E*B*E).i6)
print simplify((B*E*B-E*B*E).i7)
print "Proved : BEB = EBE:"
print simplify((C*E*C-E*C*E).r)
print simplify((C*E*C-E*C*E).i1)
print simplify((C*E*C-E*C*E).i2)
print simplify((C*E*C-E*C*E).i3)
print simplify((C*E*C-E*C*E).i4)
print simplify((C*E*C-E*C*E).i5)
print simplify((C*E*C-E*C*E).i6)
print simplify((C*E*C-E*C*E).i7)
print "Proved : CEC = ECE:"
print simplify((D*E*D-E*D*E).r)
print simplify((D*E*D-E*D*E).i1)
print simplify((D*E*D-E*D*E).i2)
print simplify((D*E*D-E*D*E).i3)
print simplify((D*E*D-E*D*E).i4)
print simplify((D*E*D-E*D*E).i5)
print simplify((D*E*D-E*D*E).i6)
print simplify((D*E*D-E*D*E).i7)
print "Proved : DED = EDE:"
print simplify((A*F*A-F*A*F).r)
print simplify((A*F*A-F*A*F).i1)
print simplify((A*F*A-F*A*F).i2)
print simplify((A*F*A-F*A*F).i3)
print simplify((A*F*A-F*A*F).i4)
print simplify((A*F*A-F*A*F).i5)
print simplify((A*F*A-F*A*F).i6)
print simplify((A*F*A-F*A*F).i7)
print "Proved : AFA = FAF:"
print simplify((B*F*B-F*B*F).r)
print simplify((B*F*B-F*B*F).i1)
print simplify((B*F*B-F*B*F).i2)
print simplify((B*F*B-F*B*F).i3)
print simplify((B*F*B-F*B*F).i4)
print simplify((B*F*B-F*B*F).i5)
print simplify((B*F*B-F*B*F).i6)
print simplify((B*F*B-F*B*F).i7)
print "Proved : BFB = FBF:"
print simplify((C*F*C-F*C*F).r)
print simplify((C*F*C-F*C*F).i1)
print simplify((C*F*C-F*C*F).i2)
print simplify((C*F*C-F*C*F).i3)
print simplify((C*F*C-F*C*F).i4)
print simplify((C*F*C-F*C*F).i5)
print simplify((C*F*C-F*C*F).i6)
print simplify((C*F*C-F*C*F).i7)
print "Proved : CFC = FCF:"
print simplify((D*F*D-F*D*F).r)
print simplify((D*F*D-F*D*F).i1)
print simplify((D*F*D-F*D*F).i2)
print simplify((D*F*D-F*D*F).i3)
print simplify((D*F*D-F*D*F).i4)
print simplify((D*F*D-F*D*F).i5)
print simplify((D*F*D-F*D*F).i6)
print simplify((D*F*D-F*D*F).i7)
print "Proved : DFD = FDF:"
print simplify((E*F*E-F*E*F).r)
print simplify((E*F*E-F*E*F).i1)
print simplify((E*F*E-F*E*F).i2)
print simplify((E*F*E-F*E*F).i3)
print simplify((E*F*E-F*E*F).i4)
print simplify((E*F*E-F*E*F).i5)
print simplify((E*F*E-F*E*F).i6)
print simplify((E*F*E-F*E*F).i7)
print "Proved : EFE = FEF:"
print simplify((A*G*A-G*A*G).r)
print simplify((A*G*A-G*A*G).i1)
print simplify((A*G*A-G*A*G).i2)
print simplify((A*G*A-G*A*G).i3)
print simplify((A*G*A-G*A*G).i4)
print simplify((A*G*A-G*A*G).i5)
print simplify((A*G*A-G*A*G).i6)
print simplify((A*G*A-G*A*G).i7)
print "Proved : AGA = GAG:"
print simplify((B*G*B-G*B*G).r)
print simplify((B*G*B-G*B*G).i1)
print simplify((B*G*B-G*B*G).i2)
print simplify((B*G*B-G*B*G).i3)
print simplify((B*G*B-G*B*G).i4)
print simplify((B*G*B-G*B*G).i5)
print simplify((B*G*B-G*B*G).i6)
print simplify((B*G*B-G*B*G).i7)
print "Proved : BGB = GBG:"
print simplify((C*G*C-G*C*G).r)
print simplify((C*G*C-G*C*G).i1)
print simplify((C*G*C-G*C*G).i2)
print simplify((C*G*C-G*C*G).i3)
print simplify((C*G*C-G*C*G).i4)
print simplify((C*G*C-G*C*G).i5)
print simplify((C*G*C-G*C*G).i6)
print simplify((C*G*C-G*C*G).i7)
print "Proved : CGC = GCG:"
print simplify((D*G*D-G*D*G).r)
print simplify((D*G*D-G*D*G).i1)
print simplify((D*G*D-G*D*G).i2)
print simplify((D*G*D-G*D*G).i3)
print simplify((D*G*D-G*D*G).i4)
print simplify((D*G*D-G*D*G).i5)
print simplify((D*G*D-G*D*G).i6)
print simplify((D*G*D-G*D*G).i7)
print "Proved : DGD = GDG:"
print simplify((E*G*E-G*E*G).r)
print simplify((E*G*E-G*E*G).i1)
print simplify((E*G*E-G*E*G).i2)
print simplify((E*G*E-G*E*G).i3)
print simplify((E*G*E-G*E*G).i4)
print simplify((E*G*E-G*E*G).i5)
print simplify((E*G*E-G*E*G).i6)
print simplify((E*G*E-G*E*G).i7)
print "Proved : EGE = GEG:"
print simplify((F*G*F-G*F*G).r)
print simplify((F*G*F-G*F*G).i1)
print simplify((F*G*F-G*F*G).i2)
print simplify((F*G*F-G*F*G).i3)
print simplify((F*G*F-G*F*G).i4)
print simplify((F*G*F-G*F*G).i5)
print simplify((F*G*F-G*F*G).i6)
print simplify((F*G*F-G*F*G).i7)
print "Proved : FGF = GFG:"
Output:
0
0
0
0
0
0
0
0
Proved : ABA = BAB:
0
0
0
0
0
0
0
0
Proved : ACA = CAC:
0
0
0
0
0
0
0
0
Proved : BCB = CBC:
0
0
0
0
0
0
0
0
Proved : ADA = DAD:
0
0
0
0
0
0
0
0
Proved : BDB = DBD:
0
0
0
0
0
0
0
0
Proved : CDC = DCD:
0
0
0
0
0
0
0
0
Proved : AEA = EAE:
0
0
0
0
0
0
0
0
Proved : BEB = EBE:
0
0
0
0
0
0
0
0
Proved : CEC = ECE:
0
0
0
0
0
0
0
0
Proved : DED = EDE:
0
0
0
0
0
0
0
0
Proved : AFA = FAF:
0
0
0
0
0
0
0
0
Proved : BFB = FBF:
0
0
0
0
0
0
0
0
Proved : CFC = FCF:
0
0
0
0
0
0
0
0
Proved : DFD = FDF:
0
0
0
0
0
0
0
0
Proved : EFE = FEF:
0
0
0
0
0
0
0
0
Proved : AGA = GAG:
0
0
0
0
0
0
0
0
Proved : BGB = GBG:
0
0
0
0
0
0
0
0
Proved : CGC = GCG:
0
0
0
0
0
0
0
0
Proved : DGD = GDG:
0
0
0
0
0
0
0
0
Proved : EGE = GEG:
0
0
0
0
0
0
0
0
Proved : FGF = GFG:
Run this example online here
Other example: Proving that for an invertible octonion of the form
x = a + a1*e1 + a2*e2 + a3*e3 + a4*e4 + a5*e5 + a6*e6
we have that
x / x = 1
Run this example online here
Please let me know what do you think about the Octonion code and how the Octonion code can be improved. Many thanks.
Please let me know what do you think about the Octonion code and how the Octonion code can be improved. Many thanks.
I think an appropriate answer to this question is that it is very nice.
Related
Compiler warning: FS0058: Possible incorrect indentation
I wrote this code with Visual Studio but when I compile it, it gives me back these warnings: FS0058: Possible incorrect indentation, this token is offside of context started at position 9:80. Try indenting this token further or using standard formatting . But if I try to compile it with an OCaml compiler it works. let converti_tempo = (fun x -> if x < 0 then failwith "error" else if x < 1000 then (0, 0, x) else let rec m = x % 1000 let s = (x / 1000) % 60 let mm = ((x / 1000) / 60) % 60 in (mm,s ,m ));;
At the end of the first line you have an else. This implies that the expression of the else is going to be defined on the next line and thus will have to be indented all the way to the else and then a bit more. Code below: let converti_tempo = (fun x -> if x < 0 then failwith "error" else if x < 1000 then (0, 0, x) else let rec m = x % 1000 let s = (x / 1000) % 60 let mm = ((x / 1000) / 60) % 60 in (mm,s ,m ));; You could also write it like this: let converti_tempo = (fun x -> if x < 0 then failwith "error" else if x < 1000 then (0, 0, x) else let rec m = x % 1000 let s = (x / 1000) % 60 let mm = ((x / 1000) / 60) % 60 in (mm,s ,m ));;
I would say this is a more "idiomatic" way of writing this function let converti_tempo x = if x < 0 then failwith "error" elif x < 1000 then (0, 0, x) else let m = x % 1000 let s = x / 1000 % 60 let mm = x / 1000 / 60 % 60 (mm, s, m)
For me this compiler warning was caused by one of my "let"s not being properly aligned. Make sure all of your function declarations start at the very start of each new line.
How to compare in ResizeArray at Fsharp?
How to Sort s by funtion compare : cmp ? type Point = { x : float y : float } let s = ResizeArray<Point>() s.Add{x=1.1 ; y=1.1} s.Add{x=2.2 ; y=2.2} s.Add{x=3.3 ; y=3.3} s.Add{x=2.2 ; y=2.0} let cmp (A:Point) (B:Point) = A.x + A.y < B.x + B.y s.Sort() //s.Sort() s |> printfn("%A") And this's result , but it's not correct with function : cmp seq [{x = 1.1; y = 1.1;}; {x = 2.2; y = 2.2;}; {x = 2.2; y = 2.0;}; {x = 3.3; y = 3.3;}] I tried using sortWith but it still not correct . Can you help me to fix ? type Point = { x : float y : float } let s = ResizeArray<Point>() s.Add{x=1.1;y=1.1} s.Add{x=2.2;y=2.2} s.Add{x=1.0;y=1.0} let cmp (A:Point) (B:Point) = if (A.x+A.y) >= (B.x+B.y) then 1 else 0 List.sortWith cmp s |> List.iter (fun x -> printfn("%f %f\n") x.x x.y)
You have to provide a Comparison delegate for your type: s.Sort(fun a b -> a.x + a.y - b.x - b.y |> int)
How to pass same parameters to multiple functions?
I have a bunch of functions that I want to compute with the same inputs. Is there a better way to see the outputs than the way I chose below? open MathNet.Numerics.Distributions // The functions let EuVanillaPut S0 K T r sigma = let d1 = (log(S0/K) + (r + sigma ** 2.0 / 2.0) * T)/(sqrt(T)*sigma) let d2 = d1 - sqrt(T)*sigma K*exp(-r*T)*Normal.CDF(0.0,1.0,-d2) - S0*Normal.CDF(0.0,1.0,-d1) let BSMdelta S0 K T r sigma = let d1 = (log(S0/K) + (r + sigma ** 2.0 / 2.0) * T)/(sqrt(T)*sigma) Normal.CDF(0.0,1.0,d1) let BSMgamma S0 K T r sigma = let d1 = (log(S0/K) + (r + sigma ** 2.0 / 2.0) * T)/(sqrt(T)*sigma) Normal.PDF(0.0,1.0,d1) / (S0 * sigma * sqrt(T)) let BSMvega S0 K T r sigma = let d1 = (log(S0/K) + (r + sigma ** 2.0 / 2.0) * T)/(sqrt(T)*sigma) Normal.PDF(0.0,1.0,d1) * S0 * sqrt(T) let BSMthetacall S0 K T r sigma = let d1 = (log(S0/K) + (r + sigma ** 2.0 / 2.0) * T)/(sqrt(T)*sigma) let d2 = d1 - sqrt(T)*sigma -S0 * Normal.PDF(0.0,1.0,d1) * sigma / (2.0*sqrt(T)) - r*K*exp(-r*T)*Normal.CDF(0.0,1.0,d2) let BSMthetaput S0 K T r sigma = let d1 = (log(S0/K) + (r + sigma ** 2.0 / 2.0) * T)/(sqrt(T)*sigma) let d2 = d1 - sqrt(T)*sigma -S0 * Normal.PDF(0.0,1.0,d1) * sigma / (2.0*sqrt(T)) + r*K*exp(-r*T)*Normal.CDF(0.0,1.0,-d2) // Calling them all at once on the same inputs // So ugly! Is there a better way? (30.0, 25.0, 5.0, 0.02, 0.05) |> fun (S0, K, T, r, sigma) -> [EuVanillaPut S0 K T r sigma; BSMdelta S0 K T r sigma; BSMgamma S0 K T r sigma; BSMvega S0 K T r sigma; BSMthetacall S0 K T r sigma; BSMthetaput S0 K T r sigma] I'm pretty new to F#, should I make a type for this? Should I be using a different data structure as an input for the functions? Any and all pointers are much appreciated.
As suggested in the comments, one option is to create a list of functions and then use List.map to iterate over all the functions and call them: let results = [ EuVanillaPut; BSMdelta; BSMgamma ] |> List.map (fun f -> f 30.0 25.0 5.0 0.02 0.05) I suppose you'd then also want to extract the individual results - to do that, you can use pattern matching (but you will get a warning, because the compiler cannot know that the number of elements in the list is correct): let [euVanillaPut; bsmdelta; bsmgamma] = results To avoid the warning, you'd have to write: match results with | [euVanillaPut; bsmdelta; bsmgamma] -> // all good | _ -> failwith "This should not happen..." Alternatively, you could change the function definition to use tuple (or a record): let EuVanillaPut (S0, K, T, r, sigma) = let d1 = (log(S0/K) + (r + sigma ** 2.0 / 2.0) * T)/(sqrt(T)*sigma) let d2 = d1 - sqrt(T)*sigma K*exp(-r*T)*Normal.CDF(0.0,1.0,-d2) - S0*Normal.CDF(0.0,1.0,-d1) Then you can define a single tuple to hold the parameters and use it as an argument to multiple functions: let ps = (30.0, 25.0, 5.0, 0.02, 0.05) let euVanillaPut = EuVanillaPut ps let bsmdelta = BSMdelta ps let bsmgamma = BSMgamma ps The first approach is a clever trick, but if you are doing this often, then extracting the individual results from the list will be a bit ugly. The second approach is simpler and makes more sense if you have a lot of functions with the same group of parameters.
F#: why using outer function version faster than pass function as argument
Version (calc1) using direct outer function take about 1s. But version (calc2) with pass function as parameter of function take about 2s, that is 2x slower. Why? open System.Diagnostics open System.Numerics let width = 1920 let height = 1200 let xMin = -2.0 let xMax = 1.0 let yMin = -1.0 let yMax = 1.0 let scaleX x = float x * (xMax - xMin) / float width + xMin let scaleY y = float y * (yMax - yMin) / float height - yMax let fn (z:Complex) (c:Complex) = z * z + c let calc1 width height = let iterFn z c = let rec iterFn' (z:Complex) c n = if z.Magnitude > 2.0 || n >= 255 then n else iterFn' (fn z c) c (n + 1) iterFn' z c 0 Array.Parallel.init (width * height) (fun i -> let x, y = i % width, i / width let z, c = Complex.Zero, Complex(scaleX x, scaleY y) (x, y, iterFn z c) ) let calc2 width height fn = let iterFn z c = let rec iterFn' (z:Complex) c n = if z.Magnitude > 2.0 || n >= 255 then n else iterFn' (fn z c) c (n + 1) iterFn' z c 0 Array.Parallel.init (width * height) (fun i -> let x, y = i % width, i / width let z, c = Complex.Zero, Complex(scaleX x, scaleY y) (x, y, iterFn z c) ) Execute in F# interactive get the following results: > calc1 width height |> ignore Real: 00:00:00.943, CPU: 00:00:03.046, GC gen0: 10, gen1: 8, gen2: 2 val it : unit = () > calc2 width height fn |> ignore Real: 00:00:02.033, CPU: 00:00:07.484, GC gen0: 9, gen1: 8, gen2: 1 val it : unit = () F# 4.0.1, .NET 4.6.1
I suspect that in the first case, the fn is inlined. Passing it as a paramter prevents this optimisation from occuring, so it is slower
Recursively computing an N x N matrix determinant
I'm having an issue with this implementation. It's giving me an error that "this expression to have type bool but here has unit" on the entire block starting at the first for loop and ending with the pown expression. I'm not entirely sure what this means. let rec detMat (m : int[,] ) : int = let mutable det = 0 let mutable n = m.Length let mutable i = 0 let mutable j = 0 let mutable j = 0 let mutable j1 = 0 let mutable j2 = 0 let mTmp = Array2D.create 0 if n = 1 then det = m.[0, 0] elif n = 2 then det = m.[0, 0] * m.[1, 1] - m.[1, 0] * m.[0, 1] else det = 0 for j1 = 0 to n do for i = 1 to n do j2 = 0 for j = 0 to n do if j <> j1 then mTmp.[i-1, j2] <- m.[i, j] j2 = j2 + 1 pown -1 (1 + j1 + 1) * m.[0, j1] * detMat(mTmp, n-1) det
<- is the assignment operator for mutable variables. Your code should be det <- m.[0, 0] det <- m.[0, 0] * m.[1, 1] - m.[1, 0] * m.[0, 1] j2 <- j2 + 1 = is for equality, that's why you are getting the "this expression to have type bool" error. The second part, "but here has unit", means the compiler is expecting an arm of if to return a unit in the last expression. Carefully read this page to understand why the error message is so confusing: https://msdn.microsoft.com/en-us/library/dd233231.aspx