以更大的算法方式替换最大符号变量

时间:2018-08-15 02:21:16

标签: algorithm substitution maxima

expJ:listarray(J);

(expJ)  ["-(l[1]*l[3]*m[3]*('diff(r[3](t),t,1))^2*sin(r[3](t)-r[1](t))+(2*l[1]*l[2]*m[3]+l[1]*l[2]*m[2])*('diff(r[2](t),t,1))^2*sin(r[2](t)-r[1](t))-l[1]*m[1]*g*cos(r[1](t)))/2","-(l[2]*l[3]*m[3]*('diff(r[3](t),t,1))^2*sin(r[3](t)-r[2](t))+((-2*l[1]*l[2]*m[3])-l[1]*l[2]*m[2])*('diff(r[1](t),t,1))^2*sin(r[2](t)-r[1](t))-2*l[2]*m[2]*g*cos(r[2](t)))/2","(l[2]*l[3]*m[3]*('diff(r[2](t),t,1))^2*sin(r[3](t)-r[2](t))+l[1]*l[3]*m[3]*('diff(r[1](t),t,1))^2*sin(r[3](t)-r[1](t))+3*l[3]*m[3]*g*cos(r[3](t)))/2"]
for i:1 thru 3 do(
    for k:1 thru 3 do(
J[i,1]:ssubst("m3","m[3]",J[i,1])
));

我想用算法替换m前面的数字,因为它们分别是1,2,3,但是当我放mi时,它会将其识别为不同的变量,因此我需要以某种方式指示ssubs(“ mi”,“ m [ i]“,J [i,1]),因为i与m分开。  有什么建议吗?

1 个答案:

答案 0 :(得分:0)

好的,这是用v(k)代替v[k]的一种方法。我相信这是可以的,因为Matlab可以识别数组下标的括号。

%o5是您在上面提供的输入(作为字符串)。我已经在%o7中解析了字符串,并在%o10中提取了下标变量的列表(通过sublistsubvarp)。从那里,我在%o14中创建了一个列表v(k) = v[k],然后将它们替换回%o15中的已解析表达式中。

我希望这将对您有所帮助。您可能仍需要修改此方法才能获得所需的内容,但是无论如何,我都会重申我对字符串处理的强烈建议。如果还有更多事情要做,那么可以肯定的是,使用表达式比使用字符串来实现更好。

(%o5) [-(l[1]*l[3]*m[3]*('diff(r[3](t),t,1))^2*sin(r[3](t)-r[1](t))+(2*l[1]*l[\
2]*m[3]+l[1]*l[2]*m[2])*('diff(r[2](t),t,1))^2*sin(r[2](t)-r[1](t))-l[1]*m[1]*\
g*cos(r[1](t)))/2, -(l[2]*l[3]*m[3]*('diff(r[3](t),t,1))^2*sin(r[3](t)-r[2](t)\
)+((-2*l[1]*l[2]*m[3])-l[1]*l[2]*m[2])*('diff(r[1](t),t,1))^2*sin(r[2](t)-r[1]\
(t))-2*l[2]*m[2]*g*cos(r[2](t)))/2, (l[2]*l[3]*m[3]*('diff(r[2](t),t,1))^2*sin\
(r[3](t)-r[2](t))+l[1]*l[3]*m[3]*('diff(r[1](t),t,1))^2*sin(r[3](t)-r[1](t))+3\
*l[3]*m[3]*g*cos(r[3](t)))/2]
(%i6) linel:65;
(%o6)                          65
(%i7) map (parse_string, %o5);
                     d          2
(%o7) [((- l  l  m  (-- (r (t)))  sin(r (t) - r (t)))
            1  3  3  dt   3            3       1
                            d          2
 - (2 l  l  m  + l  l  m ) (-- (r (t)))  sin(r (t) - r (t))
       1  2  3    1  2  2   dt   2            2       1
                                        d          2
 + l  m  g cos(r (t)))/2, ((- l  l  m  (-- (r (t)))
    1  1        1              2  3  3  dt   3
                                                    d          2
 sin(r (t) - r (t))) - ((- 2 l  l  m ) - l  l  m ) (-- (r (t)))
      3       2               1  2  3     1  2  2   dt   1
 sin(r (t) - r (t)) + 2 l  m  g cos(r (t)))/2, 
      2       1          2  2        2
           d          2
(l  l  m  (-- (r (t)))  sin(r (t) - r (t))
  2  3  3  dt   2            3       2
             d          2
 + l  l  m  (-- (r (t)))  sin(r (t) - r (t))
    1  3  3  dt   1            3       1
 + 3 l  m  g cos(r (t)))/2]
      3  3        3
(%i8) grind (%);

[((-l[1]*l[3]*m[3]*('diff(r[3](t),t,1))^2*sin(r[3](t)-r[1](t)))
  -(2*l[1]*l[2]*m[3]+l[1]*l[2]*m[2])
   *('diff(r[2](t),t,1))^2*sin(r[2](t)-r[1](t))
  +l[1]*m[1]*g*cos(r[1](t)))
  /2,
 ((-l[2]*l[3]*m[3]*('diff(r[3](t),t,1))^2*sin(r[3](t)-r[2](t)))
  -((-2*l[1]*l[2]*m[3])-l[1]*l[2]*m[2])
   *('diff(r[1](t),t,1))^2*sin(r[2](t)-r[1](t))
  +2*l[2]*m[2]*g*cos(r[2](t)))
  /2,
 (l[2]*l[3]*m[3]*('diff(r[2](t),t,1))^2*sin(r[3](t)-r[2](t))
  +l[1]*l[3]*m[3]*('diff(r[1](t),t,1))^2*sin(r[3](t)-r[1](t))
  +3*l[3]*m[3]*g*cos(r[3](t)))
  /2]$
(%o8)                         done
(%i9) listofvars (%o7);
(%o9)            [l , m , g, t, l , m , m , l ]
                   1   1         2   2   3   3
(%i10) sublist (%, subvarp);
(%o10)              [l , m , l , m , m , l ]
                      1   1   2   2   3   3
(%i11) map (op, %o10);
(%o11)                 [l, m, l, m, m, l]
(%i12) map (args, %o10);
(%o12)           [[1], [1], [2], [2], [3], [3]]
(%i13) map (lambda ([v], apply (op(v), args(v))), %o10);
(%o13)        [l(1), m(1), l(2), m(2), m(3), l(3)]
(%i14) map (lambda ([v1, v2], v1=v2), %o10, %o13);
(%o14) [l  = l(1), m  = m(1), l  = l(2), m  = m(2), m  = m(3), 
         1          1          2          2          3
                                                       l  = l(3)]
                                                        3
(%i15) subst (%, %o7);
                            d          2
(%o15) [((- l(1) l(3) m(3) (-- (r (t)))  sin(r (t) - r (t)))
                            dt   3            3       1
                                        d          2
 - (2 l(1) l(2) m(3) + l(1) l(2) m(2)) (-- (r (t)))
                                        dt   2
 sin(r (t) - r (t)) + l(1) m(1) g cos(r (t)))/2, 
      2       1                        1
                    d          2
((- l(2) l(3) m(3) (-- (r (t)))  sin(r (t) - r (t)))
                    dt   3            3       2
                                            d          2
 - ((- 2 l(1) l(2) m(3)) - l(1) l(2) m(2)) (-- (r (t)))
                                            dt   1
 sin(r (t) - r (t)) + 2 l(2) m(2) g cos(r (t)))/2, 
      2       1                          2
                 d          2
(l(2) l(3) m(3) (-- (r (t)))  sin(r (t) - r (t))
                 dt   2            3       2
                   d          2
 + l(1) l(3) m(3) (-- (r (t)))  sin(r (t) - r (t))
                   dt   1            3       1
 + 3 l(3) m(3) g cos(r (t)))/2]
                      3
(%i16) grind (%);

[((-l(1)*l(3)*m(3)*('diff(r[3](t),t,1))^2*sin(r[3](t)-r[1](t)))
  -(2*l(1)*l(2)*m(3)+l(1)*l(2)*m(2))
   *('diff(r[2](t),t,1))^2*sin(r[2](t)-r[1](t))
  +l(1)*m(1)*g*cos(r[1](t)))
  /2,
 ((-l(2)*l(3)*m(3)*('diff(r[3](t),t,1))^2*sin(r[3](t)-r[2](t)))
  -((-2*l(1)*l(2)*m(3))-l(1)*l(2)*m(2))
   *('diff(r[1](t),t,1))^2*sin(r[2](t)-r[1](t))
  +2*l(2)*m(2)*g*cos(r[2](t)))
  /2,
 (l(2)*l(3)*m(3)*('diff(r[2](t),t,1))^2*sin(r[3](t)-r[2](t))
  +l(1)*l(3)*m(3)*('diff(r[1](t),t,1))^2*sin(r[3](t)-r[1](t))
  +3*l(3)*m(3)*g*cos(r[3](t)))
  /2]$
(%o16)                        done
(%i17) listofvars (%o15);
(%o17)                       [g, t]