Welcome to Cadabra Q&A, where you can ask questions and receive answers from other members of the community.
+2 votes

I wanted to define a general function of many variables which can be used as substitution rule at many points. Here is an example:

rule:=f(???,??) = -???-??;

which gives $a+b$ and is correct. But, if I do

rule:=g(?,??,???) = -?-??-???;

it gives $-a+b+c$ instead of $a+b+c$. The problem seems to be with using single ?. This disappears when I use ???? or ?????. For example

rule:=h(????,??,???) = -????-??-???;

which gives correct result $a+b+c$.

Is there a general way to define such functions?


in General questions by (560 points)

1 Answer

+3 votes
Best answer

This triggers an interesting bug in the pattern matcher, thanks for pointing this out. In general, you are supposed to write this as

rule:= g(x??, y??, z??) = - x?? - y?? - z??;
ex:= g(-a,-b,-c);
substitute(ex, rule);

which gives $a+b+c$ as expected. This uses so-called 'object patterns', which are indicated by a name followed by two question marks. You then repeat that name and the question marks on the right-hand side of the substitution rule.

Now in your particular case things should have worked also with

rule:= g(x?, y?, z?) = -x? -y? -z?;    # triggers a bug

because a 'symbol pattern' like x? will match a (and so on). This somehow slipped the net (probably no-one wrote it in this form so far). Will put it on the todo list for the next release (in the meantime, you should be fine with the solution given earlier).

by (71.6k points)
selected by

Forgot to explain: the reason why it worked for you when you avoided single question marks is that

rule:= h(????,??,???) = -????-??-???;

has three object wildcards, one which has name '??', one which has an empty name, and one which has name '?'. This is allowed (as you can see, it works) but I would recommend using proper names for wildcards, not series of question marks.

Thanks a lot.