Scilab 符号矩阵乘法
Scilab symbolic matrix multiplication
我有 3 个矩阵:
T_01 = ['cosd*t1', '-sind*t1', '0', 'd1*cosd*t1'; 'sind*t1', 'cosd*t1', '0', 'd1*sind*t1'; '0', '1', '1', '0'; '0', '0', '0', '1']
T_12 = ['cosd*t2', '-sind*t2', '0', 'd2*cosd*t2'; 'sind*t2', 'cosd*t2', '0', 'd2*sind*t2'; '0', '1', '1', '0'; '0', '0', '0', '1']
T_23 = ['cosd*t3', '-sind*t3', '0', 'd3*cosd*t3'; 'sind*t3', 'cosd*t3', '0', 'd3*sind*t3'; '0', '1', '1', '0'; '0', '0', '0', '1']
我需要做一个符号乘法,所以我正在尝试:
mulf(T_01,T_12,T_23)
但是我得到这个错误:
!--error 39
mulf: Quantidade incorreta de argumentos de entrada: esperava-se 2.
发生了什么事?
Obs.:对不起我的英语。
如果你想要得到两个矩阵的符号乘法,你将不得不实现这样的功能。在这里,我实现了三个功能,它们可以一起执行您想要的操作:
function s = scaProd(a,b)
//escalar product of two vectors
//using recursion
if (a == [] | b == []) then
s = ""
elseif (max(size(a)) ~= max(size(b))) | ...
(min(size(a)) ~= min(size(b))) | ...
(min(size(a)) ~= 1) then
error("vectorMulf: Wrong dimensions")
else
s = addf( mulf(a(1), b(1)) , scaProd(a(2:$), b(2:$)) )
end
endfunction
function s = matrixMulf(a,b)
//matrix multiplication
acols = size(a,'c');
brows = size(b,'r');
if acols ~= brows then
error("matrixMulf: Wrong dimensions")
end
arows = size(a,'r');
bcols = size(b,'c');
s = string(zeros(arows,bcols));
for i = 1 : arows
for j = 1 : bcols
s(i,j) = scaProd(a(i,:),b(:,j)');
end
end
endfunction
function s = addP(a)
//encolses each element of a in a pair of parenthesis
s = string(zeros(a));
for i = 1 : size(a,'r')
for j = 1 : size(a,'c')
s(i,j) = "(" + a(i,j) + ")"
end
end
endfunction
这是它的输出示例。测试代码:
A = [1 2; 3 4];
B = [5 6; 7 8];
C = [9 0; 1 2];
disp(A*B*C)
As = string(A);
Bs = string(B);
Cs = string(C);
disp(matrixMulf(As,addP(matrixMulf(Bs, Cs))))
控制台输出:
193. 44.
437. 100.
!1*(5*9+6*1)+2*(7*9+8*1) 1*(5*0+6*2)+2*(7*0+8*2) !
! !
!3*(5*9+6*1)+4*(7*9+8*1) 3*(5*0+6*2)+4*(7*0+8*2) !
为了你想要的结果,你应该这样做:
- 使用
addP()
将每个矩阵的每一项用括号括起来
- 执行符号乘法,如
matrixMulf(t1,addP(matrixMulf(t2,t3)))
,其中 t1
、t2
、t3
是矩阵的封闭版本。
最后两个注意事项:
- 重要的是在每个乘法步骤使用
addP
以获得正确的结果。您可以通过删除我给出的示例中的 (
和 )
来检查:结果将不正确。
- 函数
mulf
和 addf
在 Scilab 6.0.0 上不可用。所以请记住,如果您将 Scilab 升级到当前的稳定版本,您将无法使用它们。
我有 3 个矩阵:
T_01 = ['cosd*t1', '-sind*t1', '0', 'd1*cosd*t1'; 'sind*t1', 'cosd*t1', '0', 'd1*sind*t1'; '0', '1', '1', '0'; '0', '0', '0', '1']
T_12 = ['cosd*t2', '-sind*t2', '0', 'd2*cosd*t2'; 'sind*t2', 'cosd*t2', '0', 'd2*sind*t2'; '0', '1', '1', '0'; '0', '0', '0', '1']
T_23 = ['cosd*t3', '-sind*t3', '0', 'd3*cosd*t3'; 'sind*t3', 'cosd*t3', '0', 'd3*sind*t3'; '0', '1', '1', '0'; '0', '0', '0', '1']
我需要做一个符号乘法,所以我正在尝试:
mulf(T_01,T_12,T_23)
但是我得到这个错误:
!--error 39
mulf: Quantidade incorreta de argumentos de entrada: esperava-se 2.
发生了什么事?
Obs.:对不起我的英语。
如果你想要得到两个矩阵的符号乘法,你将不得不实现这样的功能。在这里,我实现了三个功能,它们可以一起执行您想要的操作:
function s = scaProd(a,b)
//escalar product of two vectors
//using recursion
if (a == [] | b == []) then
s = ""
elseif (max(size(a)) ~= max(size(b))) | ...
(min(size(a)) ~= min(size(b))) | ...
(min(size(a)) ~= 1) then
error("vectorMulf: Wrong dimensions")
else
s = addf( mulf(a(1), b(1)) , scaProd(a(2:$), b(2:$)) )
end
endfunction
function s = matrixMulf(a,b)
//matrix multiplication
acols = size(a,'c');
brows = size(b,'r');
if acols ~= brows then
error("matrixMulf: Wrong dimensions")
end
arows = size(a,'r');
bcols = size(b,'c');
s = string(zeros(arows,bcols));
for i = 1 : arows
for j = 1 : bcols
s(i,j) = scaProd(a(i,:),b(:,j)');
end
end
endfunction
function s = addP(a)
//encolses each element of a in a pair of parenthesis
s = string(zeros(a));
for i = 1 : size(a,'r')
for j = 1 : size(a,'c')
s(i,j) = "(" + a(i,j) + ")"
end
end
endfunction
这是它的输出示例。测试代码:
A = [1 2; 3 4];
B = [5 6; 7 8];
C = [9 0; 1 2];
disp(A*B*C)
As = string(A);
Bs = string(B);
Cs = string(C);
disp(matrixMulf(As,addP(matrixMulf(Bs, Cs))))
控制台输出:
193. 44.
437. 100.
!1*(5*9+6*1)+2*(7*9+8*1) 1*(5*0+6*2)+2*(7*0+8*2) !
! !
!3*(5*9+6*1)+4*(7*9+8*1) 3*(5*0+6*2)+4*(7*0+8*2) !
为了你想要的结果,你应该这样做:
- 使用
addP()
将每个矩阵的每一项用括号括起来
- 执行符号乘法,如
matrixMulf(t1,addP(matrixMulf(t2,t3)))
,其中t1
、t2
、t3
是矩阵的封闭版本。
最后两个注意事项:
- 重要的是在每个乘法步骤使用
addP
以获得正确的结果。您可以通过删除我给出的示例中的(
和)
来检查:结果将不正确。 - 函数
mulf
和addf
在 Scilab 6.0.0 上不可用。所以请记住,如果您将 Scilab 升级到当前的稳定版本,您将无法使用它们。