Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"code methods" should deduplicate entries #3364

Closed
mahrud opened this issue Jul 17, 2024 · 0 comments · Fixed by #3388
Closed

"code methods" should deduplicate entries #3364

mahrud opened this issue Jul 17, 2024 · 0 comments · Fixed by #3388
Assignees

Comments

@mahrud
Copy link
Member

mahrud commented Jul 17, 2024

I'm not sure what's the best approach here, but I think this example makes it hard to realize that three methods are really the same code:

i1 : code methods(map, Module, List)

o1 = -- code for method: map(Module,Module,List)
     ../linuxbrew/.linuxbrew/share/Macaulay2/Core/matrix1.m2:93:45-158:10: --source code:
     map(Module,Module,List) := Matrix => options -> (M,N,p) -> (
          R := ring M;
          local rankN;
          local k;
          if N === null then (
               k = R;
               if #p === 0 then error "expected non-empty list of entries for matrix";
               rankN = #p#0;
               )
          else if class N === ZZ then (
               k = R;
               rankN = N;
               )
          else (
               k = ring N;
               try promote(1_k,R) else error "modules over incompatible rings";
               -- later, allow a ring homomorphism
               rankN = numgens N;
               );
          if not instance(N,Module) and options.Degree =!= null then error "Degree option given with indeterminate source module";
          deg := if options.Degree === null then toList ((degreeLength R):0) else options.Degree;
          deg = degreeCheck(deg,R);
          p = splice p;
          if all(p, o -> (
                    instance(o,Option)
                    and #o == 2
                    and class o#0 === Sequence
                    and #o#0 == 2
                    and class o#0#0 === ZZ
                    and class o#0#1 === ZZ
                    )
               ) then (                  -- sparse list of entries
               rows := apply(p, o -> o#0#0);
               cols := apply(p, o -> o#0#1);
               ents := toSequence apply(p, o -> raw promote(o#1,R));
               pref := 0;                                        -- ???
               m := (
                    if class N === Module
                    then rawSparseMatrix2(raw cover M, raw cover N, deg, rows, cols, ents, pref)
                    else rawSparseMatrix1(raw cover M, rankN, rows, cols, ents, pref)
                    );
               new Matrix from {
                    symbol target => M,
                    symbol RawMatrix => reduce(M,m),
                    symbol source => if class N === Module then N else new Module from (R, rawSource m),
                    symbol ring => R,
                    symbol cache => new CacheTable
                    })
          else if all(p, o -> instance(o,List)) then (                   -- dense list of entries or blocks
               p = apply(splice p,splice);
               if #p != numgens M or #p > 0 and ( not isTable p or # p#0 != rankN )
               then error( "expected ", toString numgens M, " by ", toString rankN, " table");
               p = toSequence makeRawTable(R,p);
               h := (
                    if instance(N,Module)
                    then rawMatrix2(raw cover M, raw cover N, deg, flatten p,0)
                    else rawMatrix1(raw cover M, rankN, flatten p, 0)
                    );
               new Matrix from {
                    symbol target => M,
                    symbol RawMatrix => reduce(M,h),
                    symbol source => if class N === Module then N else new Module from (R, rawSource h),
                    symbol ring => R,
                    symbol cache => new CacheTable
                    })
          else error "expected a list of lists or a list of options (i,j)=>r")
     -------------------------------------------------------------------------------------------------------------------------
     -- code for method: map(Module,Module,RingMap,List)
     ../linuxbrew/.linuxbrew/share/Macaulay2/Core/ringmap.m2:568:47-568:94: --source code:
     map(Module,Module,RingMap,List) := Matrix => o -> (M,N,p,f) -> map(M,N,p,map(M,ring M ** N,f),o)
     -------------------------------------------------------------------------------------------------------------------------
     -- code for method: map(Module,Nothing,List)
     ../linuxbrew/.linuxbrew/share/Macaulay2/Core/matrix1.m2:93:45-158:10: --source code:
     map(Module,Module,List) := Matrix => options -> (M,N,p) -> (
          R := ring M;
          local rankN;
          local k;
          if N === null then (
               k = R;
               if #p === 0 then error "expected non-empty list of entries for matrix";
               rankN = #p#0;
               )
          else if class N === ZZ then (
               k = R;
               rankN = N;
               )
          else (
               k = ring N;
               try promote(1_k,R) else error "modules over incompatible rings";
               -- later, allow a ring homomorphism
               rankN = numgens N;
               );
          if not instance(N,Module) and options.Degree =!= null then error "Degree option given with indeterminate source module";
          deg := if options.Degree === null then toList ((degreeLength R):0) else options.Degree;
          deg = degreeCheck(deg,R);
          p = splice p;
          if all(p, o -> (
                    instance(o,Option)
                    and #o == 2
                    and class o#0 === Sequence
                    and #o#0 == 2
                    and class o#0#0 === ZZ
                    and class o#0#1 === ZZ
                    )
               ) then (                  -- sparse list of entries
               rows := apply(p, o -> o#0#0);
               cols := apply(p, o -> o#0#1);
               ents := toSequence apply(p, o -> raw promote(o#1,R));
               pref := 0;                                        -- ???
               m := (
                    if class N === Module
                    then rawSparseMatrix2(raw cover M, raw cover N, deg, rows, cols, ents, pref)
                    else rawSparseMatrix1(raw cover M, rankN, rows, cols, ents, pref)
                    );
               new Matrix from {
                    symbol target => M,
                    symbol RawMatrix => reduce(M,m),
                    symbol source => if class N === Module then N else new Module from (R, rawSource m),
                    symbol ring => R,
                    symbol cache => new CacheTable
                    })
          else if all(p, o -> instance(o,List)) then (                   -- dense list of entries or blocks
               p = apply(splice p,splice);
               if #p != numgens M or #p > 0 and ( not isTable p or # p#0 != rankN )
               then error( "expected ", toString numgens M, " by ", toString rankN, " table");
               p = toSequence makeRawTable(R,p);
               h := (
                    if instance(N,Module)
                    then rawMatrix2(raw cover M, raw cover N, deg, flatten p,0)
                    else rawMatrix1(raw cover M, rankN, flatten p, 0)
                    );
               new Matrix from {
                    symbol target => M,
                    symbol RawMatrix => reduce(M,h),
                    symbol source => if class N === Module then N else new Module from (R, rawSource h),
                    symbol ring => R,
                    symbol cache => new CacheTable
                    })
          else error "expected a list of lists or a list of options (i,j)=>r")
     -------------------------------------------------------------------------------------------------------------------------
     -- code for method: map(Module,Nothing,RingMap,List)
     ../linuxbrew/.linuxbrew/share/Macaulay2/Core/ringmap.m2:569:48-569:84: --source code:
     map(Module,Nothing,RingMap,List) := Matrix => o -> (M,N,p,f) -> map(M,N,p,map(M,,f),o)
     -------------------------------------------------------------------------------------------------------------------------
     -- code for method: map(Module,ZZ,List)
     ../linuxbrew/.linuxbrew/share/Macaulay2/Core/matrix1.m2:93:45-158:10: --source code:
     map(Module,Module,List) := Matrix => options -> (M,N,p) -> (
          R := ring M;
          local rankN;
          local k;
          if N === null then (
               k = R;
               if #p === 0 then error "expected non-empty list of entries for matrix";
               rankN = #p#0;
               )
          else if class N === ZZ then (
               k = R;
               rankN = N;
               )
          else (
               k = ring N;
               try promote(1_k,R) else error "modules over incompatible rings";
               -- later, allow a ring homomorphism
               rankN = numgens N;
               );
          if not instance(N,Module) and options.Degree =!= null then error "Degree option given with indeterminate source module";
          deg := if options.Degree === null then toList ((degreeLength R):0) else options.Degree;
          deg = degreeCheck(deg,R);
          p = splice p;
          if all(p, o -> (
                    instance(o,Option)
                    and #o == 2
                    and class o#0 === Sequence
                    and #o#0 == 2
                    and class o#0#0 === ZZ
                    and class o#0#1 === ZZ
                    )
               ) then (                  -- sparse list of entries
               rows := apply(p, o -> o#0#0);
               cols := apply(p, o -> o#0#1);
               ents := toSequence apply(p, o -> raw promote(o#1,R));
               pref := 0;                                        -- ???
               m := (
                    if class N === Module
                    then rawSparseMatrix2(raw cover M, raw cover N, deg, rows, cols, ents, pref)
                    else rawSparseMatrix1(raw cover M, rankN, rows, cols, ents, pref)
                    );
               new Matrix from {
                    symbol target => M,
                    symbol RawMatrix => reduce(M,m),
                    symbol source => if class N === Module then N else new Module from (R, rawSource m),
                    symbol ring => R,
                    symbol cache => new CacheTable
                    })
          else if all(p, o -> instance(o,List)) then (                   -- dense list of entries or blocks
               p = apply(splice p,splice);
               if #p != numgens M or #p > 0 and ( not isTable p or # p#0 != rankN )
               then error( "expected ", toString numgens M, " by ", toString rankN, " table");
               p = toSequence makeRawTable(R,p);
               h := (
                    if instance(N,Module)
                    then rawMatrix2(raw cover M, raw cover N, deg, flatten p,0)
                    else rawMatrix1(raw cover M, rankN, flatten p, 0)
                    );
               new Matrix from {
                    symbol target => M,
                    symbol RawMatrix => reduce(M,h),
                    symbol source => if class N === Module then N else new Module from (R, rawSource h),
                    symbol ring => R,
                    symbol cache => new CacheTable
                    })
          else error "expected a list of lists or a list of options (i,j)=>r")

Notice:

i2 : locate methods(map, Module, List)

o2 = {0 => (../linuxbrew/.linuxbrew/share/Macaulay2/Core/matrix1.m2:93:45-158:10) }
     {1 => (../linuxbrew/.linuxbrew/share/Macaulay2/Core/ringmap.m2:568:47-568:94)}
     {2 => (../linuxbrew/.linuxbrew/share/Macaulay2/Core/matrix1.m2:93:45-158:10) }
     {3 => (../linuxbrew/.linuxbrew/share/Macaulay2/Core/ringmap.m2:569:48-569:84)}
     {4 => (../linuxbrew/.linuxbrew/share/Macaulay2/Core/matrix1.m2:93:45-158:10) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant