473,394 Members | 2,020 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,394 software developers and data experts.

Help with multidimensional matrix multiplication

Hello, I'm a newbie to C++ so excuse me if my question was trivial but
it is important to me.
I'm implementing a simple code to find the forward kinematics of a
robot:

#include "stdafx.h"
#include<iostream>
#include<iomanip>
#include<fstream>
#include"math.h"
#include"string.h"
#include<string>
#include"wchar.h"
#include<set>
using namespace std;

using std::cin;
using std::cout;
using std::endl;
#include<cmath>
int main()
{
// Initializtion
int i,j,k,sum; // Used for
FOR loop declartion
double t1,t2,t3,t4,t5,t6; // Thetas in
radians
double pi = 3.14159265;
double alp[6]={pi/3,-pi/2,0,pi/4,-pi/6,pi/2}; // The size of the
array depends on the dof of the robot (user defines it)
double a[6]={0,0.23,0.46,0.2,0.34,0.98};
double d[6]={0.24,0,0,0.1,0.56,0};
int size=sizeof(a)/sizeof(a[0]);

// User input
cout<<"Enter t1 in degrees:";cin>>t1;
cout<<"Enter t2 in degrees:";cin>>t2;
cout<<"Enter t3 in degrees:";cin>>t3;
cout<<"Enter t4 in degrees:";cin>>t4;
cout<<"Enter t5 in degrees:";cin>>t5;
cout<<"Enter t6 in degrees:";cin>>t6;
double t[6]={t1*(pi/180),t2*(pi/180),t3*(pi/180),t4*(pi/180),t5*(pi/
180),t6*(pi/180)};

double*** T= new double**[size]; //pointer to pointer
// Initialize Transformation matrices as 3D matrices to contain the
rows and columns plus
// another dimension for the whole transformations (1-6)

for(i=0;i<size;i++){
T[i] = new double*[4];

for (j=0; j<4; j++) // j represents the number of columns and rows
T[i][j]= new double[4];

T[i][0][0]=cos(t[i]); T[i][0][1]=-sin(t[i]);T[i][0][2]=0;T[i][0]
[3]=a[i];
T[i][1][0]=sin(t[i])*cos(alp[i]);T[i][1]
[1]=cos(t[i])*cos(alp[i]);T[i][1][2]=-sin(alp[i]);T[i][1][3]=-
sin(alp[i])*d[i];
T[i][2][0]=sin(t[i])*sin(alp[i]);T[i][2]
[1]=cos(t[i])*sin(alp[i]);T[i][2][2]=cos(alp[i]);T[i][2]
[3]=cos(alp[i])*d[i];
T[i][3][0]=0;T[i][3][1]=0;T[i][3][2]=0;T[i][3][3]=1;
}
---------------------------------------------------------------------------------------------------------------------------------------------
T matrix basically contains all the parameters (a, alp,t, d) which are
defined before the loop. T[1] for example is a 4-by-4 matrix that has
elements associated with a[1],t[1],alp[1] & d[1]. Since size is 6 in
this case, I need to have T[1],T[2],......T[6].
This code works fine for me and I can display all of the T's
correctly. Now what I need to do is to multiply all of them to
resultant T=T[1]*T[2]*.....*T[6]. I wrote a simple code to multiply a
simple 2D matrix, but I'm not sure how to incorporate my third
dimension. Also, is there a simpler way(more efficient) to implement
the matrix instead of the way I defined it in the loop (element by
element)?

Thanks a million.

Nov 14 '07 #1
3 3852
crazygrey wrote:
....
double*** T= new double**[size]; //pointer to pointer
why not:
typedef double mat_type[4][4];

mat_type * T = new mat_type[ size ];

>

// Initialize Transformation matrices as 3D matrices to contain the
rows and columns plus
// another dimension for the whole transformations (1-6)

for(i=0;i<size;i++){
T[i] = new double*[4];

for (j=0; j<4; j++) // j represents the number of columns and rows
T[i][j]= new double[4];
You seem to be using T[i] alot - one way of reducing those lookup is by:
mat_type & Ti = T[i];
>
T[i][0][0]=cos(t[i]); T[i][0][1]=-sin(t[i]);T[i][0][2]=0;T[i][0]
[3]=a[i];
T[i][1][0]=sin(t[i])*cos(alp[i]);T[i][1]
[1]=cos(t[i])*cos(alp[i]);T[i][1][2]=-sin(alp[i]);T[i][1][3]=-
sin(alp[i])*d[i];
T[i][2][0]=sin(t[i])*sin(alp[i]);T[i][2]
[1]=cos(t[i])*sin(alp[i]);T[i][2][2]=cos(alp[i]);T[i][2]
[3]=cos(alp[i])*d[i];
T[i][3][0]=0;T[i][3][1]=0;T[i][3][2]=0;T[i][3][3]=1;
}
---------------------------------------------------------------------------------------------------------------------------------------------
T matrix basically contains all the parameters (a, alp,t, d) which are
defined before the loop. T[1] for example is a 4-by-4 matrix that has
elements associated with a[1],t[1],alp[1] & d[1]. Since size is 6 in
this case, I need to have T[1],T[2],......T[6].
That would be T[0] ... T[5]
This code works fine for me and I can display all of the T's
correctly. Now what I need to do is to multiply all of them to
resultant T=T[1]*T[2]*.....*T[6]. I wrote a simple code to multiply a
simple 2D matrix, but I'm not sure how to incorporate my third
dimension.
What do you mean by "third dimension" ?

Anyhow - the best way of doing this is to define a single class that is
a 4x4 matrix and then define multiplication.

There are some examples of matric classes in previous posts on this NG
or some open source ones.
... Also, is there a simpler way(more efficient) to implement
the matrix instead of the way I defined it in the loop (element by
element)?
Probably not.
Nov 14 '07 #2
On Nov 14, 12:53 pm, crazygrey <malla...@gmail.comwrote:
Hello, I'm a newbie to C++ so excuse me if my question was trivial but
it is important to me.
I'm implementing a simple code to find the forward kinematics of a
robot:

#include "stdafx.h"
#include<iostream>
#include<iomanip>
#include<fstream>
#include"math.h"
#include"string.h"
#include<string>
#include"wchar.h"
#include<set>
using namespace std;

using std::cin;
using std::cout;
using std::endl;
#include<cmath>

int main()
{
// Initializtion
int i,j,k,sum; // Used for
FOR loop declartion
double t1,t2,t3,t4,t5,t6; // Thetas in
radians
double pi = 3.14159265;
double alp[6]={pi/3,-pi/2,0,pi/4,-pi/6,pi/2}; // The size of the
array depends on the dof of the robot (user defines it)
double a[6]={0,0.23,0.46,0.2,0.34,0.98};
double d[6]={0.24,0,0,0.1,0.56,0};
int size=sizeof(a)/sizeof(a[0]);

// User input
cout<<"Enter t1 in degrees:";cin>>t1;
cout<<"Enter t2 in degrees:";cin>>t2;
cout<<"Enter t3 in degrees:";cin>>t3;
cout<<"Enter t4 in degrees:";cin>>t4;
cout<<"Enter t5 in degrees:";cin>>t5;
cout<<"Enter t6 in degrees:";cin>>t6;
double t[6]={t1*(pi/180),t2*(pi/180),t3*(pi/180),t4*(pi/180),t5*(pi/
180),t6*(pi/180)};

double*** T= new double**[size]; //pointer to pointer

// Initialize Transformation matrices as 3D matrices to contain the
rows and columns plus
// another dimension for the whole transformations (1-6)

for(i=0;i<size;i++){
T[i] = new double*[4];

for (j=0; j<4; j++) // j represents the number of columns and rows
T[i][j]= new double[4];

T[i][0][0]=cos(t[i]); T[i][0][1]=-sin(t[i]);T[i][0][2]=0;T[i][0]
[3]=a[i];
T[i][1][0]=sin(t[i])*cos(alp[i]);T[i][1]
[1]=cos(t[i])*cos(alp[i]);T[i][1][2]=-sin(alp[i]);T[i][1][3]=-
sin(alp[i])*d[i];
T[i][2][0]=sin(t[i])*sin(alp[i]);T[i][2]
[1]=cos(t[i])*sin(alp[i]);T[i][2][2]=cos(alp[i]);T[i][2]
[3]=cos(alp[i])*d[i];
T[i][3][0]=0;T[i][3][1]=0;T[i][3][2]=0;T[i][3][3]=1;
}
---------------------------------------------------------------------------------------------------------------------------------------------
T matrix basically contains all the parameters (a, alp,t, d) which are
defined before the loop. T[1] for example is a 4-by-4 matrix that has
elements associated with a[1],t[1],alp[1] & d[1]. Since size is 6 in
this case, I need to have T[1],T[2],......T[6].
This code works fine for me and I can display all of the T's
correctly. Now what I need to do is to multiply all of them to
resultant T=T[1]*T[2]*.....*T[6]. I wrote a simple code to multiply a
simple 2D matrix, but I'm not sure how to incorporate my third
dimension. Also, is there a simpler way(more efficient) to implement
the matrix instead of the way I defined it in the loop (element by
element)?

Thanks a million.
There's always TNT:
http://math.nist.gov/tnt/

Nov 14 '07 #3
On Nov 14, 5:56 pm, Gianni Mariani <gi4nos...@mariani.wswrote:
crazygrey wrote:

...
double*** T= new double**[size]; //pointer to pointer

why not:
typedef double mat_type[4][4];

mat_type * T = new mat_type[ size ];
// Initialize Transformation matrices as 3D matrices to contain the
rows and columns plus
// another dimension for the whole transformations (1-6)
for(i=0;i<size;i++){
T[i] = new double*[4];
for (j=0; j<4; j++) // j represents the number of columns and rows
T[i][j]= new double[4];

You seem to be using T[i] alot - one way of reducing those lookup is by:

mat_type & Ti = T[i];


T[i][0][0]=cos(t[i]); T[i][0][1]=-sin(t[i]);T[i][0][2]=0;T[i][0]
[3]=a[i];
T[i][1][0]=sin(t[i])*cos(alp[i]);T[i][1]
[1]=cos(t[i])*cos(alp[i]);T[i][1][2]=-sin(alp[i]);T[i][1][3]=-
sin(alp[i])*d[i];
T[i][2][0]=sin(t[i])*sin(alp[i]);T[i][2]
[1]=cos(t[i])*sin(alp[i]);T[i][2][2]=cos(alp[i]);T[i][2]
[3]=cos(alp[i])*d[i];
T[i][3][0]=0;T[i][3][1]=0;T[i][3][2]=0;T[i][3][3]=1;
}
---------------------------------------------------------------------------------------------------------------------------------------------
T matrix basically contains all the parameters (a, alp,t, d) which are
defined before the loop. T[1] for example is a 4-by-4 matrix that has
elements associated with a[1],t[1],alp[1] & d[1]. Since size is 6 in
this case, I need to have T[1],T[2],......T[6].

That would be T[0] ... T[5]
This code works fine for me and I can display all of the T's
correctly. Now what I need to do is to multiply all of them to
resultant T=T[1]*T[2]*.....*T[6]. I wrote a simple code to multiply a
simple 2D matrix, but I'm not sure how to incorporate my third
dimension.

What do you mean by "third dimension" ?

Anyhow - the best way of doing this is to define a single class that is
a 4x4 matrix and then define multiplication.

There are some examples of matric classes in previous posts on this NG
or some open source ones.
... Also, is there a simpler way(more efficient) to implement
the matrix instead of the way I defined it in the loop (element by
element)?

Probably not.
Thanks, that was helpful. I created a function that does the
multiplication and everything works fine now.
Nov 15 '07 #4

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

Similar topics

7
by: Silver | last post by:
First of all...merry christmas everyone! Now, I have to write a program. Among other things, I need to overload the '*' operator, so that it returns a pointer-to-ponter of type int, which is...
14
by: amitnanda | last post by:
Hi Guys, I have a matrix multiplication program in C that multiplies two matrices. When their size is 3*3 or 800*800, the program runs fine. But above that size, I get a "segmentation fault"....
2
by: xhunga | last post by:
I have try a new version of my work. I have put the sizes of the matrix into the matrix. A = number of rows A = number of columns The first element of the matrix is A instead of A. You...
20
by: Frank-O | last post by:
Hi , Recently I have been commited to the task of "translating" some complex statistical algorithms from Matlab to C++. The goal is to be three times as fast as matlab ( the latest) . I've...
0
by: lituncse | last post by:
dear friends, i have come across a problem which is difficult to solve for me.it's about starssen's matrix multiplication.in general matrix multiplication we need 8 multiplications and 4 additions...
2
by: wuzertheloser | last post by:
Develop a program which computes the current value of the vector {x} based on the following forward iteration: {x}(n+1) = {x}(n), n = 0,1,2, ... ,8,9. In other words, the next vector {x} is...
1
by: raylegendkiller | last post by:
NEED TO MAKE A PROGRAM which computes the current value of the vectors {x} based on the following forward iterations: this >>> {x}(n+1) = {x}(n), n = 0,1,2, ... ,8,9. In other...
6
by: amitsoni.1984 | last post by:
Hi, Is there any direct function for matrix multiplication in Python or any of its packages? or do we have to multiply element by element? Thank you, Amit
8
by: joegao1 | last post by:
can some one give me a hint? I want to program the code for matrix multiplication with as less arithmetical / multiplication operations as possible. my task is to calculate the matrix...
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: 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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.