Start of matrices

main
Micke Nordin 2 years ago
parent b26517584c
commit 8e5bc40d6d
Signed by: micke
GPG Key ID: 014B273D614BE877

@ -0,0 +1,20 @@
#include "matrix.hpp"
#include "../complex-numbers/cnumber.hpp"
#include "../vectors/vector.hpp"
int main() {
vector v = vector(2);
v[0] = cnumber(1, 0);
v[1] = cnumber(0, 1);
vector w = vector(2);
w[0] = cnumber(1, 0);
w[1] = cnumber(0, -1);
matrix m = matrix(2, 2);
m[0] = v;
m[1] = w;
cout << m << endl;
return 0;
}

@ -0,0 +1,58 @@
#include "../vectors/vector.hpp"
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
class matrix {
private:
long long num_rows;
long long num_cols;
vector *rows;
public:
matrix(const long long num_rows, const long long num_cols) {
this->num_rows = num_rows;
this->num_cols = num_cols;
this->rows = (vector *)calloc(sizeof(vector), num_rows);
for (long long i = 0; i < num_rows; i++) {
this->rows[i] = vector(num_cols);
}
}
~matrix() {
this->num_rows = 0;
this->num_cols = 0;
free(this->rows);
this->rows = NULL;
}
friend ostream &operator<<(ostream &os, const matrix &m) {
char last = '\0';
for (long long i = 0; i < m.num_rows; i++) {
for (long long j = 0; j < m.num_cols; j++) {
string symbols[3];
symbols[0] = "|";
ostringstream oss;
oss << " " << m.rows[i][j] << " ";
symbols[1] = oss.str();
symbols[2] = "|";
for (int i = 0; i < 3; i++) {
int len = symbols[i].length() - 1;
char cur = symbols[i][0];
if (cur != last) {
os << symbols[i];
}
last = symbols[i][len];
}
}
if (i != m.num_rows - 1)
os << endl << "|";
}
return os;
}
const vector operator[](const long long index) const {
return this->rows[index];
}
// FIXME: Figure out how to make sure you do not try to access something
// outside of the index
vector &operator[](const long long index) { return this->rows[index]; }
};
Loading…
Cancel
Save