Search code examples
maxima

maxima - matchdeclare won't accept first argument to be a variable


Consider the following statements:

(%i1) matchdeclare([a,b], constantp);
(%o1)                                done
(%i2) defmatch(match, a*x+b);
(%o2)                                match
(%i3) match(2*x+3);
(%o3)                           [b = 3, a = 2]

I want to generalise this pattern in a function. But then it doesn't seem to work anymore:

(%i1) matchForm(test, form, constants) := block(
    matchdeclare(constants, constantp),
    defmatch(match, form),
    match(test)
);
(%o1) matchForm(test, form, constants) :=
  block(matchdeclare(constants, constantp), defmatch(match, form), match(test))
(%i2) test: 2*x+3;
(%o2)                               2 x + 3
(%i3) form: a*x+b;
(%o3)                               a x + b
(%i4) constants: [a,b];
(%o4)                               [a, b]
(%i5) matchForm(test, form, constants);
defmatch: evaluation of atomic pattern yields: a x + b
(%o5)                                false

Debugging a bit, a bit it seems like the issue is that matchdeclare doesn't accept the argument to be a variable. Is ther any way to make a function like I try to make in maxima?


Solution

  • I haven't tried this, but: perhaps you can get the effect you want via

    apply (matchdeclare, [constants, 'constantp]),
    

    which will evaluate constants before calling matchdeclare.