forked from openfheorg/openfhe-logreg-training-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pt_matrix.cpp
233 lines (202 loc) · 6.75 KB
/
pt_matrix.cpp
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
//==================================================================================
// BSD 2-Clause License
//
// Copyright (c) 2023, Duality Technologies Inc.
//
// All rights reserved.
//
// Author TPOC: [email protected]
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//==================================================================================
#include "pt_matrix.h"
/* Multiplies a matrix A with a scalar value t, in place.
*/
void MatrixScalarMult(Mat &A, prim_type t) {
for (usint i = 0; i < A.size(); i++) {
for (usint j = 0; j < A[i].size(); j++) {
A[i][j] = t * A[i][j];
}
}
}
void ScalarSubMat(prim_type t, Mat &A, Mat &B){
for (usint i = 0; i < A.size(); i++) {
for (usint j = 0; j < A[i].size(); j++) {
B[i][j] = t - A[i][j];
}
}
}
void MatrixMatrixAdd(Mat &A, Mat &B, Mat &C){
if (A.size() != B.size()|| B.size() != C.size()){
throw std::invalid_argument("MatrixMatrixAdd A B and C must all have same leading dimension");
}
if (A[0].size() != B[0].size() || B[0].size() != C[0].size()) {
throw std::invalid_argument("MatrixMatrixAdd A B and C must all have same trailing dimension");
}
for (usint i = 0; i < A.size(); i++) {
for (usint j = 0; j < A[i].size(); j++) {
C[i][j] = A[i][j] + B[i][j];
}
}
}
void MatrixMatrixSub(Mat &A, Mat &B, Mat &C) {
if (A.size() != B.size() || B.size() != C.size()) {
throw std::invalid_argument("MatrixMatrixAdd A B and C must all have same leading dimension");
}
if (A[0].size() != B[0].size() || B[0].size() != C[0].size()) {
throw std::invalid_argument("MatrixMatrixAdd A B and C must all have same trailing dimension");
}
for (usint i = 0; i < A.size(); i++) {
for (usint j = 0; j < A[i].size(); j++) {
C[i][j] = A[i][j] - B[i][j];
}
}
}
/* Applies the sigmoid function on a matrix A, in place.
*/
void MatrixSigmoid(Mat &A) {
for (usint i = 0; i < A.size(); i++) {
for (usint j = 0; j < A[i].size(); j++) {
A[i][j] = prim_type(1) / (prim_type(1.0) + exp(-A[i][j]));
}
}
}
void MatrixLog(Mat &A, Mat &B) {
for (usint i = 0; i < A.size(); i++) {
for (usint j = 0; j < A[i].size(); j++) {
B[i][j] = std::log(A[i][j]);
}
}
}
/* Prints matrix A.
*/
void PrintMatrix(const Mat &A) {
//set the output precision to double max digits.
std::cerr.precision(dbl::max_digits10);
for (usint i = 0; i < A.size(); i++) {
std::cerr << "[ ";
for (usint j = 0; j < A[i].size(); j++) {
std::cerr << A[i][j] << ", ";
}
std::cerr << " ]" << std::endl;
}
}
/* Prints Submatrix of A. 0..nrow-1 x 0..ncol-1
*/
void PrintSubmatrix(const Mat &A, const unsigned int nrow, const unsigned int ncol) {
//set the output precision to double max digits.
std::cerr.precision(dbl::max_digits10);
usint nr = std::min(nrow, usint(A.size()));
usint nc = std::min(ncol, usint(A[0].size()));
for (usint i = 0; i < nr; i++) {
std::cerr << "[ ";
for (usint j = 0; j < nc; j++) {
std::cerr << A[i][j] << ", ";
}
std::cerr << " ]" << std::endl;
}
}
void MatrixMult(const Mat &A, const Mat &B, Mat &C) {
auto numRows = A.size();
auto numCols = B[0].size();
auto middleDim = A[0].size();
if (middleDim != B.size()) {
throw std::invalid_argument(" Matrixmult: Input Dimension mismatch");
}
if ((numRows != C.size()) || (numCols != C[0].size())) {
throw std::invalid_argument(" Matrixmult: Output Dimension mismatch");
}
for (auto i = 0U; i < numRows; i++) {
for (auto j = 0U; j < numCols; j++) {
for (auto k = 0U; k < middleDim; k++) {
// C[i][j] += A[i][k] * B[k][j];
C.at(i).at(j) += A.at(i).at(k) * B.at(k).at(j);
}
}
}
}
void MatrixTransp(const Mat &A, Mat &AT) {
auto numRowsA = A.size();
auto numColsA = A[0].size();
if ((numRowsA != AT[0].size()) || (numColsA != AT.size())) {
throw std::invalid_argument(" MatrixTransp: Output Dimension mismatch");
}
for (auto i = 0U; i < numRowsA; i++) {
for (auto j = 0U; j < numColsA; j++) {
// AT[j][i] += A[i][j];
AT.at(j).at(i) += A.at(i).at(j);
}
}
}
void InvertMatrix(Mat &x) {
int dim = x.size();
if (dim <= 0) return; // sanity check
if (dim == 1) return; // must be of dimension >= 2
for (int i = 1; i < dim; i++) x[0][i] /= x[0][0]; // normalize row 0
for (int i = 1; i < dim; i++) {
for (int j = i; j < dim; j++) { // do a column of L
prim_type sum = 0.0;
for (int k = 0; k < i; k++) {
sum += x[j][k] * x[k][i];
}
x[j][i] -= sum;
}
if (i == dim - 1) continue;
for (int j = i + 1; j < dim; j++) { // do a row of U
prim_type sum = 0.0;
for (int k = 0; k < i; k++) {
sum += x[i][k] * x[k][j];
}
x[i][j] = (x[i][j] - sum) / x[i][i];
}
}
for (int i = 0; i < dim; i++) { // invert L
for (int j = i; j < dim; j++) {
prim_type tmp = 1.0;
if (i != j) {
tmp = 0.0;
for (int k = i; k < j; k++)
tmp -= x[j][k] * x[k][i];
}
x[j][i] = tmp / x[j][j];
}
}
for (int i = 0; i < dim; i++) { // invert U
for (int j = i; j < dim; j++) {
if (i == j) continue;
prim_type sum = 0.0;
for (int k = i; k < j; k++) {
sum += x[k][j] * ((i == k) ? 1.0 : x[i][k]);
}
x[i][j] = -sum;
}
}
for (int i = 0; i < dim; i++) { // final inversion
for (int j = 0; j < dim; j++) {
prim_type sum = 0.0;
for (int k = ((i > j) ? i : j); k < dim; k++) {
sum += ((j == k) ? 1.0 : x[j][k]) * x[k][i];
}
x[j][i] = sum;
}
}
}