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

Add Vector == ZZ method #3606

Open
wants to merge 4 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions M2/Macaulay2/m2/matrix.m2
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,10 @@ Matrix _ Sequence := RingElement => (m,ind) -> (
else error "expected a sequence of length two"
)

Number == Matrix :=
RingElement == Matrix := (r,m) -> m == r
Number == Matrix :=
RingElement == Matrix :=
Number == Vector :=
RingElement == Vector := (r,m) -> m == r
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get that this is necessary but looking at it I realized I hate the way things are, so opened #3608.


Matrix == Matrix := (f,g) -> (
if source f === source g
Expand All @@ -115,26 +117,34 @@ Matrix == Matrix := (f,g) -> (
target f == target g and
raw(super f * inducedMap(source f, source g)) === raw super g
)
Vector == Matrix := (v,m) -> matrix v == m
Matrix == Vector := (m,v) -> m == matrix v

Matrix == Number :=
Matrix == RingElement := (m,f) -> m - f == 0 -- slow!
Vector == Number :=
Vector == RingElement := (v,f) -> matrix v == f
Matrix == ZZ := (m,i) -> if i === 0 then rawIsZero m.RawMatrix else m - i == 0

Matrix + Matrix := Matrix => (
(f,g) -> map(target f, source f, reduce(target f, raw f + raw g))
) @@ toSameRing
Matrix + RingElement := (f,r) -> if r == 0 then f else f + r*id_(target f)
RingElement + Matrix := (r,f) -> if r == 0 then f else r*id_(target f) + f
Number + Matrix := (i,f) -> if i === 0 then f else i*id_(target f) + f
Matrix + Number := (f,i) -> if i === 0 then f else f + i*id_(target f)
Matrix + RingElement :=
Matrix + Number := (f,r) -> if r == 0 then f else f + r*id_(target f)
RingElement + Matrix :=
Number + Matrix := (r,f) -> f + r
Vector + Number := Vector + RingElement := (v,r) -> vector(matrix v + r)
Number + Vector := RingElement + Vector := (r,v) -> vector(r + matrix v)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i1 : Number + Vector := RingElement + Vector := (r,v) -> vector(r + matrix v);

i2 : 1 + (coker matrix {{ 10 }})_0
stdio:1:58:(3):[1]: error: expected source to be free with rank 1

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is coming from vector(Matrix):

i1 : code (vector, Matrix)

o1 = -- code for method: vector(Matrix)
     M2/Macaulay2/m2/modules.m2:66:17-69:5: --source code:
     vector Matrix := f -> (
          if not isFreeModule source f or numgens source f =!= 1 then error "expected source to be free with rank 1";
          new target f from {f}
         )

Should Vector objects be elements of free modules? Or arbitrary modules?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not the problem. Single generators of any module whatsoever are vectors. The implementation of Number + Vector isn't correct because it uses the identity matrix on the target.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, this seems to be a problem with matrix addition:

i1 : M = coker matrix 10;

i2 : A = map(M, ZZ^1, 1)

o2 = | 1 |

                    1
o2 : Matrix M <-- ZZ

i3 : 1 + A

o3 = | 2 |

o3 : Matrix M <-- M

i4 : A + 1

o4 = | 2 |

                    1
o4 : Matrix M <-- ZZ

Shouldn't they both have ZZ^1 as their source?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just pushed a new commit that should fix this


Matrix - Matrix := Matrix => (
(f,g) -> map(target f, source f, reduce(target f, raw f - raw g))
) @@ toSameRing
Matrix - RingElement := (f,r) -> if r == 0 then f else f - r*id_(target f)
RingElement - Matrix := (r,f) -> if r == 0 then -f else r*id_(target f) - f
Number - Matrix := (i,f) -> if i === 0 then -f else i*id_(target f) - f
Matrix - Number := (f,i) -> if i === 0 then f else f - i*id_(target f)
Matrix - RingElement :=
Matrix - Number := (f,r) -> if r == 0 then f else f - r*id_(target f)
RingElement - Matrix :=
Number - Matrix := (r,f) -> -f + r
Vector - Number := Vector - RingElement := (v,r) -> vector(matrix v - r)
Number - Vector := RingElement - Vector := (r,v) -> vector(r - matrix v)

- Matrix := Matrix => f -> new Matrix from {
symbol ring => ring f,
Expand Down
4 changes: 4 additions & 0 deletions M2/Macaulay2/packages/Macaulay2Doc/operators/minus.m2
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,18 @@ document {
(symbol -, Number, InfiniteNumber),
(symbol -, Number, Matrix),
(symbol -, Number, RingElement),
(symbol -, Number, Vector),
(symbol -, RingElement),
(symbol -, RingElement, Constant),
(symbol -, RingElement, InexactNumber),
(symbol -, RingElement, Matrix),
(symbol -, RingElement, Number),
(symbol -, RingElement, RingElement),
(symbol -, RingElement, Vector),
(symbol -, Tally),
(symbol -, Vector),
(symbol -, Vector, Number),
(symbol -, Vector, RingElement),
(symbol -, Vector, Vector),
},
Headline => "a unary or binary operator, usually used for negation or subtraction",
Expand Down
4 changes: 4 additions & 0 deletions M2/Macaulay2/packages/Macaulay2Doc/operators/plus.m2
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,20 @@ document {
(symbol +, Number, InfiniteNumber),
(symbol +, Number, Matrix),
(symbol +, Number, RingElement),
(symbol +, Number, Vector),
(symbol +, RingElement),
(symbol +, RingElement, Constant),
(symbol +, RingElement, Ideal),
(symbol +, RingElement, InexactNumber),
(symbol +, RingElement, Matrix),
(symbol +, RingElement, Number),
(symbol +, RingElement, RingElement),
(symbol +, RingElement, Vector),
(symbol +, Ideal, Number),
(symbol +, Ideal, RingElement),
(symbol +, Vector),
(symbol +, Vector, Number),
(symbol +, Vector, RingElement),
(symbol +, Vector, Vector),
},
Headline => "a unary or binary operator, usually used for addition",
Expand Down
1 change: 1 addition & 0 deletions M2/Macaulay2/tests/normal/matrix.m2
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ A = map(M, ZZ^1, {{1}})
assert Equation(A + A, 0)
assert Equation(A - A, 0)
assert Equation(A, -A)
assert Equation(A + 1, 1 + A)

-- empty matrix
scan({matrix {}, matrix(ZZ, {}), map(ZZ^0, ZZ^0, {}), map(ZZ^0,, {})}, A -> (
Expand Down
4 changes: 4 additions & 0 deletions M2/Macaulay2/tests/normal/vector.m2
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ N = image vars R
v = vector(N, {1, 2, 3})
w = vector(N, {4, 5, 6})
assert Equation(v, vector map(N,, {{1}, {2}, {3}}))
assert Equation(v, matrix v)
assert Equation(matrix v, v)

assert zero vector {0, 0, 0}

-- module operations
assert Equation(+v, v)
Expand Down
Loading