【问题标题】:How to get partial expression from symbolic expression containing specific variable MAPLE?如何从包含特定变量 MAPLE 的符号表达式中获取部分表达式?
【发布时间】:2019-02-21 10:25:32
【问题描述】:

我有一个符号表达式如下

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


    【解决方案1】:
    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
    

    【讨论】:

    • 太棒了。它是如此紧凑的代码并且完美地工作。我很高兴也很感激:)。我是枫树的新手,正在做我的基础知识。我真的不明白那行代码到底是怎么做的。如果可能的话,请为我提供上述代码的一些材料的相关链接
    • 我在原始答案中添加了简洁代码的细分。
    【解决方案2】:
    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函数的行为会有所不同

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-27
      • 1970-01-01
      • 2021-04-03
      • 2011-01-18
      相关资源
      最近更新 更多