如何从包含特定变量MAPLE的符号表达式中获得部分表达式?

问题描述 投票:2回答:2

我有一个符号表达式如下

y1 = (1/a)-(b/a^2)+x*a*b-x/b
y2 = a*b+a*x+b*sqrt(x)

现在我需要得到具有特定术语的部分表达式。喜欢

xFunction(y1, x) # should return x*a*b-x/b
xFunction(y2,x)  # should return a*x+b*sqrt(x)

任何建议或想法都非常有帮助谢谢

symbolic-math maple
2个回答
3
投票
restart;

y1 := (1/a)-(b/a^2)+x*a*b-x/b:
y2 := a*b+a*x+b*sqrt(x):

K := (ee,x) -> `if`(ee::`+`,select(depends,ee,x),ee):

K( y1, x );

                     x
             x a b - -
                     b

K( y2, x );

                     (1/2)
            a x + b x     

#
# Leave alone an expression which is not a sum of terms.
#
K( sin(x+4)*x^3, x );

                         3
             sin(x + 4) x 

#
# Don't select subterms in which `x` is a just dummy name.
#
K( x^3 + sin(x) + Int(sqrt(x), x=a..b), x );

               3         
              x  + sin(x)

[编辑]

y1 := (1/a)-(b/a^2)+x*a*b-x/b;

                      1   b            x
                y1 := - - -- + x a b - -
                      a    2           b
                          a             

op(3,y1);

                         x a b

depends(op(3,y1), x);

                          true

select命令将其第一个参数映射到其第二个参数的所有操作数。

select( s->depends(s,x), y1 );

                               x
                       x a b - -
                               b

一种更简洁的语法,其中select将其第一个参数depends映射到其第二个参数的操作数上,并将其第三个参数作为附加选项传递给选择器。

select( depends, y1, x );

                               x
                       x a b - -
                               b

现在创建一个程序来完成它。使用条件测试,以便在不是术语总和时返回第一个参数。

K1 := proc(ee, x)
  if type(ee,`+`) then
    select( depends, ee, x );
  else
    # leave it alone
    ee;
  end if;
end proc:

K1( y1, x);

                               x
                       x a b - -
                               b

使用更简洁的语法进行该类型检查。

K2 := proc(ee, x)
  if ee::`+` then
    select( depends, ee, x );
  else
    # leave it alone
    ee;
  end if;
end proc:

K2( y1, x);

                               x
                       x a b - -
                               b

对if..then..end if使用更简洁的语法。这就是所谓的if运营商形式。单词if在名称引号内,以区别于if ... then ... end if中的语言关键字。

K3 := proc(ee, x)
  `if`( ee::`+` , select( depends, ee, x ), x );
end proc:

K3( y1, x);

                               x
                       x a b - -
                               b

由于过程K3的主体只有一个语句,因此我们可以使用所谓的运算符形式使其更简洁。

K4 := (ee, x) -> `if`( ee::`+` , select( depends, ee, x ), x ):

K4( y1, x);

                               x
                       x a b - -
                               b

1
投票
listOfTerms = op(expression);  # y1 or y2
numberOfSubExpressions=nops(expression); # for y1 or y2

requiredTerm = 0;

for i 1 to numberOfSubExpressions do 
    if has(listOfTerms [i], x) then # x is our required term
       requiredTerm := requiredTerm +listOfTerms [i] 
    end if 
end do

以上代码符合我的要求。但是,如果有任何特殊表达的错误,请告诉我。因为当我们有像(sin,cos Log ..etc)这样的函数时,op函数的行为会有所不同

© www.soinside.com 2019 - 2024. All rights reserved.