Base version with linear combinated classes

This commit is contained in:
2019-12-13 18:17:29 +03:00
parent d3b1fc754a
commit 30ddecfa3c
3 changed files with 191 additions and 8 deletions

122
al_bool_matrix.hpp Normal file
View File

@@ -0,0 +1,122 @@
#ifndef _AL_BOOL_MATRIX_HPP
#define _AL_BOOL_MATRIX_HPP
#include <cassert>
#include <cstdint>
#include <bitset>
#include <limits>
#include <string>
template <class STORAGE, size_t SIZE>
class BoolVector {
public:
BoolVector(): _value(0) {
static_assert(not std::numeric_limits<STORAGE>::is_signed);
}
BoolVector(STORAGE val): _value(val) {
static_assert(not std::numeric_limits<STORAGE>::is_signed);
if ( val > 1ll << SIZE) {
std::cout << "here " << std::endl;
}
}
BoolVector operator*(BoolVector other) const {
return BoolVector(_value & other.get_value());
}
STORAGE get_value() const {
return _value;
}
STORAGE at(STORAGE ind) const {
return (_value >> ind) % 2;
}
std::string string() const {
std::string res(SIZE, '0');
for (size_t ind = 0; ind < SIZE; ++ind) {
char cur_ch = at(ind) % 2 == 0 ? '0' : '1';
res[ind] = cur_ch;
}
return res;
}
private:
STORAGE _value;
};
template<class STORAGE, size_t SIZE>
class BoolSquareMatrix {
public:
BoolSquareMatrix(): _values(0) {
static_assert(not std::numeric_limits<STORAGE>::is_signed);
}
BoolSquareMatrix(STORAGE val): _values(val) {
static_assert(not std::numeric_limits<STORAGE>::is_signed);
}
STORAGE at(STORAGE row, STORAGE col) const {
return (_values >> index(row, col)) % 2;
}
STORAGE get_determinant() {
if ( SIZE == 2 ) {
return at(0,0) * at(1,1) - at(0,1) * at(1,0);
} else if ( SIZE == 3 ) {
return at(0,0) * at(1,1) * at(2,2) + at(0,1) * at(1,2) * at(2,0)
+ at(0,2) * at(1,0) * at(2,1) - at(0,2) * at(1,1) * at(2,0)
- at(0,1) * at(1,0) * at(2,2) - at(0,0) * at(1,2) * at(2,1);
}
}
BoolVector<STORAGE, SIZE> operator * (BoolVector<STORAGE, SIZE> vec) const {
/* a_1
* a_2
* a_3
*/
STORAGE vector_value = 0;
for (STORAGE ind = 0; ind < SIZE; ++ind) {
BoolVector<STORAGE, SIZE> cur_vec = vec * get_row(ind);
STORAGE cur_sum = 0;
for (STORAGE vec_ind = 0; vec_ind < SIZE; ++vec_ind)
cur_sum += cur_vec.at(vec_ind);
vector_value *= 2;
vector_value += cur_sum % 2;
}
return BoolVector<STORAGE, SIZE>(vector_value);
}
STORAGE get_value() const {
return _values;
}
std::string string(std::string delimiter="\n") const {
std::string res;
for (STORAGE i = 0; i < SIZE; ++i) {
for (STORAGE j = 0; j < SIZE; ++j) {
char cur_ch = at(i,j) % 2 == 0 ? '0' : '1';
res += cur_ch;
}
if ( i != SIZE - 1 )
res += delimiter;
}
return res;
}
bool operator < (BoolSquareMatrix other) const {
return _values < other._values;
}
bool operator == (BoolSquareMatrix other) const {
return _values == other._values;
}
private:
STORAGE _values;
STORAGE index(STORAGE row, STORAGE col) const {
return SIZE * row + col;
}
BoolVector<STORAGE, SIZE> get_row(STORAGE row) const {
STORAGE vector_value = 0;
for (STORAGE i = 0; i < SIZE; ++i) {
vector_value *= 2;
vector_value += at(row, i);
}
return BoolVector<STORAGE, SIZE>(vector_value);
}
};
#endif // _AL_BOOL_MATRIX_HPP