Skip to content

Commit de861cb

Browse files
committed
Push down rawMatrixEntry
1 parent fea71d1 commit de861cb

4 files changed

Lines changed: 49 additions & 15 deletions

File tree

M2/Macaulay2/e/interface/matrix.cpp

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,21 +57,7 @@ const RingElement /* or null */ *rawMatrixEntry(const Matrix *M,
5757
{
5858
try
5959
{
60-
if (r < 0 || r >= M->n_rows())
61-
{
62-
ERROR("matrix row index %d out of range 0 .. %d", r, M->n_rows() - 1);
63-
return nullptr;
64-
}
65-
if (c < 0 || c >= M->n_cols())
66-
{
67-
ERROR("matrix column index %d out of range 0 .. %d",
68-
c,
69-
M->n_cols() - 1);
70-
return nullptr;
71-
}
72-
ring_elem result;
73-
result = M->elem(r, c);
74-
return RingElement::make_raw(M->get_ring(), result);
60+
return M->entry(r, c);
7561
} catch (const exc::engine_error& e)
7662
{
7763
ERROR(e.what());

M2/Macaulay2/e/matrix.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,23 @@ unsigned int Matrix::computeHashValue() const
5858
return hashval;
5959
}
6060

61+
const RingElement /* or null */ *Matrix::entry(int r, int c) const
62+
{
63+
if (r < 0 || r >= n_rows())
64+
{
65+
ERROR("matrix row index %d out of range 0 .. %d", r, n_rows() - 1);
66+
return nullptr;
67+
}
68+
if (c < 0 || c >= n_cols())
69+
{
70+
ERROR("matrix column index %d out of range 0 .. %d",
71+
c,
72+
n_cols() - 1);
73+
return nullptr;
74+
}
75+
return RingElement::make_raw(get_ring(), elem(r, c));
76+
}
77+
6178
engine_RawRingElementArrayArrayOrNull Matrix::entries() const
6279
{
6380
int ncols = n_cols();

M2/Macaulay2/e/matrix.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <vector>
1111

1212
class MatrixConstructor;
13+
class RingElement;
1314

1415
/**
1516
* \ingroup matrices
@@ -92,6 +93,7 @@ class Matrix : public EngineObject
9293
ring_elem elem(int i, int j) const;
9394
vec &elem(int i) { return mEntries[i]; }
9495
const vec &elem(int i) const { return mEntries[i]; }
96+
const RingElement /* or null */ *entry(int r, int c) const;
9597
engine_RawRingElementArrayArrayOrNull entries() const;
9698
/*****************************************/
9799

M2/Macaulay2/e/unit-tests/MatrixIOTest.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,35 @@ TEST(Matrix, entriesFromSparseColumns)
140140
}
141141
}
142142

143+
TEST(Matrix, entry)
144+
{
145+
const Ring* R = simplePolynomialRing(101, {"x"});
146+
const FreeModule* target = R->make_FreeModule(3);
147+
MatrixConstructor mat(target, 4);
148+
149+
mat.set_entry(1, 2, R->from_long(17));
150+
mat.compute_column_degrees();
151+
152+
const Matrix* M = mat.to_matrix();
153+
const RingElement* nonzero = M->entry(1, 2);
154+
ASSERT_NE(nonzero, nullptr);
155+
EXPECT_EQ(nonzero->get_ring(), R);
156+
EXPECT_TRUE(R->is_equal(nonzero->get_value(), R->from_long(17)));
157+
158+
const RingElement* zero = M->entry(0, 0);
159+
ASSERT_NE(zero, nullptr);
160+
EXPECT_EQ(zero->get_ring(), R);
161+
EXPECT_TRUE(R->is_zero(zero->get_value()));
162+
163+
EXPECT_EQ(M->entry(3, 0), nullptr);
164+
EXPECT_TRUE(error());
165+
EXPECT_STREQ(error_message(), "matrix row index 3 out of range 0 .. 2");
166+
167+
EXPECT_EQ(M->entry(0, 4), nullptr);
168+
EXPECT_TRUE(error());
169+
EXPECT_STREQ(error_message(), "matrix column index 4 out of range 0 .. 3");
170+
}
171+
143172
TEST(Matrix, concatArray)
144173
{
145174
const PolynomialRing* R = simplePolynomialRing(101, {"x", "y"});

0 commit comments

Comments
 (0)