Macaulay2 / M2

The primary source code repository for Macaulay2, a system for computing in commutative algebra, algebraic geometry and related fields.
https://macaulay2.com
343 stars 229 forks source link

matrix of exponents instead of monomials from basis #3459

Open mahrud opened 1 week ago

mahrud commented 1 week ago

Is there a fast (i.e. implemented in the engine) routine for the following? (or its transpose)

exponentMatrix = B -> transpose matrix apply(numcols B, c -> first exponents B_(0,c))

-- example:
i17 : exponentMatrix basis(3, kk[x,y,z])

o17 = | 3 2 2 1 1 1 0 0 0 0 |
      | 0 1 0 2 1 0 3 2 1 0 |
      | 0 0 1 0 1 2 0 1 2 3 |

               3       10
o17 : Matrix ZZ  <-- ZZ

The one liner above obviously works, but when B is a matrix of basis monomials with tens thousands of columns, it gets quite slow (e.g. the basis itself takes a couple of seconds but computing the exponent matrix takes a minute!!)

cc: @mikestillman

mikestillman commented 1 week ago

I don't see your timings. I get something like:

i1 : exponentMatrix = B -> transpose matrix apply(numcols B, c -> first exponents B_(0,c));

i2 : kk = ZZ/32003;

i3 : elapsedTime B = basis(5, kk[x_1..x_30]); -- takes .22 sec, 278,256 columns
 -- 0.163152 seconds elapsed

                         1                  278256
o3 : Matrix (kk[x ..x  ])  <-- (kk[x ..x  ])
                 1   30             1   30

i4 : elapsedTime exponentMatrix B; -- takes anywhere from 9 to 17 or so seconds, depending on garbage collections...
 -- 17.1095 seconds elapsed

              30       278256
o4 : Matrix ZZ   <-- ZZ

i5 : elapsedTime exponentMatrix B; -- takes 
 -- 9.71178 seconds elapsed

              30       278256
o5 : Matrix ZZ   <-- ZZ

i6 : elapsedTime exponentMatrix B; -- takes 
 -- 10.2289 seconds elapsed

              30       278256
o6 : Matrix ZZ   <-- ZZ

It should still be made faster, but just wondered what your example was?

mahrud commented 1 week ago

Hmm maybe your computer is much faster than mine, or maybe it's garbage collection that's throwing in randomness.

Even in your timings though, when the basis matrix takes 0.163152s to construct a 1x278256 matrix, a conservative estimate might be 30x0.16 so ~4.8s for constructing a 3x278256 matrix, but of course this is just integers so it should be much faster I would imagine.