Defining a method: expected number of parameter types

16 views
Skip to first unread message

Mike Cummings

unread,
Jul 2, 2024, 3:49:40 PMJul 2
to Macaulay2
I am used to using the following syntax when defining a function (in particular, for use in a package)

foo = method()
foo(RR, RR, RR, RR) := (a, b, c, d) -> (
    a + b + c + d
    )


The above example works fine and as expected, however the following,

bar = method()
bar(RR, RR, RR, RR, RR) := (a, b, c, d, e) -> (
    a + b + c + d + e
    )


throws an error (whether loaded from within package or not):

error: expected 1, 2, 3, or 4 parameter types

This error message appears in the file Macaulay2/d/actors.d

My questions are:
  1. Is my syntax for defining a method correct, or is there a preferred way?
  2. Is this a hard-coded limit of 4 on the number of non-optional parameters that a method can take as input?

The workaround I have found is to define bar instead as:

bar = method()
bar := (a, b, c, d, e) -> (
    a + b + c + d + e
    )

which works for my setting (where it is a non-exported method for a package).

Mahrud Sayrafi

unread,
Jul 2, 2024, 7:05:11 PMJul 2
to maca...@googlegroups.com
Hi Mike,

1. The syntax is correct.
2. It is unfortunately hardcoded, but imo that's unnecessary and could theoretically be unlimited without any drawbacks that I can think of. It just takes knowledge of the interpreter and a decent bit of changes.

A couple of other workarounds I've seen:

1. You could group some of the arguments in a sequence or list, e.g. submatrixByDegrees(Matrix, List, List) is really submatrixByDegrees(Matrix, {target-low-list, source-low-list}, {target-high-list, source-high-list}), which is five inputs, then validate the types of the inputs manually.

2. You could have some of the inputs be options instead (e.g. DegreeLimit, etc.)

3. If all inputs are the same, as in your example, you could implement a method that takes in a sequence instead.

i1 : foo = method(Dispatch => Thing)

i2 : foo Sequence := L -> sum toList L

i3 : foo(1.0, 2.0, 3.0, 4.0, 5.0)

o3 = 15


4. If your method is a binary operator (e.g. tensor, etc.) you could implement it as follows:

i1 : foo = method(Binary => true)

i2 : foo(RR, RR) := (x, y) -> x + y

i3 : foo(1.0, 2.0, 3.0, 4.0, 5.0, 6.0)

o3 = 21

o3 : RR (of precision 53)

i4 : foo(Number, Number) := foo(RR, Number) := foo(Number, RR) := (x, y) -> x + y

i5 : foo(1, 2.0, 3, 4.0, 5)

o5 = 15


Best,
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 on the web visit https://groups.google.com/d/msgid/macaulay2/21c4673b-dba8-4c17-bf86-201fe5fcc26an%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages