473,386 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

simple matrix routines

i am looking for some simple matrix routines: vector*matrix,
matrix*matrix etc...in the form of code like the following that
multiplies two vectors:

void vmult(double* aa, double* bb, double* cc, int lnt) {
//cc += lnt;
while(lnt--) {
*cc++ = *bb++ * *aa++;
}
}

thanks in advance - Rohan
comp.lang.c
Nov 14 '05 #1
1 1672
Rohan Shah wrote:
i am looking for some simple matrix routines: vector*matrix,
matrix*matrix etc...in the form of code like the following that
multiplies two vectors:

void vmult(double* aa, double* bb, double* cc, int lnt) {
//cc += lnt;
while(lnt--) {
*cc++ = *bb++ * *aa++;
}
}

thanks in advance - Rohan
comp.lang.c


C++ not C, but very easy to convert:

struct vector4
{
float val[4];

float& operator[] (uint i) {return val[i];}
float& x() {return val[0];}
float& y() {return val[1];}
float& z() {return val[2];}
float& w() {return val[3];}

vector4()
{x()=0.0f;y()=0.0f;z()=0.0f;w()=1.0f;}
vector4(float X,float Y,float Z,float W)
{x()=X;y()=Y;z()=Z;w()=W;}
};

struct matrix4x4
{
float val[4][4];
float* operator[] (uint i) {return val[i];}
};

matrix4x4 BuildIdentityMatrix()
{
matrix4x4 out;
for(uint i=0;i<4;++i)
{
for(uint j=0;j<4;++j)
{
if(i==j)
out[i][j] = 1.0f;
else
out[i][j] = 0.0f;
}
}
return out;
}

matrix4x4 BuildTranslationMatrix(vector4 t)
{
vector4 tn = VectorNormalize(t);
matrix4x4 out = BuildIdentityMatrix();
out[3][0] = tn.x();
out[3][1] = tn.y();
out[3][2] = tn.z();
return out;
}

matrix4x4 MatrixMult(matrix4x4 a, matrix4x4 b)
{
matrix4x4 result;
for(uint i=0;i<4;++i)
{
for(uint j=0;j<4;++j)
{
float value = 0.0f;
for(uint k=0;k<4;++k)
{
value = value + a[i][k] * b[k][j];
}
result[i][j] = value;
}
}
return result;
}

vector4 MatrixApply(matrix4x4 m, vector4 v)
{
vector4 result;
for(uint i=0;i<4;++i)
{
float value = 0.0f;
for(uint j=0;j<4;++j)
{
value = value + v[j] * m[j][i];
}
result[i] = value;
}
return VectorNormalize(result);
}

vector4 VectorNormalize(vector4 v)
{
vector4 out;
out[0] = v[0] / v[3];
out[1] = v[1] / v[3];
out[2] = v[2] / v[3];
out[3] = 1.0f;
return out;
}

float DotProduct(vector4 v1, vector4 v2)
{
return v1.x()*v2.x() + v1.y()*v2.y() + v1.z()*v2.z();
}

vector4 CrossProduct(vector4 v1, vector4 v2)
{
return vector4(v1.y()*v2.z()-v1.z()*v2.y(), v1.z()*v2.x()-v1.x()*v2.z(),
v1.x()*v2.y()-v1.y()*v2.x(), 1.0f);
}

- Pete
Nov 14 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Erik Borgstr?m | last post by:
Hi, I simply want to use simple matrices of ints or doubles in C++ and I want all the good: 1) be able to use double-index, e.g. m 2) not have to use pointers at all 3) do it fast I know...
1
by: SUPER_SOCKO | last post by:
Suppose I have a matrix M x N dimension. Normally, to print all of the elements in the matrix. I write the following codes: Suppose M = 5, N = 4: for(int j=0; j<5; ++j) { for(int k=0; k<4;...
15
by: christopher diggins | last post by:
Here is some code I wrote for Matrix multiplication for arbitrary dimensionality known at compile-time. I am curious how practical it is. For instance, is it common to know the dimensionality of...
2
by: Emacs Row | last post by:
Hello NG, I'm using Numerical Recipes in C++. Now I'm looking for a routine to calculate the eigenvalues of a matrix with *complex* valued entries. The matrix type is Mat_IO_CPLX_DP. So...
10
by: Ing. Carlos Villaseñor M. | last post by:
Hi everybody! I have developed in C# and got in a news group a math class that make matrix operations, eigenvals, eigenvecs, stat functions and much more, but now I trying to develop software in...
14
by: Paul McGuire | last post by:
I've posted a simple Matrix class on my website as a small-footprint package for doing basic calculations on matrices up to about 10x10 in size (no theoretical limit, but performance on inverse is...
6
by: lancered | last post by:
Hi dear all, I am using Python2.4.2+NumPy1.0.1 to deal with a parameter estimation problem with the least square methods. During the calculations, I use NumPy package to deal with matrix...
5
by: adam.kleinbaum | last post by:
Hi there, I'm a novice C programmer working with a series of large (30,000 x 30,000) sparse matrices on a Linux system using the GCC compiler. To represent and store these matrices, I'd like to...
10
by: Babak | last post by:
Hi, I've developed a C program which contains a large number of vectors and matrices operations. Throughout my code, I used the template from the Numerical Recipes book to define vectors and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.