LINUX.ORG.RU

История изменений

Исправление Nxx, (текущая версия) :

Попросил реализовать для системы «Mathematica» код, реализующий кватернионы, без использования пакета Quaternions. Вот результат (всё, включая комментарии, написано ИИ):

Unprotect[Inverse, Conjugate, Norm];

Quaternion /: Conjugate[Quaternion[w_, x_, y_, z_]] := 
  Quaternion[w, -x, -y, -z];

(*Define the norm of a quaternion as the square root of the sum of \
the squares of its components using/:*)
Quaternion /: Norm[Quaternion[w_, x_, y_, z_]] := 
  Sqrt[w^2 + x^2 + y^2 + z^2];

(*Define the inverse of a quaternion as the conjugate divided by the \
norm squared using/:*)
Quaternion /: Inverse[Quaternion[q_]] := 
 Conjugate[Quaternion[q]]/Norm[Quaternion[q]]^2; 2;

(*Define quaternion multiplication using the formula \
(w1,x1,y1,z1)*(w2,x2,y2,z2)=(w1*w2-x1*x2-y1*y2-z1*z2,w1*x2+x1*w2+y1*\
z2-z1*y2,w1*y2-x1*z2+y1*w2+z1*x2,w1*z2+x1*y2-y1*x2+z1*w2)*)
ClearAll[QuaternionProduct]
QuaternionProduct[Quaternion[w1_, x1_, y1_, z1_], 
   Quaternion[w2_, x2_, y2_, z2_]] := 
  Quaternion[w1*w2 - x1*x2 - y1*y2 - z1*z2, 
   w1*x2 + x1*w2 + y1*z2 - z1*y2, w1*y2 - x1*z2 + y1*w2 + z1*x2, 
   w1*z2 + x1*y2 - y1*x2 + z1*w2];

(*Define quaternion addition as the componentwise sum of the two \
quaternions using/:*)
Quaternion /: 
  Plus[Quaternion[w1_, x1_, y1_, z1_], 
   Quaternion[w2_, x2_, y2_, z2_]] := 
  Quaternion[w1 + w2, x1 + x2, y1 + y2, z1 + z2];

(*Define quaternion subtraction as the componentwise difference of \
the two quaternions using/:*)
Quaternion /: 
  Minus[Quaternion[w1_, x1_, y1_, z1_], 
   Quaternion[w2_, x2_, y2_, z2_]] := 
  Quaternion[w1 - w2, x1 - x2, y1 - y2, z1 - z2];

Исходная версия Nxx, :

Попросил реализовать для системы «Mathematica» код, реализующий кватернионы, без использования пакета Quaternions. Вот результат (всё, включая комментарии, написано ИИ):

Quaternion /: Conjugate[Quaternion[w_, x_, y_, z_]] := Quaternion[w, -x, -y, -z];

(Define the norm of a quaternion as the square root of the sum of
the squares of its components using/:
) Quaternion /: Norm[Quaternion[w_, x_, y_, z_]] := Sqrt[w^2 + x^2 + y^2 + z^2];

(Define the inverse of a quaternion as the conjugate divided by the
norm squared using/:
) Quaternion /: Inverse[Quaternion[q_]] := Conjugate[Quaternion[q]]/Norm[Quaternion[q]]^2; 2;

(Define quaternion multiplication using the formula
(w1,x1,y1,z1)
(w2,x2,y2,z2)=(w1w2-x1x2-y1y2-z1z2,w1x2+x1w2+y1*
z2-z1y2,w1y2-x1z2+y1w2+z1x2,w1z2+x1y2-y1x2+z1w2)) ClearAll[QuaternionProduct] QuaternionProduct[Quaternion[w1_, x1_, y1_, z1_], Quaternion[w2_, x2_, y2_, z2_]] := Quaternion[w1w2 - x1x2 - y1y2 - z1z2, w1x2 + x1w2 + y1z2 - z1y2, w1y2 - x1z2 + y1w2 + z1x2, w1z2 + x1y2 - y1x2 + z1w2];

(Define quaternion addition as the componentwise sum of the two
quaternions using/:
) Quaternion /: Plus[Quaternion[w1_, x1_, y1_, z1_], Quaternion[w2_, x2_, y2_, z2_]] := Quaternion[w1 + w2, x1 + x2, y1 + y2, z1 + z2];

(Define quaternion subtraction as the componentwise difference of
the two quaternions using/:
) Quaternion /: Minus[Quaternion[w1_, x1_, y1_, z1_], Quaternion[w2_, x2_, y2_, z2_]] := Quaternion[w1 - w2, x1 - x2, y1 - y2, z1 - z2];