File: mutablemat-imp.hpp

package info (click to toggle)
macaulay2 1.24.11%2Bds-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 171,648 kB
  • sloc: cpp: 107,850; ansic: 16,307; javascript: 4,188; makefile: 3,947; lisp: 682; yacc: 604; sh: 476; xml: 177; perl: 114; lex: 65; python: 33
file content (350 lines) | stat: -rw-r--r-- 11,278 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// Copyright 2015 Anton Leykin and Mike Stillman

// Anton Leykin's code in this file is in the public domain.

#ifndef _mutable_mat_imp_hpp_
#define _mutable_mat_imp_hpp_

template <typename Mat>
M2SLEvaluator* MutableMat<Mat>::createSLEvaluator(M2SLProgram* P,
                                                M2_arrayint constsPos,
                                                M2_arrayint varsPos) const
{
  if (n_rows() != 1 || n_cols() != constsPos->len) {
    ERROR("1-row matrix expected; or numbers of constants don't match");
    return nullptr;
  } else return new M2SLEvaluator(
    new SLEvaluatorConcrete<typename Mat::CoeffRing> (&(P->value()), constsPos, varsPos, this)
    );
}

template <typename Mat>
M2SLEvaluator* MutableMat<Mat>::createCompiledSLEvaluator(
      M2_string libName,
      int nInputs,
      int nOutputs) const
{
  return new M2SLEvaluator(
    new SLEvaluatorConcrete<typename Mat::CoeffRing> (libName, nInputs, nOutputs, this)
    );
}

template <typename T>
size_t MutableMat<T>::rank() const
{
  return MatrixOps::rank(mat);
}

template <typename T>
const RingElement* MutableMat<T>::determinant() const
{
  ring_elem det;
  typename T::Element a(mat.ring());
  MatrixOps::determinant(mat, a);
  //  MatrixOps::BasicLinAlg<MatType>::determinant(mat, a);
  mat.ring().to_ring_elem(det, a);
  return RingElement::make_raw(get_ring(), det);
}

template <typename T>
MutableMatrix* MutableMat<T>::invert() const
{
  assert(n_rows() == n_cols());
  MutableMat<T>* result = makeZeroMatrix(n_rows(), n_cols());
  bool val = MatrixOps::inverse(mat, result->mat);
  if (!val)
    {
      delete result;
      return 0;
    }
  return result;
}

template <typename T>
MutableMatrix* MutableMat<T>::rowReducedEchelonForm() const
{
  MutableMat<T>* result = makeZeroMatrix(n_rows(), n_cols());
  try
    {
      // ignore returned value (the rank of mat):
      MatrixOps::rowReducedEchelonForm(mat, result->mat);
      return result;
  } catch (const exc::engine_error& e)
    {
      delete result;
      throw;
  }
}

template <typename T>
MutableMatrix* MutableMat<T>::solveLinear(const MutableMatrix* B) const
{
  const T* B1 = B->coerce_const<T>();
  if (B1 == 0) throw exc::engine_error("expected matrices of the same type");
  if (B->get_ring() != get_ring())
    throw exc::engine_error("expected same ring");
  if (B->n_rows() != n_rows())
    throw exc::engine_error("expected matrices with same number of rows");
  MutableMat<T>* solns = makeZeroMatrix(0, 0);
  bool retval = MatrixOps::solveLinear(mat, *B1, solns->mat);
  if (retval) return solns;
  delete solns;
  return NULL;
}

template <typename T>
MutableMatrix* MutableMat<T>::solveInvertible(const MutableMatrix* B) const
{
  const T* B1 = B->coerce_const<T>();
  if (B1 == 0) throw exc::engine_error("expected matrices of the same type");
  if (B->get_ring() != get_ring())
    throw exc::engine_error("expected same ring");
  if (n_rows() != n_cols()) throw exc::engine_error("expected a square matrix");
  if (B->n_rows() != n_rows())
    throw exc::engine_error("expected matrices with same number of rows");
  MutableMat<T>* solns = makeZeroMatrix(0, 0);
  bool retval = MatrixOps::solveInvertible(mat, *B1, solns->mat);
  if (retval) return solns;
  delete solns;
  return NULL;
}

template <typename T>
MutableMatrix* MutableMat<T>::nullSpace() const
{
  MutableMat<T>* ker = makeZeroMatrix(0, 0);
  MatrixOps::nullSpace(mat, ker->mat);  // ignore return value of nullSpace...
  return ker;
}

template <typename T>
MutableMatrix /* or null */* MutableMat<T>::mult(const MutableMatrix* B) const
{
  // First, make sure B has the same ring/type as 'this'.
  const T* B1 = B->coerce_const<T>();
  if (B1 == 0)
    {
      ERROR(
          "mutable matrix/ring type for (mutable) matrix multiplication "
          "required to be the same");
      return 0;
    }
  // Second, make sure the sizes are correct.
  if (mat.numColumns() != B1->numRows())
    {
      ERROR("matrix sizes do not match in matrix multiplication");
      return 0;
    }
  // create the result matrix
  MutableMat<T>* result = makeZeroMatrix(n_rows(), B1->numColumns());
  MatrixOps::mult(mat, *B1, result->mat);

  return result;
}

template <typename T>
void MutableMat<T>::addMultipleTo(const MutableMatrix* A,
                                  const MutableMatrix* B)
{
  // First: make sure that A, B have the right ring/matrix type
  const T* A1 = A->coerce_const<T>();
  const T* B1 = B->coerce_const<T>();
  if (A1 == 0 or B1 == 0)
    throw exc::engine_error(
        "mutable matrix/ring type for (mutable) matrix multiplication required "
        "to be the same");
  if (mat.numRows() != A1->numRows() or mat.numColumns() != B1->numColumns())
    throw exc::engine_error(
        "expected matrix sizes to be compatible with matrix multiplication");
  MatrixOps::addMultipleTo(mat, *A1, *B1);
}

template <typename T>
void MutableMat<T>::subtractMultipleTo(const MutableMatrix* A,
                                       const MutableMatrix* B)
{
  // First: make sure that A, B have the right ring/matrix type
  const T* A1 = A->coerce_const<T>();
  const T* B1 = B->coerce_const<T>();
  if (A1 == 0 or B1 == 0)
    throw exc::engine_error(
        "mutable matrix/ring type for (mutable) matrix multiplication required "
        "to be the same");
  if (mat.numRows() != A1->numRows() or mat.numColumns() != B1->numColumns())
    throw exc::engine_error(
        "expected matrix sizes to be compatible with matrix multiplication");
  MatrixOps::subtractMultipleTo(mat, *A1, *B1);
}

template <typename T>
M2_arrayintOrNull MutableMat<T>::rankProfile(bool row_profile) const
{
  //  LUComputation<T> C(mat);
  //  return C.rankProfile(row_profile);
  return MatrixOps::rankProfile(mat, row_profile);
}

template <typename T>
M2_arrayintOrNull MutableMat<T>::LU(MutableMatrix* L, MutableMatrix* U) const
{
  T* L1 = L->coerce<T>();
  T* U1 = U->coerce<T>();
  if (L1 == 0 or U1 == 0)
    throw exc::engine_error("expected matrices of the same ring/type");
  return MatrixOps::LU(mat, *L1, *U1);
}

template <typename T> // T should be a matrix type, generally DMat<RT>
M2_arrayintOrNull MutableMat<T>::LUincremental(std::vector<size_t>& P,
                                  const MutableMatrix* v,
                                  int m)
{
  T* LU1 = this->coerce<T>();
  const T* v1 = const_cast<MutableMatrix*>(v)->coerce<T>();
  if (LU1 == nullptr or v1 == nullptr)
    throw exc::engine_error("expected matrices of the same ring/type");
  return MatrixOps::LUincremental(P, *LU1, *v1, m);
}

template <typename T> // T should be a matrix type, generally DMat<RT>
void MutableMat<T>::triangularSolve(MutableMatrix* x,
                                    int m,
                                    int strategy)
{
  T* Lv1 = this->coerce<T>();
  T* x1 = x->coerce<T>();
  if (Lv1 == nullptr or x1 == nullptr)
    throw exc::engine_error("expected matrices of the same ring/type");
  MatrixOps::triangularSolve(*Lv1, *x1, m, strategy);
}

template <typename T>
bool MutableMat<T>::eigenvalues(MutableMatrix* eigenvals,
                                bool is_symm_or_hermitian) const
{
  if (!is_dense())
    throw exc::engine_error(
        "'eigenvalues' is only implemented for dense matrices");
  if (is_symm_or_hermitian)
    {
      auto E1 = eigenvals->coerce<DMat<HermitianEigenvalueType> >();
      if (E1 == 0)
        throw exc::engine_error("eigenvalue matrix is of the wrong type/ring");
      return MatrixOps::eigenvaluesHermitian(mat, *E1);
    }
  else
    {
      auto E1 = eigenvals->coerce<DMat<EigenvalueType> >();
      if (E1 == 0)
        throw exc::engine_error("eigenvalue matrix is of the wrong type/ring");
      return MatrixOps::eigenvalues(mat, *E1);
    }
}

template <typename T>
bool MutableMat<T>::eigenvectors(MutableMatrix* eigenvals,
                                 MutableMatrix* eigenvecs,
                                 bool is_symm_or_hermitian) const
{
  if (!is_dense())
    throw exc::engine_error(
        "'eigenvalues' is only implemented for dense matrices");
  if (is_symm_or_hermitian)
    {
      DMat<HermitianEigenvalueType>* E1 =
          eigenvals->coerce<DMat<HermitianEigenvalueType> >();
      DMat<HermitianEigenvectorType>* evecs1 =
          eigenvecs->coerce<DMat<HermitianEigenvectorType> >();
      if (E1 == 0 or evecs1 == 0)
        throw exc::engine_error(
            "eigenvalue/vector matrix is of the wrong type/ring");
      return MatrixOps::eigenvectorsHermitian(mat, *E1, *evecs1);
    }
  else
    {
      DMat<EigenvalueType>* E1 = eigenvals->coerce<DMat<EigenvalueType> >();
      DMat<EigenvectorType>* evecs1 =
          eigenvecs->coerce<DMat<EigenvectorType> >();
      if (E1 == 0 or evecs1 == 0)
        throw exc::engine_error(
            "eigenvalue/vector matrix is of the wrong type/ring");
      return MatrixOps::eigenvectors(mat, *E1, *evecs1);
    }
}

template <typename T>
bool MutableMat<T>::least_squares(const MutableMatrix* B,
                                  MutableMatrix* X,
                                  bool assume_full_rank) const
{
  const T* B1 = B->coerce_const<T>();
  T* X1 = X->coerce<T>();
  if (B1 == 0 or X1 == 0)
    throw exc::engine_error("expected matrices of the same type");
  bool retval = MatrixOps::leastSquares(mat, *B1, *X1, assume_full_rank);
  return retval;
}

template <typename T>
bool MutableMat<T>::QR(MutableMatrix* Q, MutableMatrix* R, bool return_QR) const
{
  if (!is_dense())
    throw exc::engine_error("'QR' is only implemented for dense matrices");

  auto Q1 = Q->coerce<DMat<CoeffRing> >();
  if (Q1 == 0) throw exc::engine_error("Q matrix is of the wrong type/ring");
  auto R1 = R->coerce<DMat<CoeffRing> >();
  if (R1 == 0) throw exc::engine_error("R matrix is of the wrong type/ring");
  return MatrixOps::QR(mat, *Q1, *R1, return_QR);
}

template <typename T>
bool MutableMat<T>::SVD(MutableMatrix* Sigma,
                        MutableMatrix* U,
                        MutableMatrix* Vt,
                        bool use_divide_and_conquer) const
{
  auto Sigma2 = Sigma->coerce<DMat<HermitianEigenvalueType> >();
  T* U2 = U->coerce<T>();
  T* Vt2 = Vt->coerce<T>();
  if (Sigma2 == 0 || U2 == 0 || Vt2 == 0)
    throw exc::engine_error("expected matrices of the same type");
  int strategy = (use_divide_and_conquer ? 1 : 0);
  return MatrixOps::SVD(mat, *Sigma2, *U2, *Vt2, strategy);
}

template <typename Mat>
engine_RawArrayIntPairOrNull MutableMat<Mat>::LQUPFactorizationInPlace(
    bool transpose)
{
  throw exc::engine_error(
      "LU decomposition currently not implemented for this ring and matrix "
      "type");
}

template <typename T>
void MutableMat<T>::clean(gmp_RR epsilon)
{
  MatrixOps::clean(epsilon, mat);
}

template <typename T>
gmp_RRorNull MutableMat<T>::norm() const
{
  if (get_ring()->get_precision() == 0)
    throw exc::engine_error("expected a matrix over RR or CC");

  gmp_RRmutable nm = getmemstructtype(gmp_RRmutable);
  mpfr_init2(nm, get_ring()->get_precision());
  mpfr_set_si(nm, 0, MPFR_RNDN);

  MatrixOps::increase_norm(nm, mat);
  return moveTo_gmpRR(nm);
}

#endif

// Local Variables:
// compile-command: "make -C $M2BUILDDIR/Macaulay2/e "
// indent-tabs-mode: nil
// End: