Added matrix code
This commit is contained in:
parent
179cbc234b
commit
fa54698850
@ -3,6 +3,5 @@
|
|||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
164
XMath/Matrix.cpp
Normal file
164
XMath/Matrix.cpp
Normal file
@ -0,0 +1,164 @@
|
|||||||
|
#define MATRIX_API
|
||||||
|
#include "Matrix.h"
|
||||||
|
|
||||||
|
namespace CryptoCPP {
|
||||||
|
namespace Math {
|
||||||
|
|
||||||
|
MATRIX_API Vector::Vector(size_t count) : count(count), valueSet(new long long[count])
|
||||||
|
{
|
||||||
|
memset(valueSet, 0, count * sizeof(long long));
|
||||||
|
}
|
||||||
|
|
||||||
|
MATRIX_API Vector::Vector(size_t count, long long * values) : count(count), valueSet(new long long[count])
|
||||||
|
{
|
||||||
|
memcpy(valueSet, values, count * sizeof(long long));
|
||||||
|
}
|
||||||
|
|
||||||
|
MATRIX_API Vector::~Vector()
|
||||||
|
{
|
||||||
|
delete[] valueSet;
|
||||||
|
}
|
||||||
|
|
||||||
|
MATRIX_API long long Vector::at(size_t index) const
|
||||||
|
{
|
||||||
|
if (index < 0 || index >= count) throw new std::exception("Index out of bounds");
|
||||||
|
return valueSet[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
MATRIX_API long long Vector::at(size_t index, long long newval)
|
||||||
|
{
|
||||||
|
if (index < 0 || index >= count) throw new std::exception("Index out of bounds");
|
||||||
|
long long l = valueSet[index];
|
||||||
|
valueSet[index] = newval;
|
||||||
|
return l;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
DelegatingFPTR::DelegatingFPTR(Delegate impl, Matrix* context)
|
||||||
|
{
|
||||||
|
this->impl = impl;
|
||||||
|
this->context = context;
|
||||||
|
}
|
||||||
|
|
||||||
|
MATRIX_API const DelegatingFPTR* DelegatingFPTR::operator()(const Vector & input, size_t index)
|
||||||
|
{
|
||||||
|
return (context->*impl)(input, index);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Matrix::Matrix(size_t height, size_t width) : columns(new Vector*[width]), width(width), height(height)
|
||||||
|
{
|
||||||
|
for (size_t t = 0; t < width; ++t)
|
||||||
|
columns[t] = new Vector(height);
|
||||||
|
}
|
||||||
|
|
||||||
|
Matrix::Matrix(const Matrix & copy) : columns(new Vector*[copy.width]), width(copy.width), height(copy.height)
|
||||||
|
{
|
||||||
|
for (size_t t = 0; t < width; ++t)
|
||||||
|
for (size_t t1 = 0; t < height; ++t1)
|
||||||
|
columns[t]->at(t1, copy.columns[t]->at(t1));
|
||||||
|
}
|
||||||
|
|
||||||
|
MATRIX_API Matrix::~Matrix() {
|
||||||
|
delete ar;
|
||||||
|
delete ac;
|
||||||
|
|
||||||
|
for (size_t t = 0; t < width; ++t)
|
||||||
|
delete columns[t];
|
||||||
|
delete[] columns;
|
||||||
|
// TODO: Add other deletions
|
||||||
|
}
|
||||||
|
|
||||||
|
MATRIX_API const DelegatingFPTR* Matrix::set_row(const Vector & row, size_t rowidx)
|
||||||
|
{
|
||||||
|
if (rowidx < 0 || rowidx >= height) throw new std::exception("Index out of bounds");
|
||||||
|
size_t min = row.count < width ? row.count : width;
|
||||||
|
for (size_t t = 0; t < min; ++t) columns[t]->at(rowidx, row.at(t));
|
||||||
|
return ar;
|
||||||
|
}
|
||||||
|
|
||||||
|
MATRIX_API const DelegatingFPTR* Matrix::set_col(const Vector & col, size_t colidx)
|
||||||
|
{
|
||||||
|
return ac;
|
||||||
|
}
|
||||||
|
|
||||||
|
MATRIX_API long long Matrix::set_at(size_t col, size_t row, long long value)
|
||||||
|
{
|
||||||
|
if (col < 0 || col >= width || row < 0 || row >= height) throw new std::exception("Index out of bounds");
|
||||||
|
columns[col]->at(row, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
MATRIX_API long long Matrix::set_at(size_t index, bool rowMajor, long long value)
|
||||||
|
{
|
||||||
|
return set_at(rowMajor ? index % width : index / height, rowMajor ? index / width : index % height, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
MATRIX_API Vector * Matrix::at_row(size_t index) const
|
||||||
|
{
|
||||||
|
if (index < 0 || index >= height) throw new std::exception("Index out of bounds");
|
||||||
|
Vector * collect = new Vector(width);
|
||||||
|
for (size_t t = 0; t < width; ++t)
|
||||||
|
collect->at(t, columns[t]->at(index));
|
||||||
|
return collect;
|
||||||
|
}
|
||||||
|
|
||||||
|
MATRIX_API Vector * Matrix::at_col(size_t index) const
|
||||||
|
{
|
||||||
|
if (index < 0 || index >= width) throw new std::exception("Index out of bounds");
|
||||||
|
Vector * collect = new Vector(height);
|
||||||
|
for (size_t t = 0; t < height; ++t)
|
||||||
|
collect->at(t, columns[index]->at(t));
|
||||||
|
return collect;
|
||||||
|
}
|
||||||
|
|
||||||
|
MATRIX_API long long Matrix::at(size_t col, size_t row) const
|
||||||
|
{
|
||||||
|
if (col < 0 || col >= width || row < 0 || row >= height) throw new std::exception("Index out of bounds");
|
||||||
|
return columns[col]->at(row);
|
||||||
|
}
|
||||||
|
|
||||||
|
MATRIX_API long long Matrix::at(size_t index, bool rowMajor) const
|
||||||
|
{
|
||||||
|
return at(rowMajor ? index % width : index / height, rowMajor ? index / width : index % height);
|
||||||
|
}
|
||||||
|
|
||||||
|
MATRIX_API Matrix * Matrix::mul(const Matrix & factor) const
|
||||||
|
{
|
||||||
|
if (factor.height != width) throw new std::exception("Mismatched dimensions");
|
||||||
|
Matrix* result = new Matrix(height, factor.width);
|
||||||
|
for (int i = 0; i < factor.width; ++i)
|
||||||
|
for (int j = 0; j < height; ++j)
|
||||||
|
for (int k = 0; k < width; ++k)
|
||||||
|
result->columns[i]->at(j, result->columns[i]->at(j) + (factor.columns[i]->at(k) * columns[k]->at(j)));
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
MATRIX_API Matrix * Matrix::mul(long long scalar) const
|
||||||
|
{
|
||||||
|
Matrix* m = new Matrix(*this);
|
||||||
|
for (size_t t = 0; t < width; ++t)
|
||||||
|
for (size_t t1 = 0; t < height; ++t1)
|
||||||
|
m->columns[t]->at(t1, m->columns[t]->at(t1) * scalar);
|
||||||
|
return m;
|
||||||
|
}
|
||||||
|
|
||||||
|
MATRIX_API Matrix * Matrix::minor(size_t row, size_t col) const
|
||||||
|
{
|
||||||
|
Matrix* result = new Matrix(height - 1, width - 1);
|
||||||
|
for (int i = 0; i < width; ++i) {
|
||||||
|
if (i == col) continue;
|
||||||
|
for (int j = 0; j < height; ++j) {
|
||||||
|
if (j == row) continue;
|
||||||
|
result->columns[i + ((i > col) ? -1 : 0)]->at(j + ((j > row) ? -1 : 0), columns[i]->at(j));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
MATRIX_API long long Matrix::det() const
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -11,20 +11,56 @@
|
|||||||
|
|
||||||
namespace CryptoCPP {
|
namespace CryptoCPP {
|
||||||
namespace Math {
|
namespace Math {
|
||||||
|
class Matrix;
|
||||||
|
|
||||||
|
class Vector
|
||||||
|
{
|
||||||
|
friend class Matrix;
|
||||||
|
public:
|
||||||
|
MATRIX_API Vector(size_t count);
|
||||||
|
MATRIX_API Vector(size_t count, long long * values);
|
||||||
|
MATRIX_API ~Vector();
|
||||||
|
|
||||||
|
MATRIX_API long long at(size_t index) const;
|
||||||
|
MATRIX_API long long at(size_t index, long long newval);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
const size_t count;
|
||||||
|
long long * const valueSet;
|
||||||
|
};
|
||||||
|
|
||||||
|
class DelegatingFPTR;
|
||||||
|
typedef const DelegatingFPTR*(Matrix::*Delegate)(const Vector & input, size_t at);
|
||||||
|
|
||||||
|
class DelegatingFPTR {
|
||||||
|
public:
|
||||||
|
|
||||||
|
DelegatingFPTR(Delegate impl, Matrix* context);
|
||||||
|
|
||||||
|
MATRIX_API const DelegatingFPTR* operator()(const Vector & input, size_t index);
|
||||||
|
protected:
|
||||||
|
Delegate impl;
|
||||||
|
Matrix* context;
|
||||||
|
};
|
||||||
|
|
||||||
class Matrix
|
class Matrix
|
||||||
{
|
{
|
||||||
|
friend class Vector;
|
||||||
|
friend class DelegatingFPTR;
|
||||||
public:
|
public:
|
||||||
MATRIX_API typedef void(*ChainFunction) (Vector & valueSet);
|
|
||||||
|
|
||||||
MATRIX_API Matrix(size_t height, size_t width);
|
MATRIX_API Matrix(size_t height, size_t width);
|
||||||
MATRIX_API Matrix(Matrix & copy);
|
MATRIX_API Matrix(const Matrix & copy);
|
||||||
|
MATRIX_API ~Matrix();
|
||||||
|
|
||||||
MATRIX_API ChainFunction set_row(const Vector & row);
|
MATRIX_API const DelegatingFPTR* set_row(const Vector & row, size_t rowidx);
|
||||||
MATRIX_API ChainFunction set_col(const Vector & col);
|
MATRIX_API const DelegatingFPTR* set_col(const Vector & col, size_t colidx);
|
||||||
|
MATRIX_API long long set_at(size_t col, size_t row, long long value);
|
||||||
|
MATRIX_API long long set_at(size_t index, bool rowMajor, long long value);
|
||||||
|
|
||||||
MATRIX_API Vector* at_row(size_t index) const;
|
MATRIX_API Vector* at_row(size_t index) const;
|
||||||
MATRIX_API Vector* at_col(size_t index) const;
|
MATRIX_API Vector* at_col(size_t index) const;
|
||||||
MATRIX_API long long at(size_t row, size_t col) const;
|
MATRIX_API long long at(size_t col, size_t row) const;
|
||||||
|
MATRIX_API long long at(size_t index, bool rowMajor) const;
|
||||||
|
|
||||||
MATRIX_API Matrix* mul(const Matrix & factor) const;
|
MATRIX_API Matrix* mul(const Matrix & factor) const;
|
||||||
MATRIX_API Matrix* mul(long long scalar) const;
|
MATRIX_API Matrix* mul(long long scalar) const;
|
||||||
@ -33,24 +69,11 @@ namespace CryptoCPP {
|
|||||||
MATRIX_API long long det() const;
|
MATRIX_API long long det() const;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Vector * columns;
|
const DelegatingFPTR* ar = new DelegatingFPTR(add_row, this);
|
||||||
size_t height;
|
const DelegatingFPTR* ac = new DelegatingFPTR(add_col, this);
|
||||||
size_t width;
|
Vector * * const columns;
|
||||||
|
const size_t height;
|
||||||
};
|
const size_t width;
|
||||||
|
|
||||||
class Vector
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
MATRIX_API Vector(size_t count);
|
|
||||||
MATRIX_API Vector(size_t count, long long * values);
|
|
||||||
|
|
||||||
MATRIX_API long long at(size_t index) const;
|
|
||||||
MATRIX_API long long at(size_t index, long long newval);
|
|
||||||
|
|
||||||
protected:
|
|
||||||
long double * valueSet;
|
|
||||||
size_t count;
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -122,6 +122,7 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="BigInteger.cpp" />
|
<ClCompile Include="BigInteger.cpp" />
|
||||||
|
<ClCompile Include="Matrix.cpp" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||||
<ImportGroup Label="ExtensionTargets">
|
<ImportGroup Label="ExtensionTargets">
|
||||||
|
@ -18,6 +18,9 @@
|
|||||||
<ClCompile Include="BigInteger.cpp">
|
<ClCompile Include="BigInteger.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="Matrix.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="BigInteger.h">
|
<ClInclude Include="BigInteger.h">
|
||||||
|
Loading…
x
Reference in New Issue
Block a user