4
Copyright (c) 2022 МГТУ им. Н.Э. Баумана, кафедра ИУ-6, Михаил Фетисов,
6
https://bmstu.codes/lsx/simodo/loom
9
#include "simodo/variable/matrix_operations.h"
10
#include "simodo/bormental/DrBormental.h"
11
#include "simodo/inout/format/fmt.h"
16
namespace simodo::variable
18
void minusMatrix(Value & matrix_value)
20
std::shared_ptr<Array> matrix = matrix_value.getArray();
21
for(Value & v : matrix->values_mutable())
23
v.setVariant(-v.getInt());
25
v.setVariant(-v.getFloat());
27
throw bormental::DrBormental("minusMatrix",
28
inout::fmt("Invalid operation for a matrix element with type %1")
29
.arg(getValueTypeName(v.type())));
32
void additionMatrixMatrix(Value & matrix_value_1, const Value & matrix_value_2)
34
std::shared_ptr<Array> m1 = matrix_value_1.getArray();
35
std::shared_ptr<Array> m2 = matrix_value_2.getArray();
37
if (m1->values().size() != m2->values().size())
38
throw bormental::DrBormental("additionMatrixMatrix",
39
inout::fmt("Inconsistent dimension of matrices"));
41
for(size_t i=0; i < m1->values().size(); ++i) {
42
Value & e1 = m1->values_mutable().at(i);
43
const Value & e2 = m2->values_mutable().at(i);
46
e1.setVariant(e2.variant());
48
e1.setVariant(e1.getInt() + e2.getInt());
49
else if (e1.isFloat())
50
e1.setVariant(e1.getFloat() + e2.getFloat());
51
else if (!e1.isNull())
52
throw bormental::DrBormental("additionMatrixMatrix",
53
inout::fmt("Invalid operation for a matrix element with type %1")
54
.arg(getValueTypeName(e1.type())));
58
void multiplicationMatrixScalar(Value & matrix_value, const Value & scalar_value)
60
std::shared_ptr<Array> matrix = matrix_value.getArray();
61
for(Value & v : matrix->values_mutable())
63
v.setVariant(v.getInt() * scalar_value.getInt());
65
v.setVariant(v.getFloat() * scalar_value.getFloat());
67
throw bormental::DrBormental("multiplicationMatrixScalar",
68
inout::fmt("Invalid operation for a matrix element with type %1")
69
.arg(getValueTypeName(v.type())));
72
void multiplicationMatrixMatrix(Value & /*matrix_value_1*/, const Value & /*matrix_value_2*/)
74
throw bormental::DrBormental("multiplicationMatrixMatrix",
75
inout::fmt("Matrix multiplication operations are not supported"));
78
void inverseMatrix(Value & /*matrix_value*/)
80
throw bormental::DrBormental("inverseMatrix",
81
inout::fmt("Matrix reversal operations are not supported"));
84
double lengthVector(const Value & vector_value)
87
std::shared_ptr<Array> m = vector_value.getArray();
89
for(size_t i=0; i < m->values().size(); ++i) {
90
Value & e = m->values_mutable().at(i);
95
res += double(e.getInt() * e.getInt());
97
res += e.getFloat() * e.getFloat();
99
throw bormental::DrBormental("lengthVector",
100
inout::fmt("Invalid operation for a vector element with type %1")
101
.arg(getValueTypeName(e.type())));
104
return std::sqrt(res);