Introduction
Introduction Statistics Contact Development Disclaimer Help
tRemoved comments, matrixes square - numeric - C++ library with numerical algor…
git clone git://src.adamsgaard.dk/numeric
Log
Files
Refs
LICENSE
---
commit 43b64455bb2586f8d0dca506dd9068373de92ad6
parent 95c6e4f090aa65daa2e9897a7824f4800d756c29
Author: Anders Damsgaard Christensen <[email protected]>
Date: Tue, 22 Jan 2013 17:34:02 +0100
Removed comments, matrixes square
Diffstat:
M matrixmul/c-arrofarrs.c | 8 --------
M matrixmul/c-linarr.c | 6 ------
M matrixmul/cpp-linvectors.cpp | 32 ++++++++++++-----------------…
M matrixmul/cpp-vectorofvectors.cpp | 24 ++++++++----------------
M matrixmul/julia.jl | 8 +++-----
M matrixmul/octave.m | 8 +++-----
M matrixmul/python-numpy.py | 13 +++++--------
7 files changed, 31 insertions(+), 68 deletions(-)
---
diff --git a/matrixmul/c-arrofarrs.c b/matrixmul/c-arrofarrs.c
t@@ -25,7 +25,6 @@ int main(int argc, char* argv[])
double** B;
double** C;
- /* Read input argument as matrix height */
if (argc == 2) {
N = atoi(argv[1]);
} else {
t@@ -33,31 +32,25 @@ int main(int argc, char* argv[])
return 1;
}
- /* Allocate arrays */
A = (double**) malloc(N * sizeof(double*));
B = (double**) malloc(N * sizeof(double*));
C = (double**) malloc(N * sizeof(double*));
- /* Allocate row arrays inside arrays */
for (i = 0; i < N; i++) {
A[i] = (double*) malloc(N * sizeof(double));
B[i] = (double*) malloc(N * sizeof(double));
C[i] = (double*) malloc(N * sizeof(double));
}
- /* Fill matrices A and B with values */
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
A[i][j] = 2.0;
- /* We want a random value between 0 and 1 */
B[i][j] = rand()/RAND_MAX;
}
}
- /* Perform matrix multiplication */
matrixMult(A, B, C, N);
- /* Free memory */
for (i = 0; i < N; i++) {
free(A[i]);
free(B[i]);
t@@ -67,6 +60,5 @@ int main(int argc, char* argv[])
free(B);
free(C);
- /* Exit with success */
return 0;
}
diff --git a/matrixmul/c-linarr.c b/matrixmul/c-linarr.c
t@@ -25,7 +25,6 @@ int main(int argc, char* argv[])
double* B;
double* C;
- /* Read input argument as matrix height */
if (argc == 2) {
N = atoi(argv[1]);
} else {
t@@ -33,22 +32,17 @@ int main(int argc, char* argv[])
return 1;
}
- /* Allocate arrays */
A = (double*) malloc(N * N * sizeof(double*));
B = (double*) malloc(N * N * sizeof(double*));
C = (double*) malloc(N * N * sizeof(double*));
- /* Fill matrices A and B with values */
for (i = 0; i < N*N; i++) {
A[i] = 2.0;
- /* We want a random value between 0 and 1 */
B[i] = rand()/RAND_MAX;
}
- /* Perform matrix multiplication */
matrixMult(A, B, C, N);
- /* Free memory */
free(A);
free(B);
free(C);
diff --git a/matrixmul/cpp-linvectors.cpp b/matrixmul/cpp-linvectors.cpp
t@@ -1,15 +1,14 @@
#include <iostream>
#include <vector>
-#include <cstdlib> // rand is here
+#include <cstdlib>
int main(int argc, char* argv[])
{
using std::cout;
using std::vector;
- int M, N, i, j, k;
+ int N, i, j, k;
- // Read input argument as matrix height
if (argc == 2) {
N = atoi(argv[1]);
} else {
t@@ -17,33 +16,26 @@ int main(int argc, char* argv[])
return 1;
}
- // Width equal to height
- M = N;
+ vector<double> A(N*N);
+ vector<double> B(N*N);
+ vector<double> C(N*N);
- vector<double> A(M*N);
- vector<double> B(M*N);
- vector<double> C(M*N);
-
- // Fill matrices A and B with values
for (i = 0; i<N; ++i) {
- for (j = 0; j<M; ++j) {
- A[j*M+i] = 2.0;
- // We want a random value between 0 and 1
- B[j*M+i] = rand()/RAND_MAX;
+ for (j = 0; j<N; ++j) {
+ A[j*N+i] = 2.0;
+ B[j*N+i] = rand()/RAND_MAX;
}
}
double sum;
- // Perform matrix multiplication
for (i = 0; i < N; ++i) {
- for (j = 0; j < M; ++j) {
+ for (j = 0; j < N; ++j) {
sum = 0.0f;
- for (k = 0; k < M; ++k)
- sum += A[k*M+i] * B[j*M+k];
- C[j*M+i] = sum;
+ for (k = 0; k < N; ++k)
+ sum += A[k*N+i] * B[j*N+k];
+ C[j*N+i] = sum;
}
}
- // Exit with success
return 0;
}
diff --git a/matrixmul/cpp-vectorofvectors.cpp b/matrixmul/cpp-vectorofvectors.…
t@@ -1,15 +1,14 @@
#include <iostream>
#include <vector>
-#include <cstdlib> // rand is here
+#include <cstdlib>
int main(int argc, char* argv[])
{
using std::cout;
using std::vector;
- int M, N, i, j, k;
+ int N, i, j, k;
- // Read input argument as matrix height
if (argc == 2) {
N = atoi(argv[1]);
} else {
t@@ -17,33 +16,26 @@ int main(int argc, char* argv[])
return 1;
}
- // Width equal to height
- M = N;
-
- vector< vector<double> > A(M,vector<double>(M));
- vector< vector<double> > B(M,vector<double>(M));
- vector< vector<double> > C(M,vector<double>(M));
+ vector< vector<double> > A(N,vector<double>(N));
+ vector< vector<double> > B(N,vector<double>(N));
+ vector< vector<double> > C(N,vector<double>(N));
- // Fill matrices A and B with values
for (i = 0; i<N; ++i) {
- for (j = 0; j<M; ++j) {
+ for (j = 0; j<N; ++j) {
A[i][j] = 2.0;
- // We want a random value between 0 and 1
B[i][j] = rand()/RAND_MAX;
}
}
double sum;
- // Perform matrix multiplication
for (i = 0; i < N; ++i) {
- for (j = 0; j < M; ++j) {
+ for (j = 0; j < N; ++j) {
sum = 0.0;
- for (k = 0; k < M; ++k)
+ for (k = 0; k < N; ++k)
sum = A[k][j] * B[i][k];
C[i][j] = sum;
}
}
- // Exit with success
return 0;
}
diff --git a/matrixmul/julia.jl b/matrixmul/julia.jl
t@@ -1,4 +1,4 @@
-#!/usr/bin/julia
+#!/usr/bin/env julia
if (length(ARGS) == 1)
N = int(ARGS[1])
t@@ -6,8 +6,6 @@ else
println("Sorry, I need the matrix width as a command line argument\n")
end
-M = N
-
-A = ones(M,N)*2.0
-B = rand(M,N)
+A = ones(N,N)*2.0
+B = rand(N,N)
C = A*B
diff --git a/matrixmul/octave.m b/matrixmul/octave.m
t@@ -1,14 +1,12 @@
#/usr/bin/octave -q -f --no-window-system
if (nargin == 1)
- M = str2num(argv(){1});
+ N = str2num(argv(){1});
else
disp("Sorry, I need the matrix size as a command line argument");
exit(1);
end
-N = M;
-
-A = ones(M,N) * 2.0;
-B = rand(M,N);
+A = ones(N,N) * 2.0;
+B = rand(N,N);
C = A*B;
diff --git a/matrixmul/python-numpy.py b/matrixmul/python-numpy.py
t@@ -1,16 +1,13 @@
#!/usr/bin/env python
-
import sys
import numpy
if (len(sys.argv) == 2):
- M = int(sys.argv[1])
-else:
+ N = int(sys.argv[1])
+else :
print("Sorry, I need a matrix width as input argument!")
sys.exit(1)
-N = M
-
-A = numpy.ones((M,N))*2.0
-B = numpy.random.random_sample((M,N))
-C = numpy.dot(A,B) # A*B is element wise on numpy arrays
+A = numpy.ones((N,N))*2.0
+B = numpy.random.random_sample((N,N))
+C = numpy.dot(A,B)
You are viewing proxied material from mx1.adamsgaard.dk. The copyright of proxied material belongs to its original authors. Any comments or complaints in relation to proxied material should be directed to the original authors of the content concerned. Please see the disclaimer for more details.