Question: select and structured types. How to make select start looking at whole expression before looking at its operands?

Ok, I think I am starting to get the hang of this. So in Maple, structured types is like a pattern in Mathematica. To find some subexpression one needs to first define a structured type which match that subexpression, and then use  select(type,.....).  

This works well so far (but it is not as easy as setting up a pattern).

But one small problem is that select() starts looking at the operands of the expression to look for a match.

So if the whole expression itself matches the structured type, it will not detect the whole expression, since select goes over each operand, missing that the whole thing actually matches the type.

May be an example helps shows what I mean. I want to find if an expression has cos(anything)*sin(anything) so I made a structured type for this 

mytype_3 :=  ''`*`'( {'specfunc'(cos),'specfunc'(sin)})';

btw, I do not know if I should use `&*` or '`*`' but that is a side point, I'll try to find this out.   

Now I want to use the above to check if expression has the same "structured type", or "pattern". The expression is expr:=cos(x)*sin(x); clearly it matches it. But since select looks at the operands, it will only see cos(x) by itself, and then sin(x) by itself and hence not find the structured type. 

restart;
mytype_3 :=  ''`*`'( {'specfunc'(cos),'specfunc'(sin)})';
expr:=cos(x)*sin(x);
type(expr,mytype_3);  # true
select(type, expr, mytype_3); # does not work, does not find it.

Since I am doing this in code, and I do not know what the expression is, I think I have to now do the following 

restart;
mytype_3 :=  ''`*`'( {'specfunc'(cos),'specfunc'(sin)})';
expr:=cos(x)*sin(x);
if type(expr,mytype_3) then
   print("The whole expression is a match! nothing to do. no need to use select");
else
   select(type, expr, mytype_3);
fi;

Which is OK. I can do this, But it will be nice if there was a way to have select (or another function like it) starts at the top level before looking at the operands?  

How do others handle such cases? does the above sound like an OK solution to this?


 

Please Wait...