Cleaned up ChrisTest, added some Matrix functionality in C++, waiting for templates

This commit is contained in:
Chris Fadden
2015-05-04 12:35:31 -04:00
parent 08431aa748
commit 647a6fe20b
6 changed files with 284 additions and 129 deletions

View File

@@ -1,84 +0,0 @@
#include <stdlib.h>
/*********************
* Matrix Transpose
*
* No real concept of vector transpose
* in this kraken implementation
* *******************/
void transpose(int m, int n, double** A)
{
int i, j;
/******************************
* Allocate memory for tmp array
* ***************************/
double** A_trans;
A_trans = malloc(sizeof(double*) * m);
for(i = 0; i < m; i++)
{
A_trans[i] = malloc(sizeof(double) * n);
}
/*********************
* Copy A into tmp
********************/
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
A_trans[i][j] = A[i][j];
}//end j loop
}//end i loop
/***************
* Transpose operation
***************/
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
A[i][j] = A_trans[j][i];
}//end j loop
}//end i loop
/**********
* Free Memory
* *******/
for(i = 0; i < m; i++)
free(A_trans[i]);
free(A_trans);
}//end transpose function
void multiply(int m, int n, int q, double** A, double** B, double**C)
{
//Kraken: check if C is empty, then allocate memory
return;
}
void printMatrix(int m, int n, double** A)
{
int i, j;
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
printf("%f ",A[i][j]);
}//end j loop
printf("\n");
}//end i loop
printf("\n");
}//end print function

View File

@@ -1,31 +0,0 @@
#include <stdio.h>
#include "ChrisMatrix.h"
int main(int argc, char** argv)
{
double** A;
int m = 3;
int n = 3;
int i, j;
A = malloc(sizeof(double*) * m);
for(i = 0; i < m; i++)
A[i] = malloc(sizeof(double) * n);
int k = 0;
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
A[i][j] = k;
k++;
}//end j loop
}//end i loop
printMatrix(m, n, A);
transpose(m,n,A);
printMatrix(m,n,A);
printf("Hello World\n");
return 0;
}

View File

@@ -0,0 +1,230 @@
#include <iostream>
#include <vector>
class Matrix {
public:
// default constructor
Matrix() {
rows = 0;
cols = 0;
}
// standard sizes
Matrix(const int m, const int n) {
rows = m;
cols = n;
data.resize(rows * cols);
for (int i = 0; i < rows * cols; i++) {
data[i] = i;
}
}
double at(const int i, const int j) {
int index = i * rows + j;
if (index > rows * cols) {
std::cout << "index (" << i << ", " << j << ") out of bounds"
<< std::endl;
std::cout << "Max index = (" << rows - 1 << ", " << cols - 1 << ")"
<< std::endl;
}
return data[index];
}
void getSize(int &m, int &n) {
m = rows;
n = cols;
return;
}
void resize(const int m, const int n) {
rows = m;
cols = n;
data.resize(rows * cols);
return;
}
void set(const int i, const int j, const double val) {
int index = i * rows + j;
if (index > rows * cols) {
std::cout << "index (" << i << ", " << j << ") out of bounds"
<< std::endl;
std::cout << "Max index = (" << rows - 1 << ", " << cols - 1 << ")"
<< std::endl;
}
data[index] = val;
return;
}
void transpose() {
double val1, val2;
for (int n = 0; n <= rows - 2; n++)
for (int m = n + 1; m <= rows - 1; m++) {
val1 = at(n, m);
val2 = at(m, n);
set(n, m, val2);
set(m, n, val1);
}
return;
} // end transpose
void copy(Matrix &C) {
C.resize(rows, cols);
double val;
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++) {
val = at(i, j);
C.set(i, j, val);
}
} // end copy
void inverse() {
std::vector<std::vector<double> > a(2 * rows + 1,
std::vector<double>(2 * rows + 1, 0));
int n = rows;
double d, val;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) a[i][j] = at(i - 1, j - 1);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= 2 * n; j++)
if (j == (i + n)) a[i][j] = 1;
/****************
* Partial Pivot
***************/
for (int i = n; i > 1; i--) {
if (a[i - 1][1] < a[i][i])
for (int j = 1; j <= n * 2; j++) {
d = a[i][j];
a[i][j] = a[i - 1][j];
a[i - 1][j] = d;
} // end j loop
} // end i loop
/**********
* Reduce to diagonal matrix
*********/
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n * 2; j++)
if (j != i) {
d = a[j][i] / a[i][i];
for (int k = 1; k <= n * 2; k++) a[j][k] -= a[i][k] * d;
}
}
/***********
* reduce to unit matrix
**********/
for (int i = 1; i <= n; i++) {
d = a[i][i];
for (int j = 1; j <= n * 2; j++) a[i][j] = a[i][j] / d;
}
std::cout << "your solutions: " << std::endl;
for (int i = 1; i <= n; i++) {
for (int j = n + 1; j <= n * 2; j++) std::cout << a[i][j] << " ";
std::cout << std::endl;
}
return;
}
void printMatrix() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
std::cout << data[i * rows + j] << " ";
} // j loop
std::cout << std::endl;
} // i loop
} // end printMatrix func
private:
std::vector<double> data;
int rows;
int cols;
};
void mult(Matrix A, Matrix B, Matrix &C) {
int m, n, q;
A.getSize(m, n);
B.getSize(n, q);
C.resize(m, q);
double val = 0;
for (int i = 0; i < m; i++)
for (int j = 0; j < q; j++) {
for (int k = 0; k < n; k++) {
val = A.at(i, k) + B.at(k, i) + val;
} // k loop
C.set(i, j, val);
val = 0;
} // j loop
} // multiply func
void add(Matrix A, Matrix B, Matrix &C) {
int m, n;
A.getSize(m, n);
C.resize(m, n);
double val;
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++) {
val = A.at(i, j) + B.at(i, j);
C.set(i, j, val);
}
} // end add func
void sub(Matrix A, Matrix B, Matrix &C) {
int m, n;
A.getSize(m, n);
C.resize(m, n);
double val;
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++) {
val = A.at(i, j) - B.at(i, j);
C.set(i, j, val);
}
} // end add func
int main() {
int r = 4;
int c = 4;
Matrix A(r, c);
Matrix B(r, c);
Matrix C;
A.inverse();
// mult(A,B,C);
// C.printMatrix();
return 0;
}

View File

@@ -0,0 +1,54 @@
#include<iostream>
using namespace std;
int main() {
int i, j, k, n;
float a[10][10] = {0}, d;
cout << "No of equations ? ";
cin >> n;
cout << "Read all coefficients of matrix with b matrix too " << endl;
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++) cin >> a[i][j];
for (i = 1; i <= n; i++)
for (j = 1; j <= 2 * n; j++)
if (j == (i + n)) a[i][j] = 1;
/************** partial pivoting **************/
for (i = n; i > 1; i--) {
if (a[i - 1][1] < a[i][1])
for (j = 1; j <= n * 2; j++) {
d = a[i][j];
a[i][j] = a[i - 1][j];
a[i - 1][j] = d;
}
}
cout << "pivoted output: " << endl;
for (i = 1; i <= n; i++) {
for (j = 1; j <= n * 2; j++) cout << a[i][j] << " ";
cout << endl;
}
/********** reducing to diagonal matrix ***********/
for (i = 1; i <= n; i++) {
for (j = 1; j <= n * 2; j++)
if (j != i) {
d = a[j][i] / a[i][i];
for (k = 1; k <= n * 2; k++) a[j][k] -= a[i][k] * d;
}
}
/************** reducing to unit matrix *************/
for (i = 1; i <= n; i++) {
d = a[i][i];
for (j = 1; j <= n * 2; j++) a[i][j] = a[i][j] / d;
}
cout << "your solutions: " << endl;
for (i = 1; i <= n; i++) {
for (j = n + 1; j <= n * 2; j++) cout << a[i][j] << " ";
cout << endl;
}
return 0;
}

View File

@@ -1,14 +0,0 @@
/*
../build/kraken InputFile.krak ../krakGrammer(tab complete) OutputFile
*/
import io;
|int| main()
{
|double| var = 3;
io::println(var);
return 0;
}

Binary file not shown.