-2

let the Symbolic expression is as below.

y = s + (a/b)*log((a+c)/(b*a)); %# it can be any type of expression

how can I get all possible sub-expressions with two variables and one operator between them.

subExpression1 = b*a;
subExpression2 = a/b;

I got stuck whlie extracting sub-expressions based on operator. if I read one operator I have to study its LHS and RHS and verify that it operates just on one variable but not a other subexpression.

Is there a way to study both LHS and RHS of a operator ??

any comments and suggestions would be very helpful

Rohithsai Sai
  • 468
  • 4
  • 17
  • 2
    How is this different from your [previous question](https://stackoverflow.com/q/52368691/3978545), or even broadly your [question from last week](https://stackoverflow.com/q/52272042/3978545)? It feels like you keep asking similar questions and not adding any context which might help answer the underlying issue. It's not efficient for anyone if you keep asking similar questions... It would also be helpful to limit the issue to a single language, since the syntax, treatment of symbolic expressions and solutions will be very language specific. – Wolfie Sep 18 '18 at 09:54
  • 1
    Earlier question is based on operands but this one is to get sub-expressions based on arithmetic operators. Also, in this question my requirement is get only valid expressions(b*a and a/b in this case). remaining sub/expressions are complex and out of my interest – Rohithsai Sai Sep 18 '18 at 10:06
  • 3
    I think you missed my point, providing context helps answer questions more completely. Rather than "I'm stuck with X because of Y, help me solve Xa, Xb, Xc, ...", you can ask "I'm stuck with X because of Y, here's an example, here's what I expect, here's where I'm stuck" and you can learn more, get a more complete answer. Optional but might be easier. You also didn't address my point about different languages. – Wolfie Sep 18 '18 at 10:17
  • yeah Now I understood what you meant. Thank you I will add few more deatis – Rohithsai Sai Sep 18 '18 at 10:18

2 Answers2

2

Firstly, there is no subexpression a*b in your expression. Inside the call to log in your unparsed input there appears a subterm c/b*a. But in Maple's syntax that gets parsed to something mathematically equivalent to (c*a)/b and not c/(b*a).

But your question contains other ambiguity. Let's consider a few examples:

restart;

expr1 := y = s + (a/b)*log(a+c/b*a);

                                  c a
                         a ln(a + ---)
                                   b
        expr1 := y = s + -------------
                               b

expr2 := y = s + a*log(a+c/b*a)/b;

                                  c a
                         a ln(a + ---)
                                   b
        expr2 := y = s + -------------
                               b

expr2 - expr1;

                    0 = 0

So the expr1 and expr2 are mathematically equivalent. Maple is even holding their structures the same, internally. (You can check that using both the lprint and the dismantle commands.)

So you appear to be asking for a/b to be recognized in either, after parsing the input, even though that term does not appear indentically in the verbatim input (before parsing). That's not wrong, per se, but we'll need to know that it's part of your expectation. If a/b is to be recognized as a candidate subpression of that value, regardless of whether it's entered like expr1 or expr2, then that is a key detail. If that is not your desire then you will really have to justify how they could be distinguished from each other after parsing (since they may parse to the same thing, depending on what's occurred already in the Maple session!).

Also, how do you intend on handling something which is mathematically equivalent to (a*s)/(b)? Do you want code that returns all possible arithmetic pairings, eg. a*s, a/b, s/b? Or do you want just a*s, or just a/b, or just s/b?

Now consider another example:

expr3 := a+c*a/b;

                            c a
               expr3 := a + ---
                             b

normal(expr3);

                   a (b + c)
                   ---------
                       b

Those are mathematically equivalent, though stored differently. Depending on your definition of acceptable "sub-expression" you may or may not want want a/b, or c/b, or b+c, in your results.

I think that you'll probably need to resolve what precisely you want, in at least these three example's ambiguous situations above, before your question could be resolved sensibly.

acer
  • 6,671
  • 15
  • 15
  • Thank you for showing few possible ambiguities. your explanation is very detail. I have edited my question a little. Now Regarding what I require, for example in expr3: a+c*a/b . I need to get all possible sub-expressions(c*a alone in this case) but if my expr is modified using normal(normal(expr)) then I need to get (b+c alone). I have obeserved that even if I enter expr:1 it has beed changed to expr:2 in maple. In that case am iterested to get c*a(most importantly am least interested operators operating on function like log, sin,exp) – Rohithsai Sai Sep 19 '18 at 07:33
0

You can try the semi-documented mtree utility to create a parse tree, which can be used to analyze valid code strings (including symbolics) as well as whole files. Here's how to use it:

tree = mtree('y = s + (a/b)*log((a+c)/(b*a));');

The you can do all sorts of things with it, like dump it to text:

>> tree.dumptree

  1  *<root>:  EXPR:   1/03 
  2     *Arg:  EQUALS:   1/03 
  3        *Left:  ID:   1/01  (y)
  4        *Right:  PLUS:   1/07 
  5           *Left:  CALL:   1/05 
  6              *Left:  ID:   1/05  (s)
  7           *Right:  MUL:   1/14 
  8              *Left:  PARENS:   1/09 
  9                 *Arg:  DIV:   1/11 
 10                    *Left:  CALL:   1/10 
 11                       *Left:  ID:   1/10  (a)
 12                    *Right:  CALL:   1/12 
 13                       *Left:  ID:   1/12  (b)
 14              *Right:  CALL:   1/18 
 15                 *Left:  ID:   1/15  (log)
 16                 *Right:  DIV:   1/24 
 17                    *Left:  PARENS:   1/19 
 18                       *Arg:  PLUS:   1/21 
 19                          *Left:  CALL:   1/20 
 20                             *Left:  ID:   1/20  (a)
 21                          *Right:  CALL:   1/22 
 22                             *Left:  ID:   1/22  (c)
 23                    *Right:  PARENS:   1/25 
 24                       *Arg:  MUL:   1/27 
 25                          *Left:  CALL:   1/26 
 26                             *Left:  ID:   1/26  (b)
 27                          *Right:  CALL:   1/28 
 28                             *Left:  ID:   1/28  (a)

Notice that when you have binary operations (e.g. PLUS or MUL) they are followed by a *Left and a *Right line which contains the operands.

Take a look inside MATLAB\R20###\toolbox\matlab\codetools\@mtree\mtree.m for more information on what can be done with these objects.

Dev-iL
  • 23,742
  • 7
  • 57
  • 99