mathematica中有像matlab中定义细胞,每个元都是矩阵的方法吗?
mathematica吧
全部回复
仅看楼主
level 5
2018年05月03日 11点05分 1
level 5
内容是:n-1个(n+1)的矩阵相乘,然后相乘,matlab我写出来了,大家看一下
2018年05月03日 11点05分 2
level 5
format long e
B=cell(n-1,1);
T=eye(n+1);
for k=1:n-1
B{k}=eye(n+1);
end
for t=1:n-1
for j=1:n-t
B{t}(j+1,j)=1-q^t;
end
end
for t=1:n-1
T=T*B{t};
end
2018年05月03日 11点05分 3
level 5
就是这样的相乘矩阵整体转置后相乘
2018年05月03日 11点05分 4
level 5
2018年05月04日 01点05分 5
你给我发过私信吗?抱歉我回复不了私信。
2018年05月04日 06点05分
我对 MATLAB 的 cell 并不了解。
2018年05月04日 06点05分
@Alexander0620 matlab不用管,就是这样的系列矩阵相乘在mathematica中怎么实现。主要就是指标问题,然后赋值
2018年05月04日 06点05分
@浩对等沼焊 Dot@@{mat1, mat2, ..., matn}
2018年05月04日 06点05分
level 5
2018年05月04日 01点05分 7
level 10
f[x_, y_] :=
Block[{a = x, n = y},
Normal@SparseArray[{Band[{1, 1}] -> 1,
Band[{1, 2}, {a, a + 1}] -> r^a - q^(n - a)}, {n + 1, n + 1}]]
g[n_] := Dot @@ Table[f[i, n], {i, 1, 4}] // MatrixForm
2018年05月04日 01点05分 8
十分感谢。试了三天一直用table,ayyay,都没解决,我还有点问题,第一,我在前面加入了输入r,q,n,不能执行,输入,r,q,再输入g[3]就能执行,最后我要的是结果乘积,用transpose 没有效果,第二,如果是一般的矩阵,怎样解决系列矩阵指标问题,再对每一个矩阵赋值呢
2018年05月04日 03点05分
@浩对等沼焊 我有急事要做,你等等其他吧友帮你解答一下。
2018年05月04日 05点05分
@奥斯马登 好的,您先忙,有空了再给看下[乖]
2018年05月04日 06点05分
level 12
prod[n_, r_, q_] := Module[{diag1st, diag2nd},
diag1st = SparseArray[{i_, i_} -> 1, n + 1];
diag2nd[idx_] :=
SparseArray[
Band[{1, 2}] -> Table[r^(idx - i) - q^(n - idx), {i, idx, 1, -1}],
n + 1];
Dot @@ Table[diag1st + diag2nd[i], {i, 1, n - 1, 1}]
]
2018年05月04日 09点05分 9
不知道有没有理解到位。如果有一组明确的 n, r, q 值,能对比一下结果就好了。
2018年05月04日 09点05分
回复 Alexander0620 :我可以在matlab中,实现。r=1,q=0.5,n=6,时结果放楼下你看下,还有就是这个的精度能够控制16位吗
2018年05月04日 09点05分
level 12
prod[n_, r_, q_] := Module[{mat},
mat[idx_] :=
SparseArray[{Band[{1, 1}] -> 1,
Band[{1, 2}] ->
Table[r^(idx - i) - q^(n - idx), {i, idx, 1, -1}]}, n + 1];
Array[mat, n - 1, 1, Dot]
]
2018年05月04日 09点05分 10
自认为更优化的版本 1. 只用一个 SparseArray 2. 利用 Array 的最后一个语法,不用再使用 Apply (@@)
2018年05月04日 09点05分
level 5
回复 Alexander0620 :实际上是n-1个n+1维矩阵相乘,我写个例子你看下
2018年05月04日 09点05分 11
level 12
2018年05月04日 09点05分 12
就是这样除了精度还不够
2018年05月04日 09点05分
@浩对等沼焊 看楼下如何
2018年05月04日 10点05分
level 5
就是这样的矩阵乘起来,后来我需要的是乘积转置和精度16位以上
2018年05月04日 09点05分 13
“乘积转置”什么意思 精度先不管,先看算法是否实现
2018年05月04日 09点05分
上一楼就是 r=1, q=0.5, n=6 的结果
2018年05月04日 09点05分
@Alexander0620 就是这样,精度怎么控制?这个矩阵如何加转置?
2018年05月04日 09点05分
@Alexander0620 老哥在吗[小乖]
2018年05月06日 10点05分
level 12
prod[n_, r_, q_] :=
Block[{mat, $MaxPrecision = 16, $MinPrecision = 16},
mat[idx_] :=
SparseArray[{Band[{1, 1}] -> 1,
Band[{1, 2}] ->
Table[r^(idx - i) - q^(n - idx), {i, idx, 1, -1}]}, n + 1];
Array[mat, n - 1, 1, Dot]
]
2018年05月04日 09点05分 14
转置怎么输出呢?如果是一般的矩阵,怎样解决系列矩阵指标问题,再对每一个矩阵赋值呢,我的实验怎么不能输出16位呢
2018年05月04日 10点05分
@浩对等沼焊 要对哪个/些矩阵转置?
2018年05月04日 11点05分
@Alexander0620 就是现在写出来的矩阵,另外问什么我实验不是16位呢,我把偏好设置数字调30,还是不行
2018年05月04日 12点05分
@浩对等沼焊 Transpose
2018年05月04日 12点05分
level 5
2018年05月04日 10点05分 15
level 5
Input["n=", n];
n = %
Input["q=", q];
q = %
B = IdentityMatrix[n + 1]
X = Array[x, n + 1];
X[[1]] = 35/1000
For[j = 2, j <= n + 1, j++, X[[j]] = X[[1]] + j*10^-5]
f[r_, q_] := If[q == 1, r, (1 - q^r)/(1 - q)]
h[r_, q_] := Product[f[i, q], {i, 1, r}]
g[r_, q_] := If[r == 0, 1, h[r, q]]
k[n_, i_, q_] :=
If[q == 1, Binomial[n, i], h[n, q]/(h[i, q] h[n - i, q])];
z[q_, x_, n_, i_] :=
k[n, i, q]*x^i*Product[(1 - q^s*x), {s, 0, n - i - 1}]
For[j = 1, j <= n + 1, j++, B[[1]][[j]] = -z[q, X[[2]], n, j - 1]]
For[i = 2, i <= n + 1, i++,
For[j = 1, j <= n + 1, j++,
B[[i]][[j]] =
z[q, X[[i]], n,
j - 1] - (z[q, X[[2]], n, j - 1] +
z[q, X[[1]], n, j - 1])*(z[q, X[[i]], n, 0]/
z[q, X[[2]], n, 0])]]
NumberForm[MatrixForm[B, 16], 16]
%此处位第一个矩阵
Transpose[
prod[n_, r_, q_] :=
Block[{mat, $MaxPrecision = 16, $MinPrecision = 16},
mat[idx_] :=
SparseArray[{Band[{1, 1}] -> 1,
Band[{1, 2}] ->
Table[r^(idx - i) - q^(n - idx), {i, idx, 1, -1}]}, n + 1];
Array[mat, n - 1, 1, Dot]]]
%此处位第二个矩阵
Input["n=", n];
n = %
M = IdentityMatrix[n + 1]
For[i = 1, i <= n + 1, i++, M[[i]][[i]] = Binomial[n, i - 1]]
NumberForm[MatrixForm[M, 16], 16]
此处为第三个矩阵
Input["n=", n];
n = %
Input["q=", q];
q = %
Dq = IdentityMatrix[n + 1]
f[r_, q_] := If[q == 1, r, (1 - q^r)/(1 - q)]
h[r_, q_] := Product[f[i, q], {i, 1, r}]
g[r_, q_] := If[r == 0, 1, h[r, q]]
k[n_, i_, q_] :=
If[q == 1, Binomial[n, i], h[n, q]/(h[i, q] h[n - i, q])];
For[i = 1, i <= n + 1, i++, Dq[[i]][[i]] = k[n, i, q]] NumberForm[
MatrixForm[Dq, 16], 16]
%此处为第四个矩阵
2018年05月05日 06点05分 23
请附上Input的典型值。(话说回来你为什么非要在示例代码里塞Input不可?)
2018年05月05日 06点05分
还有,Mathematica里的注释是(**)
2018年05月05日 06点05分
最后,“第二个矩阵”那段代码根本就是错的。
2018年05月05日 06点05分
@xzcyr 多谢指教,注释是我百度贴的时候才打的,第二段我在层主基础加了转置,。现在计算这样4个矩阵乘积,我这些如何修改呢,能贴出来看下吗
2018年05月05日 06点05分
1 2 尾页