Macaulay2 Engine
Loading...
Searching...
No Matches

◆ coeffs()

Matrix * Matrix::coeffs ( M2_arrayint vars,
const Matrix * monoms ) const

Definition at line 1852 of file matrix.cpp.

1854{
1855 // Given an array_ of variable indices, 'vars', and given
1856 // that 'monoms' and 'this' both have one row, makes a matrix
1857 // having number of rows = ncols(monoms),
1858 // number of cols = ncols(this),
1859 // whose (r,c) entry is the coefficient (in the other variables)
1860 // of this[0,c] in the monomial monoms[0,r].
1861
1862 // Step 0: Do some error checking
1863 const PolynomialRing *P = get_ring()->cast_to_PolynomialRing();
1864 if (P != nullptr)
1865 {
1866 int nvars = P->n_vars();
1867 int nelements = monoms->n_cols();
1868 if (monoms->n_rows() != n_rows())
1869 {
1870 ERROR("expected matrices with the same number of rows");
1871 return nullptr;
1872 }
1873 for (unsigned int i = 0; i < vars->len; i++)
1874 if (vars->array[i] < 0 || vars->array[i] >= nvars)
1875 {
1876 ERROR("coeffs: expected a set of variable indices");
1877 return nullptr;
1878 }
1879
1880 // Step 1: Make an exponent_table of all of the monoms.
1881 // We set the value of the i th monomial to be 'i+1', since 0
1882 // indicates a non-existent entry.
1883
1884 // The extra size in monomial refers to the component:
1885 exponent_table *E = exponent_table_new(nelements, 1 + vars->len);
1886 exponent EXP = newarray_atomic(int, nvars);
1887 for (int i = 0; i < nelements; i++)
1888 {
1889 vec v = monoms->elem(i);
1890 if (v == nullptr)
1891 {
1892 ERROR("expected non-zero column");
1893 return nullptr;
1894 }
1895 ring_elem f = v->coeff;
1897 P->getMonoid()->to_expvector(m, EXP);
1898
1899 // grab only that part of the monomial we need
1900 exponent e = newarray_atomic(int, 1 + vars->len);
1901 get_part_of_expvector(vars, EXP, v->comp, e);
1902 exponent_table_put(E, e, i + 1);
1903 }
1904
1905 // Step 2: for each vector column of 'this'
1906 // create a column, and put this vector into result.
1907
1909 for (int i = 0; i < n_cols(); i++)
1910 mat.append(coeffs_of_vec(E, vars, rows(), elem(i)));
1911
1912 return mat.to_matrix();
1913 }
1914 const M2FreeAlgebraOrQuotient* Q = dynamic_cast<const M2FreeAlgebraOrQuotient*>(get_ring());
1915 if (Q != nullptr)
1916 {
1917 ModuleMonomialSet H(Q->n_vars());
1918 NCMonomials(H, monoms);
1919 return NCCoefficientMatrix(H, this);
1920 }
1921 ERROR("expected polynomial ring");
1922 return nullptr;
1923}
static const int nelements
virtual int n_vars() const =0
const Ring * get_ring() const
Definition matrix.hpp:134
ring_elem elem(int i, int j) const
Definition matrix.cpp:307
friend class MatrixConstructor
Definition matrix.hpp:76
int n_cols() const
Definition matrix.hpp:147
int n_rows() const
Definition matrix.hpp:146
const FreeModule * rows() const
Definition matrix.hpp:144
void to_expvector(const_monomial m, exponents_t result_exp) const
Definition monoid.cpp:747
virtual const Monoid * getMonoid() const
Definition polyring.hpp:282
int n_vars() const
Definition polyring.hpp:196
virtual const_monomial lead_flat_monomial(const ring_elem f) const =0
virtual FreeModule * make_FreeModule() const
Definition ring.cpp:53
virtual const PolynomialRing * cast_to_PolynomialRing() const
Definition ring.hpp:243
exponent_table * exponent_table_new(int hint, int nvars)
Definition exptable.c:45
long exponent_table_put(exponent_table *E, const exponent expon, long value)
Definition exptable.c:62
int * exponent
Definition exptable.h:34
const int * const_monomial
Definition imonorder.hpp:45
const int ERROR
Definition m2-mem.cpp:55
static void get_part_of_expvector(M2_arrayint vars, exponent big, int comp, exponent result)
Definition matrix.cpp:1788
static vec coeffs_of_vec(exponent_table *E, M2_arrayint vars, const FreeModule *F, vec f)
Definition matrix.cpp:1807
void NCMonomials(ModuleMonomialSet &H, const Matrix *M)
Definition matrix.cpp:1593
Matrix * NCCoefficientMatrix(ModuleMonomialSet &H, const Matrix *M)
Definition matrix.cpp:1639
IntsSet< ModuleMonomDefaultConfig > ModuleMonomialSet
#define newarray_atomic(T, len)
Definition newdelete.hpp:91

References MatrixConstructor::append(), coeffs(), coeffs_of_vec(), elem(), ERROR, exponent_table_new(), exponent_table_put(), get_part_of_expvector(), get_ring(), PolynomialRing::getMonoid(), PolynomialRing::lead_flat_monomial(), Ring::make_FreeModule(), Matrix(), MatrixConstructor, n_cols(), n_rows(), M2FreeAlgebraOrQuotient::n_vars(), PolynomialRing::n_vars(), NCCoefficientMatrix(), NCMonomials(), nelements, newarray_atomic, rows(), Monoid::to_expvector(), and MatrixConstructor::to_matrix().

Referenced by coeffs(), and rawCoefficients().