473,739 Members | 10,283 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

matrix multiplication

Hi again. I'm now asking your help because of a smal problem i'm getting
with my multiplication matrix code.

I'll try to give you as many details as possible.

My matrix structure:

typedef struct {
int col; /* number of colowns */
int lin; /* number of lines*/
double *element; /* pointer do double... this is actually, a "simulated
multidymensiona l array", got the idea from the C FAQ from this newsgroup */
} matrix;
matrix mul_matrix(matr ix mat1, matrix mat2, int lin, int col)
{
matrix M;
int i, j, k, exp;
M = init_matrix(lin , col); /* inits M(will old the result from mat1 *
mat2), no problem here. At this point i am sure that mat1 and mat2 all have
their right values
and that M has the right size*/
/* Try to multiply */
for (i=0; i<lin; i++) {
for (j=0; j<col; j++) {
exp = 0;
for (k=0; k<col; k++) {
exp += mat1.element[i * (mat1.col * mat1.lin) + k]*
mat2.element[k * (mat2.col * mat2.lin) + j]; /* big problem... give me
random results*/
}
M.element[i * (lin * col) + j] = exp;
}
}
return M;
}

After 4 hours of trying to resolve this problem i'm exaust and in need for
some sleep :P Maybe that will make me good cause i sinceriously can't
see the problem on the code! Until now i've only used printf to assist me as
a rudimentary debugger. I think i need something more powerfull but
gdb don't want to work with devc++...
Thanks to anyone who can help me out!
Nov 13 '05 #1
3 12905
In article <3f************ ***********@new s.telepac.pt>,
"robix" <ne**@fm.cv> wrote:
Hi again. I'm now asking your help because of a smal problem i'm getting
with my multiplication matrix code.

I'll try to give you as many details as possible.

My matrix structure:

typedef struct {
int col; /* number of colowns */
int lin; /* number of lines*/
double *element; /* pointer do double... this is actually, a "simulated
multidymensiona l array", got the idea from the C FAQ from this newsgroup */
} matrix;
matrix mul_matrix(matr ix mat1, matrix mat2, int lin, int col)
{
matrix M;
int i, j, k, exp;
M = init_matrix(lin , col); /* inits M(will old the result from mat1 *
mat2), no problem here. At this point i am sure that mat1 and mat2 all have
their right values
and that M has the right size*/
/* Try to multiply */
for (i=0; i<lin; i++) {
for (j=0; j<col; j++) {
exp = 0;
for (k=0; k<col; k++) {
exp += mat1.element[i * (mat1.col * mat1.lin) + k]*
mat2.element[k * (mat2.col * mat2.lin) + j]; /* big problem... give me
random results*/
}
M.element[i * (lin * col) + j] = exp;
}
}
return M;
}


Just check which two matrix elements you access when i = 1, j = 1 and k
= 1. Then compare this with the number of elements in the matrix mat1
and mat2, and you will see that something is very badly wrong.
Nov 13 '05 #2
In article <3f************ ***********@new s.telepac.pt>,
robix <ne**@fm.cv> wrote:

My matrix structure:

typedef struct {
int col; /* number of colowns */
int lin; /* number of lines*/
double *element; /* pointer do double... this is actually, a "simulated
multidymension al array", got the idea from the C FAQ from this newsgroup */
} matrix;
matrix mul_matrix(matr ix mat1, matrix mat2, int lin, int col)
{
matrix M;
int i, j, k, exp;
M = init_matrix(lin , col); /* inits M(will old the result from mat1 *
mat2), no problem here. At this point i am sure that mat1 and mat2 all have
their right values
and that M has the right size*/
/* Try to multiply */
for (i=0; i<lin; i++) {
for (j=0; j<col; j++) {
exp = 0;
for (k=0; k<col; k++) {
exp += mat1.element[i * (mat1.col * mat1.lin) + k]*
mat2.element[k * (mat2.col * mat2.lin) + j]; /* big problem... give me
random results*/
}
M.element[i * (lin * col) + j] = exp;
}
}
return M;
}


1.
Why "int exp"?

2.
Suppose A is an mxn matrix and B is a nxk matrix. Their product
will be an mxk matrix. As you see, to multiply matrices, you need
to specify three index ranges, m, n and k. Your mul_matrix() function
takes only to index ranges, lin and col. Something's badly wrong
here.

--
Rouben Rostamian <ro*******@umbc .edu>

Nov 13 '05 #3
Thanks for the tips. It works now!
Nov 13 '05 #4

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

Similar topics

6
3331
by: Ben Ingram | last post by:
Hi all, I am writing a template matrix class in which the template parameters are the number of rows and number of columns. There are a number of reasons why this is an appropriate tradeoff for my particular application. One of the advantages is that the _compiler_ can force inner matrix dimensions used in multiplication to agree. A _complie-time_ error will be triggered if you write A * B and the number of coluns in A does not equal the...
15
13277
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 matricies at compile-time? Any help would be appreciated. Hopefully this code comes in useful for someone, let me know if you find it useful, or if you have suggestions on how to improve it. // Public Domain by Christopher Diggins, 2005 ...
14
4970
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". I need this huge size as part of my assignment.
20
5240
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 used various techniques ( loop unrolling, loop jamming...) and tried some matrix libraries : newmat (slow for large matrix) , STL (fast but ..not usefull) , hand coding (brain consuming...), and recently Meschach...
0
4209
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 but for the starssen's matrix multiplication we need 7 multiplication and 14 additions which is very important from time complexity point of view.please give a solution if anyone knows. ratikanta panda
6
16435
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
3
3881
by: crazygrey | last post by:
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"
1
9163
by: Sozos | last post by:
Hi guys. I have a problem with writing the base case for the following matrix multiplication function I have implemented. Please help. #define index(i,j,power) (((i)<<(power))+(j)) void recMultiply(int i, int j, float a, int k, int l, float b, int x, int y, float c, int s); int i, j, k, s, matrixsize, blocksize, jj, kk, power, bsize; float sum, maxr, total=0.0, startmult, finishmult, multtime; float* A = NULL; float* B = NULL;
8
7895
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 multiplication A*B*A' , where - A' refers to the transpose of matrix A - matrix B is symmetric matrix. - (optional) it will be much better if the result of (A*B) or (A*B)' can be stored as temperal matrix, as this value is required thereafter.
0
8969
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8792
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9479
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9337
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9209
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4826
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3280
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2748
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.