File: linbox-sage.C

package info (click to toggle)
linbox 1.5.2-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 13,640 kB
  • sloc: cpp: 106,991; lisp: 5,469; makefile: 1,293; sh: 1,049; csh: 95; python: 74; perl: 2
file content (176 lines) | stat: -rw-r--r-- 4,772 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
/* linbox-sage.C
 * Copyright (C) 2007 Martin Albrecht
 *               2008 Clement Pernet
 *
 * Written by Martin Albrecht
 *            Clement Pernet <clement.pernet@gmail.com>
 *
 * ========LICENCE========
 * This file is part of the library LinBox.
 *
  * LinBox is free software: you can redistribute it and/or modify
 * it under the terms of the  GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * ========LICENCE========
 */

#include <iostream>

#include <cstdlib>
#include <vector>
#include <list>
#include "linbox/linbox-config.h"
#include "linbox/util/commentator.h"

#include "linbox/matrix/sparse-matrix.h"
#include "linbox/vector/sparse.h"

#include "linbox/matrix/matrix-domain.h"
#include "linbox/algorithms/gauss.h"
#include "linbox/solutions/rank.h"
#include "linbox/solutions/solve.h"
#include "linbox/solutions/methods.h"
#include "linbox/integer.h"

#include <gmp.h>

#include "linbox-sage.h"

using namespace LinBox;


/*************************************************************************
  sparse modulo Z/nZ
 *************************************************************************/

struct c_vector_modint_linbox {
	// copy of the declaration in vector_modn_sparse.pxi
	int *entries;
	int p;
	size_t *positions;
	size_t degree;
	size_t num_nonzero;
};

typedef unsigned int mod_int;
typedef Givaro::Modular<unsigned int> GFp;
typedef GFp::Element  GFpElement;
typedef std::vector <std::pair <size_t, GFpElement> > SparseSeqVectorGFp;
typedef SparseMatrix<GFp, VectorTraits<SparseSeqVectorGFp>::SparseFormat> SparseMatrixGFp;

SparseMatrixGFp* linbox_new_modn_sparse_matrix(mod_int modulus, size_t numrows, size_t numcols, void *rows)
{
	GFp *F=new GFp(modulus);
	SparseMatrixGFp* M=new SparseMatrixGFp(*F, numrows, numcols);

	struct c_vector_modint_linbox *A = static_cast<struct c_vector_modint_linbox *>(rows);

	for(size_t i = 0; i < numrows; ++i) {
		for(size_t j = 0; j < A[i].num_nonzero; ++j) {
                        M->setEntry(i, A[i].positions[j], A[i].entries[j]);
		}
	}
	return M;
}

void linbox_delete_modn_sparse_matrix (SparseMatrixGFp * A) {
        const GFp * F = &A->field();
        delete A;
        delete F;
}

static std::vector<GFpElement> linbox_new_modn_sparse_vector(mod_int modulus, size_t len, void *_vec)
{
	std::vector<GFpElement> A(len);

	if (_vec==NULL) {
		return A;
	}

	struct c_vector_modint_linbox *vec = static_cast<struct c_vector_modint_linbox*>(_vec);
	for(size_t i = 0; i < vec->num_nonzero; ++i) {
		A[vec->positions[i]] = vec->entries[i];
	}
	return A;
}

unsigned long linbox_modn_sparse_matrix_rank(mod_int modulus,
					     size_t numrows, size_t numcols,
					     void *rows, int gauss)
{
	unsigned long M_rank;
	GFpElement M_det;

	SparseMatrixGFp *M = linbox_new_modn_sparse_matrix(modulus, numrows, numcols, rows) ;
	const GFp &F = M->field();
	GaussDomain<GFp> dom(F);

	if(!gauss) {
		dom.InPlaceLinearPivoting(M_rank, M_det, *M, numrows, numcols);
	}
	else {
		dom.NoReordering(M_rank, M_det, *M, numrows, numcols);
	}

	//*pivots = (int*)calloc(sizeof(int), dom.pivots.size());

	//   int j=0;
	//   for(std::vector<int>::const_iterator i= dom.pivots.begin(); i!= dom.pivots.end(); ++i, ++j){
	//     (*pivots)[j] = *i;
	//   }
        linbox_delete_modn_sparse_matrix(M);
	return M_rank;
}

std::vector<mod_int> linbox_modn_sparse_matrix_solve(mod_int p, size_t numrows, size_t numcols,
						     void *_a, void *b, int method)
{
	// solve ax = b, for x, a matrix, b vector, x vector

	SparseMatrixGFp *A =linbox_new_modn_sparse_matrix(p, numrows, numcols, _a);

	const GFp & F = A->field();

        DenseVector<GFp> X(F, numrows);
        DenseVector<GFp> B(F, linbox_new_modn_sparse_vector(p, numcols, b) );

	switch(method) {
	case 1:
		solve(X, *A, B, Method::BlasElimination());
		break;

	case 2:
		solve(X, *A, B, Method::Blackbox());
		break;

	case 3:
		solve(X, *A, B, Method::Wiedemann());
		break;

	default:
		solve(X, *A, B);
	}

        linbox_delete_modn_sparse_matrix(A);

	return X.refRep();
}

// vim:sts=8:sw=8:ts=8:noet:sr:cino=>s,f0,{0,g0,(0,:0,t0,+0,=s
// Local Variables:
// mode: C++
// tab-width: 8
// indent-tabs-mode: nil
// c-basic-offset: 8
// End: