## #2568 Strange behavior for mat_function

None
closed
nobody
None
5
2013-04-14
2013-04-14
Mate Matica
No

[Maxima 5.28.0-2.]

A:matrix([2,1],[0,3]);
f(x):=x^m;
B:mat_function(f,A);
g(x):=x^5;
C:mat_function(g,A);

The matrix B is correct i.e. [[2^m, 3^m-2^m], [0,3^m]]
but C is wrong, actually the same B is returned!

Now, in a fresh session:

A:matrix([2,1],[0,3]);
f(x):=x^n;
B:mat_function(f,A);
g(x):=x^5;
C:mat_function(g,A);

The only difference is that that m is replaced by n.
Now both B and C are wrong: [[4,5],[0,9]].

## Discussion

• Rupert Swarbrick - 2013-04-14

I've just pushed a fix in the development tree. Basically, the problem was that mat_function took an argument called f and the Maxima language has a rather confusing "feature" that when the contents of mat_function say `f(x)`, it is interpreted as the function f that you bound already. Here's a simple example of what happens:

```f(x) := x;
g(x) := 1+x;

foo(f, x) := f(x);
foo(g, x);
```

The last line gives x, because the f(x) in the third refers to the globally bound f rather than to the argument. The fix I pushed uses the `apply` function (a bit like FUNCALL in Common Lisp). Another option is to use the `local` function.

Anyway, things are fixed in the development tree now, I think. If you want to work around things at your end without compiling the latest version, you can probably just replace your version of diag.mac with the one at https://sourceforge.net/p/maxima/code/ci/master/tree/share/contrib/diag.mac

I made some pretty dramatic edits to that file a month or two ago to fix some other bugs, but I think it will work with Maxima 5.28.0 as well as newer versions.

• Rupert Swarbrick - 2013-04-14
• status: open --> closed

• Rupert Swarbrick - 2013-04-14

Eek. I realised that I'd messed up a couple of variable names in the commit I pointed to this morning. I think it's fixed now, but if you pulled a copy of diag.mac before about 5 minutes ago, you might want to get the updated one.