executable strings

14 views
Skip to first unread message

Irena Swanson

unread,
Jul 4, 2026, 10:48:18 PM (2 days ago) Jul 4
to Macaulay2
I would like functions to define new functions.  Here is a very simple example:

f = (n) -> (
 cmd = "newFunction = (x) -> (x^" | n | ")";
 print cmd;
)

When I run
f 3
the screen prints out
newFunction = (x) -> (x^3)

I then have to copy and paste this output line into M2 for it to become a cubing function.
Is there a way of making the cmd executable without my having to copy and paste?

Thank you,
Irena

Mahrud Sayrafi

unread,
Jul 5, 2026, 12:42:30 AM (yesterday) Jul 5
to maca...@googlegroups.com
Yes, you can create a function and return it directly. In your example, it could be just one line:
f = n -> x -> x^n

For instance:
i1 : f = n -> x -> x^n;

i2 : g = f(3);

i3 : g(5)

o3 = 125

More generally, it could look like:

functionGenerator = n -> (
    A := ...;
    fun := x -> (
        B := ...;
        something(A,B,x));
    ...;
    fun)

Mahrud

--
You received this message because you are subscribed to the Google Groups "Macaulay2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to macaulay2+...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/macaulay2/65c1e875-de8c-41a5-8c2e-ea96f64e413en%40googlegroups.com.

Irena Swanson

unread,
Jul 5, 2026, 6:09:03 PM (24 hours ago) Jul 5
to maca...@googlegroups.com
Thank you, Mahrud! It worked (but for some reason it didn't work quite right on my first tries on the non-simple function; restart perhaps did the fix).

Many thanks!
Irena


Irena Swanson

unread,
Jul 5, 2026, 6:31:01 PM (23 hours ago) Jul 5
to maca...@googlegroups.com
Well, I would like to know the code of my function (named "closedForm"), and I get the ugly output below.  Is it possible to simply spell out the C_i, C_j on the third line without the extra things that were defined inside my function that defined the new function closedForm?

i15 : code (closedForm)
o15 = myFileName:151:15-153:97: --source code:
        closedForm = (n) -> (
              R := QQ[n];
              return sum apply (k+1, i -> C_i*n^i) + sum apply(l, i -> (j = i + k+1; C_j*n^i*(-1)^n)););
      | symbol  class   value                                                location of symbol
      | ------  -----   -----                                                ------------------
      | A       Matrix  | 1 0 0  0   0    0     0      |                     myFileName:140:2-140:3  
      |                 | 1 1 1  1   1    1     1      |                    
      |                 | 1 2 4  8   16   32    64     |                    
      |                 ................................                    
      | B       Matrix  | 1  0  |                                            myFileName:141:2-141:3  
      |                 | -1 -1 |                                            
      |                 | 1  2  |                          
      |                 .........                                            
      |                     5      19   17    5   31    1     1    5    1    
      | C       List    {- ---, - ---, ----, --, ----, ---, ----, ---, ---}  myFileName:148:2-148:3  
      |                    256    384  1440  96  1152  192  2880  256  128  
      | L       List    {2, 4}                                               myFileName:138:19-138:20
      | k       ZZ      6                                                    myFileName:138:17-138:18
      | o       ZZ      1                                                    myFileName:138:21-138:22
      | AM      Matrix  | 1 0 0  0   0    0     0      1  0  0   |           myFileName:146:2-146:4  
      |                 | 1 1 1  1   1    1     1      -1 -1 0   |          
      |                 | 1 2 4  8   16   32    64     1  2  1   |
      |                 ..........................................
      | BM      Matrix  | 1 0 0 0 0 0 0 0 0 -5/256  |                        myFileName:147:2-147:4  
      |                 | 0 1 0 0 0 0 0 0 0 -19/384 |                        
      |                 | 0 0 1 0 0 0 0 0 0 17/1440 |
      |                 .............................  

On Sat, Jul 4, 2026 at 10:42 PM 'Mahrud Sayrafi' via Macaulay2 <maca...@googlegroups.com> wrote:

Mahrud Sayrafi

unread,
12:23 AM (17 hours ago) 12:23 AM
to maca...@googlegroups.com
Probably, but it requires a bit of extra work. You can trace through what code(Function) does here. For instance `code locate closedForm` will print the code without the table below, and I think something like (first localDictionaries closedForm)#"C" might show you what you're looking for.

Mahrud

Irena Swanson

unread,
12:56 PM (5 hours ago) 12:56 PM
to maca...@googlegroups.com
Wow, so much I do not know.  Below are some outputs.

But perhaps I am set for what I need: I no longer have to copy and paste to activate a function, thanks to you, Mahrud.  I really appreciate it.
And I know how to coax M2 to print out the desired visual function with printing a string similar to as before.

Thank you!
Irena

i16 : code locate closedForm
o16 = myFileName:151:15-153:97: --source code:

        closedForm = (n) -> (
              R := QQ[n];
              return sum apply (k+1, i -> C_i*n^i) + sum apply(l, i -> (j = i + k+1; C_j*n^i*(-1)^n)););

(It would be good to see the numbers C_i.)

i17 : (first localDictionaries closedForm)#"C"
o17 = C
o17 : Symbol

I then tried a direct call as in the linked-to code.m2, to possibly modify to different levels, but even the basic one did not work.

i18 : codeFunction(null,closedForm,0)
stdio:21:12:(3):[1]: error: no method for adjacent objects:
            codeFunction (of class Symbol)
    SPACE   (, closedForm, 0) (of class Sequence)

I then tried

i30 : loadPackage "code"
stdio:31:11:(3):[1]: error: file not found on path: "code.m2"

Mahrud Sayrafi

unread,
5:36 PM (10 minutes ago) 5:36 PM
to maca...@googlegroups.com
Ah sorry code.m2 is not a package, but an internal part of Macaulay2's "Core".
To get value of C, try this instead:
value (first localDictionaries closedForm)#"C"
I forgot the "value" the first time.

Best,
Mahrud

Reply all
Reply to author
Forward
0 new messages