【发布时间】:2022-01-11 19:57:44
【问题描述】:
我正在编写交换超复数类。
首先:如果已经存在,请告知。我还没有找到。 PS:四元数存在但不可交换。
我试图重现 Matlab 对真实和复杂的所有功能,但使用超复杂。我正在研究的解决方案是定义一个类。如果可以使用更聪明的方法,请告诉。
存在许多具有复数的代码,其想法是巧妙地重载以使所有现有程序都与 Hypercomplex 一起工作。许多用户可能会使用这项工作。
请在文末查看 Ablamowicz1996 中的主要数学原理 _ Clifford Algebras with Numeric and Symbolic Computations
我的 Hypercomplex 类适用于基本操作:我重载了 mtimes(a,b) = a*b 操作。
例如,我可以这样做:
A = MyClass(1);
B = MyClass(2);
A*B; % ->that works
作为一个主要问题,例如在 2D 中,我希望能够使用
[A B C].*[D ; E]
在使用 Matlab 的逐元素行为时,对于任何维度,以及我对 * 的定义。例如,在处理大小兼容但不相同的矩阵时:
A = [1 2 3]; B = [1;2]
Matlab 给出
A.*B
ans =
1 2 3
2 4 6
兼容时也适用于 N 维。
如何重载 times(a,b) = a.*b , power, rdivide.. 以获得 Matlab 的元素行为?
这是我的课
classdef Hypercomplex < handle
properties (SetAccess = public, GetAccess = public)
A
end
properties (SetAccess = private, GetAccess = private)
eps,nu,e1,e2
end
methods
function s = Hypercomplex(Z) %% Conctruc
% Z might be real, complex or hypercomplex
if isa(Z,'Hypercomplex') % is HyperComplex
s = Z;
elseif ~isreal(Z) && length(Z) == 1 % is Complex
s = Hypercomplex([real(Z) imag(Z) 0 0]);
elseif isreal(Z) && length(Z) < 5
s.A = zeros(1,4);
for ii = 1:length(Z) % is vecteur
s.A(ii) = Z(ii);
end
end
end
function s = epsnu(s)
s.eps = (s.A(1) - s.A(4)) + 1i*(s.A(2)+s.A(3));
s.nu = (s.A(1) + s.A(4)) + 1i*(s.A(2)-s.A(3));
end
function s = e1e2(s)
s.e1 = Hypercomplex([0.5 0 0 -0.5]);
s.e2 = Hypercomplex([0.5 0 0 0.5]);
end
%% Overload
function out = cos(a)
a.epsnu.e1e2;
out = cos(a.eps)*a.e1 + cos(a.nu)*a.e2;
% I might also do for sin exp sqrt ...
end
function out = mtimes(a,b) % '*'
a = Hypercomplex(a).epsnu;
b = Hypercomplex(b).epsnu;
eps12 = 0.5*(a.eps * b.eps);% produit complex
nu12 = 0.5*(a.nu * b.nu );% produit complex
h1 = Hypercomplex([real(eps12) imag(eps12) imag(eps12) -real(eps12)]);
h2 = Hypercomplex([real(nu12) imag(nu12) -imag(nu12) real(nu12) ]);
out = h1+h2;
end
function out = mrdivide(a,b)
a = Hypercomplex(a);
b = Hypercomplex(b);
out = a * b.inverse;
end
function s = inverse(s)
s = Hypercomplex(s).epsnu.e1e2;
s = 1/(s.eps)*s.e1 + 1/(s.nu)*s.e2;
end
function out = plus(a,b)
a = Hypercomplex(a);
b = Hypercomplex(b);
out = Hypercomplex(a.A + b.A);
end
function out = minus(a,b)
a = Hypercomplex(a);
b = Hypercomplex(b);
out = Hypercomplex(a.A - b.A);
end
end
end
示例 i^2 = -1 :
>> Ni = Hypercomplex([0 1 0 0]) ;
>> Ni*Ni
Hypercomplex with properties:
A: [-1 0 0 0]
【问题讨论】:
-
您可能需要重载
times以使用bsxfun(隐式扩展在它成为本机功能之前完成的方式)与重载的mtimes -
我试过 bsxfun(@mtimes,A,B) 但 A 和 B 需要是数字数组,而不是来自我定义的类。还是我错了?
-
我认为如果它们属于您定义的类,那么重载会起作用,不是吗?
-
显然,它不起作用,即使在调用内部应用
标签: matlab class oop operator-overloading complex-numbers