File: dmat.cpp

package info (click to toggle)
macaulay2 1.21%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 133,096 kB
  • sloc: cpp: 110,377; ansic: 16,306; javascript: 4,193; makefile: 3,821; sh: 3,580; lisp: 764; yacc: 590; xml: 177; python: 140; perl: 114; lex: 65; awk: 3
file content (301 lines) | stat: -rw-r--r-- 8,775 bytes parent folder | download | duplicates (2)
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
// Copyright 2005,2013  Michael E. Stillman

#include "exceptions.hpp"
#include "error.h"

#include "mat-linalg.hpp"

////////////////////////////////////////////////////////////////////////////
// dmat code that might have alternate implementations, depending of type //
////////////////////////////////////////////////////////////////////////////

namespace MatrixOps {
void addMultipleTo(DMatZZpFFPACK& C,
                   const DMatZZpFFPACK::ElementType& a,
                   const DMatZZpFFPACK& A,
                   const DMatZZpFFPACK& B)
{
  // Compute C := C + a*A*B
  // Both DMat, and FFPACK store dense matrices in row major order.
  // Note that the leading dimension in gemm arguments is #columns,
  // as the matrix is in row-major order

  FFLAS::FFLAS_TRANSPOSE tA = FFLAS::FflasNoTrans;
  FFLAS::FFLAS_TRANSPOSE tB = FFLAS::FflasNoTrans;

  size_t m = A.numRows();
  size_t k = A.numColumns();
  assert(A.numColumns() == B.numRows());
  size_t n = B.numColumns();

  assert(C.numRows() == m);
  assert(C.numColumns() == n);

  DMatZZpFFPACK::ElementType b;
  C.ring().init(b);
  C.ring().set_from_long(b, 1);
  FFLAS::fgemm(C.ring().field(),
               tB,
               tA,
               m,
               n,
               k,
               a,
               A.array(),
               A.numColumns(),
               B.array(),
               B.numColumns(),
               b,
               C.array(),
               C.numColumns());
}

void addMultipleTo(DMatZZpFFPACK& C,
                   const DMatZZpFFPACK& A,
                   const DMatZZpFFPACK& B)
{
  DMatZZpFFPACK::ElementType one;
  A.ring().set_from_long(one, 1);

  addMultipleTo(C, one, A, B);
}

void subtractMultipleTo(DMatZZpFFPACK& C,
                        const DMatZZpFFPACK& A,
                        const DMatZZpFFPACK& B)
{
  DMatZZpFFPACK::ElementType minus_one;
  A.ring().set_from_long(minus_one, -1);
  addMultipleTo(C, minus_one, A, B);
}

void mult(const DMatZZpFFPACK& A, const DMatZZpFFPACK& B, DMatZZpFFPACK& C)
{
  // We assume that C is set to the correct size, and is the zero matrix here.
  addMultipleTo(C, A, B);
}
};  // namespace MatrixOps

namespace ffpackInterface {
size_t rank(const DMatZZpFFPACK& mat)
{
  /// @note 1. matrix data (N) is modified by FFPACK
  DMatZZpFFPACK N(mat);  // copy of matrix mat.
  size_t result = FFPACK::Rank(mat.ring().field(),
                               mat.numRows(),
                               mat.numColumns(),
                               N.array(),
                               mat.numColumns());
  return result;
}

void determinant(const DMatZZpFFPACK& mat, ZZpFFPACK::ElementType& result_det)
{
  /// @note 1. matrix data (N) is modified by FFPACK
  if (mat.numRows() == 0)
    {
      // 26 April 2014: this branch is needed as FFPACK gives answer of 0 in
      // this case.
      mat.ring().set_from_long(result_det, 1);
    }
  else
    {
      DMatZZpFFPACK N(mat);
      ZZpFFPACK::ElementType det = 0;
      result_det = FFPACK::Det(mat.ring().field(),
                               det,
                               mat.numRows(),
                               N.array(),
                               mat.numColumns());
    }
}

bool inverse(const DMatZZpFFPACK& mat, DMatZZpFFPACK& result_inv)
{
  assert(mat.numRows() == mat.numColumns());
  result_inv.resize(mat.numRows(), mat.numRows());

  assert(result_inv.numRows() == mat.numRows());
  assert(result_inv.numColumns() == mat.numRows());

  if (mat.numRows() == 0)
    {
      // 26 April 2014: this branch is needed as FFPACK gives answer of 0 in
      // this case.
      return true;
    }

  DMatZZpFFPACK N(mat);
  size_t n = mat.numRows();
  int nullspacedim;
  FFPACK::Invert2(
      mat.ring().field(), n, N.array(), n, result_inv.array(), n, nullspacedim);
  return (nullspacedim == 0);
}

size_t nullSpace(const DMatZZpFFPACK& mat, DMatZZpFFPACK& nullspace)
{
  bool right_side = true;  // This function is written so that one could set
                           // right_side to false.
  // (It used to be a parameter).

  DMatZZpFFPACK N(mat);  // copy of mat
  size_t nr = mat.numRows();
  size_t nc = mat.numColumns();

  DMatZZpFFPACK::ElementType* nullspaceFFPACK = 0;

  size_t nullspace_dim;
  size_t nullspace_leading_dim;

  FFPACK::NullSpaceBasis(mat.ring().field(),
                         (right_side ? FFLAS::FflasRight : FFLAS::FflasLeft),
                         nr,
                         nc,
                         N.array(),
                         nc,
                         nullspaceFFPACK,
                         nullspace_leading_dim,
                         nullspace_dim);

  //    std::cerr << "leading dim = " << nullspace_leading_dim << " and dim = "
  //    << nullspace_dim << std::endl;
  if (right_side && nullspace_dim != nullspace_leading_dim)
    {
      std::cerr << "error: this should not happen!" << std::endl;
    }
  else if (!right_side && nullspace_leading_dim != nc)
    {
      std::cerr << "error: this should not happen either!" << std::endl;
    }

  if (right_side)
    nullspace.resize(nc, nullspace_dim);
  else
    nullspace.resize(nullspace_dim, nr);

  std::swap(nullspace.array(), nullspaceFFPACK);

  delete[] nullspaceFFPACK;
  return nullspace_dim;
}

bool solveLinear(const DMatZZpFFPACK& A,
                 const DMatZZpFFPACK& B,
                 bool right_side,
                 DMatZZpFFPACK& X,
                 bool declare_A_is_invertible)  // this parameter is unused
{
  //    std::cerr << "inside FFpackSolveLinear" << std::endl;

  size_t a_rows = A.numRows();
  size_t a_cols = A.numColumns();

  size_t b_rows = B.numRows();
  size_t b_cols = B.numColumns();

  DMatZZpFFPACK copyA(A);
  DMatZZpFFPACK copyB(B);

  // preallocate the space for the solutions:
  size_t x_rows = (right_side ? a_cols : b_rows);
  size_t x_cols = (right_side ? b_cols : a_rows);

  X.resize(x_rows, x_cols);  // sets it to 0 too.

  int info = 0;  // >0 if the system is inconsistent, ==0 means success

  FFPACK::fgesv(A.ring().field(),
                (right_side ? FFLAS::FflasLeft : FFLAS::FflasRight),
                a_rows,
                a_cols,
                (right_side ? b_cols : b_rows),
                copyA.array(),
                a_cols,  // leading dim of A
                X.array(),
                x_cols,
                copyB.array(),
                b_cols,
                &info);

  if (info > 0)
    {
      // the system is inconsistent
      return false;
    }

  return true;
}

bool solveLinear(const DMatZZpFFPACK& A,
                 const DMatZZpFFPACK& B,
                 DMatZZpFFPACK& X)
{
  return solveLinear(A, B, true, X, false);
}

M2_arrayintOrNull rankProfile(const DMatZZpFFPACK& mat, bool row_profile)

{
  DMatZZpFFPACK N(mat);

  size_t* prof;  // this is where the result will be placed
  size_t rk;
  if (row_profile)
    rk = FFPACK::RowRankProfile(mat.ring().field(),
                                mat.numRows(),
                                mat.numColumns(),
                                N.array(),
                                mat.numColumns(),
                                prof);
  else
    rk = FFPACK::ColumnRankProfile(mat.ring().field(),
                                   mat.numRows(),
                                   mat.numColumns(),
                                   N.array(),
                                   mat.numColumns(),
                                   prof);

  M2_arrayint profile = M2_makearrayint(static_cast<int>(rk));
  for (size_t i = 0; i < rk; i++) profile->array[i] = static_cast<int>(prof[i]);

  delete[] prof;
  return profile;
}

void rankProfile(const DMatZZpFFPACK& mat,
                 bool row_profile,
                 std::vector<size_t>& result_profile)

{
  DMatZZpFFPACK N(mat);

  size_t* prof;  // this is where the result will be placed
  size_t rk;
  if (row_profile)
    rk = FFPACK::RowRankProfile(mat.ring().field(),
                                mat.numRows(),
                                mat.numColumns(),
                                N.array(),
                                mat.numColumns(),
                                prof);
  else
    rk = FFPACK::ColumnRankProfile(mat.ring().field(),
                                   mat.numRows(),
                                   mat.numColumns(),
                                   N.array(),
                                   mat.numColumns(),
                                   prof);

  result_profile.resize(0);
  for (size_t i = 0; i < rk; i++) result_profile.push_back(prof[i]);

  delete[] prof;
}

};  // namespace ffpackInterface

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