By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,830 Members | 1,770 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,830 IT Pros & Developers. It's quick & easy.

Can a function returns a matrix?

P: n/a
hi all,
I want a function that takes matrix as input and returns a
matrix. (say, function that performs matrix multiplication). How can I
go about this? Can anybody help me?
Balaji.V

Aug 2 '06 #1
Share this Question
Share on Google+
12 Replies


P: n/a
MQ

Balaji.V wrote:
hi all,
I want a function that takes matrix as input and returns a
matrix. (say, function that performs matrix multiplication). How can I
go about this? Can anybody help me?
Balaji.V
Define a matrix data structure. Pass a pointer to the structure to the
function. There are probably already matrix algebra implementations in
the standard library, so take a look...

MQ

Aug 2 '06 #2

P: n/a
"MQ" <mi**************@gmail.comwrites:
Balaji.V wrote:
> I want a function that takes matrix as input and returns a
matrix. (say, function that performs matrix multiplication). How can I
go about this? Can anybody help me?

Define a matrix data structure. Pass a pointer to the structure to the
function. There are probably already matrix algebra implementations in
the standard library, so take a look...
There are no matrix algebra functions in the C standard library.
There probably are such functions in third-party libraries (the
details of which are off-topic here).

There's no one way to represent a matrix; if you're going to use one
of these libraries, you'll have to conform to the representation it
expects.

If you want to roll your own, section 6 of the FAQ,
<http://www.c-faq.com/>, has some good information on allocating
multidimensional arrays.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Aug 2 '06 #3

P: n/a

You can try my work, I give an example.

Windows : Dev-C++ 4
Linux : gcc abc.c -lm Return
a.out Return

First step :
http://www.geocities.com/xhungab/tutorial/t04a.zip

Second step :
http://www.geocities.com/xhungab/tutorial/t04b.zip

Example

/* http://groups.yahoo.com/group/mathc/ */
/* ------------------------------------------ */
#include <stdio.h /* getchar(); */

/*-------------------------- Matrix structure */
typedef struct
{
int rows;
int cols;
double *pb; /* Pointer on a block of memory */
} mR, /* Type mR */
*PmR; /* Pointer on a type mR */

/* --------------------------------- FUNCTION */
/* Do : create a matrix without initialization.*/
/* ------------------------------------------- */
double * create_mR(
int r, /* (r)ow */
int c /* (c)olumn */
)
{
double *P_A;

P_A = (double *) malloc(r*c*sizeof(double));

if(P_A==NULL){exit(1);}

return(P_A);
}

/* --------------------------------- FUNCTION */
/* Do : print matrix A. */
/* ---------------------------------------- - */
void p_mR(
PmR A /* matrix A */
)
{
int r; /* (r)ow */
int c; /* (c)olumn */

for(r=0 ; r<A->rows; r++)
{
printf("\n");
for(c=0 ;c<A->cols ;c++)
printf(" %+8.3f",*(A->pb+r *A->cols+c));
}
printf("\n");
}

/* --------------------------------- FUNCTION */
/* Do : copy A into B. */
/* ---------------------------------------- - */
void copy_mR(
PmR A,
PmR B)
{
int r; /* (r)ow */
int c; /* (c)olumn */

for ( r=0; r<A->rows; r++)
for ( c=0; c<A->cols; c++)

*(B->pb+r *B->cols+c) = *(A->pb+r *A->cols+c);
}

/* --------------------------------- MAIN */
int main(void)
{
double pbA[3][2]=
{
1, 2,
3, 4,
5, 6,
};mR A={3,2,&pbA[0][0]};

mR B ={3,2,create_mR(3,2)}; /* malloc */
/*-------------------------------- PROGRAM */
printf("\nMatrix A :\n");
p_mR(&A);

copy_mR(&A,&B);

printf("\nMatrix B :\n");
p_mR(&B);

free( B.pb);

printf("\n Press Return to continue");
getchar();
return 0;
}
Aug 3 '06 #4

P: n/a
Bernard Xhumga wrote:
You can try my work, I give an example.

Windows : Dev-C++ 4
Linux : gcc abc.c -lm Return
a.out Return

First step :
http://www.geocities.com/xhungab/tutorial/t04a.zip

Second step :
http://www.geocities.com/xhungab/tutorial/t04b.zip

Example

/* http://groups.yahoo.com/group/mathc/ */
/* ------------------------------------------ */
#include <stdio.h /* getchar(); */

/*-------------------------- Matrix structure */
typedef struct
{
int rows;
int cols;
double *pb; /* Pointer on a block of memory */
} mR, /* Type mR */
*PmR; /* Pointer on a type mR */

/* --------------------------------- FUNCTION */
/* Do : create a matrix without initialization.*/
/* ------------------------------------------- */
double * create_mR(
int r, /* (r)ow */
int c /* (c)olumn */
)
{
double *P_A;

P_A = (double *) malloc(r*c*sizeof(double));
<snip>

No need to go further than this before saying DO NOT use this code.
Calling malloc without a valid prototype in scope invokes undefined
behaviour and it DOES cause problem on real MODERN systems. Casting the
return value shuts up the compiler but it does NOT solve the problem.

Search this group and read the comp.lang.c FAQ for further information.
--
Flash Gordon
Still sigless on this computer
Aug 3 '06 #5

P: n/a
What do you think of this code.

thank.

/* .c freeware
http://groups.yahoo.com/group/mathc/ */
/* -------------------------------------------------------------------- */
#include <stdio.h>
#include <stdlib.h>
/* ------------------------------- FUNCTION */
/* Do : print matrix A. */
/* ---------------------------------------------- */
void p_mR(
double **A,
int r,
int c
)
{
int i;
int j;

for (i=0; i<r; i++)
{
for (j=0; j<c; j++)printf(" %+6.2f ",A[i][j]);
printf("\n");
}
}

/* ------------------------------- FUNCTION */
/* Do : copy A -B. */
/* ---------------------------------------------- */
void c_mR(
double **A, /* matrix A */
double **B, /* matrix B */
int r,
int c
)
{
int i;
int j;

for (i=0; i<r; i++)
for (j=0; j<c; j++) B[i][j] = A[i][j];
}

/* --------------------------------- MAIN */
int main()
{
int i;
int j;
int n;
int r=4;
int c=4;

double **A = malloc(r * sizeof( *A) );
double **B = malloc(r * sizeof( *B) );

/*------------------------- INITIALISATION */

A[0] = malloc(r * c * sizeof(**A) );
for(i=1; i<r; i++) A[i] = A[0]+i*c;

B[0] = malloc(r * c * sizeof(**B) );
for(i=1; i<r; i++) B[i] = B[0]+i*c;

/*-------------------------------- PROGRAM */

for (i=0,n=0; i<r; i++)
for (j=0; j<c; j++) A[i][j]=n++;

printf(" A : \n");
p_mR(A,r,c);
printf("\n");
c_mR(A,B,r,c);
printf(" B : \n");
p_mR(B,r,c);

printf("\n Press Return to continue");
getchar();

return 0;
}
Aug 4 '06 #6

P: n/a
Bernard Xhumga said:
What do you think of this code.
I think it fails to handle the possibility that memory resource requests can
fail.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Aug 4 '06 #7

P: n/a
Perhaps this time it is good.

/* http://groups.yahoo.com/group/mathc/ */
/* ---------------------------------------------- */
#include <stdio.h>
#include <stdlib.h>
/* --------------------------------- FUNCTION */
/* Do : print matrix A. */
/* ---------------------------------------------- */
void p_mR(
double **A,
int r,
int c
)
{
int i;
int j;

for (i=0; i<r; i++)
{
for (j=0; j<c; j++)printf(" %+6.2f ",A[i][j]);
printf("\n");
}
}

/* --------------------------------- FUNCTION */
/* Do : copy A -B. */
/* ------------------------------------------------ */
void c_mR(
double **A,
double **B,
int r,
int c
)
{
int i;
int j;

for (i=0; i<r; i++)
for (j=0; j<c; j++) B[i][j] = A[i][j];
}

/* --------------------------------- FUNCTION */
/* Do : initialize a matrix A. */
/* ------------------------------------------------ */
double **i_mR(
int r,
int c
)
{
int i;

double **A = malloc(r * sizeof( *A) );
if(!A){exit(1);}

A[0] = malloc(r * c * sizeof(**A) );
if(!A[0]){exit(1);}

for(i=1; i<r; i++)
{
A[i] = A[0]+i*c;
if(!A[i]){exit(1);}
}

return(A);
}

/* --------------------------------- FUNCTION */
/* Do : */
/* ------------------------------------------------ */
void f_mR(
double **A,
int r
)
{
int i;
int j;

for(i=0; i<r; i++) free((void *)A[i]);

free((void *)A);
}
/* --------------------------------- MAIN */
int main()
{
int i;
int j;
int n;
int r;
int c;

double **A;
double **B;
/*------------------------- INITIALISATION */
r = 7;
c = 9;

A = i_mR(r,c);
B = i_mR(r,c);
/*-------------------------------- PROGRAM */

for (i=0,n=0; i<r; i++)
for (j=0; j<c; j++) A[i][j]=n++;

printf(" A : \n");
p_mR(A,r,c);
printf("\n");
c_mR(A,B,r,c);
printf(" B : \n");
p_mR(B,r,c);

f_mR(A,r);
f_mR(B,r);

printf("\n Press Return to continue");
getchar();

return 0;
}
Aug 4 '06 #8

P: n/a
Bernard Xhumga wrote:
Perhaps this time it is good.
No. It is still obviously flawed.

<snip>
/* --------------------------------- FUNCTION */
/* Do : initialize a matrix A. */
/* ------------------------------------------------ */
double **i_mR(
int r,
int c
)
{
int i;

double **A = malloc(r * sizeof( *A) );
if(!A){exit(1);}

A[0] = malloc(r * c * sizeof(**A) );
if(!A[0]){exit(1);}
Two calls to malloc.
for(i=1; i<r; i++)
{
A[i] = A[0]+i*c;
if(!A[i]){exit(1);}
}

return(A);
}

/* --------------------------------- FUNCTION */
/* Do : */
/* ------------------------------------------------ */
void f_mR(
double **A,
int r
)
{
int i;
int j;

for(i=0; i<r; i++) free((void *)A[i]);

free((void *)A);
r+1 calls to free.
}
<snip>
int main()
Better to be explicit about not using parameters.
int main(void)
{
<snip>
printf("\n Press Return to continue");
There is no guarantee the message will be printed before waiting for
input. To stand a better chance of the message being seen

fflush(stdout);
getchar();

return 0;
}
There is also an unused variable, in my opinion this is a sign of sloppy
programming.
--
Flash Gordon
Still sigless on this computer
Aug 4 '06 #9

P: n/a
Now it is perfect ?

Perhaps

/* .c freeware
http://groups.yahoo.com/group/mathc/ */
/* -------------------------------------------------------------------------
- */
#include <stdio.h>
#include <stdlib.h>
/* --------------------------------- FUNCTION */
/* Do : initialize a matrix A. */
/* ----------------------------------------------- */
double **i_mR(
int r,
int c
)
{
int i;

double **A = malloc(r * sizeof( *A) );
if(!A)exit(1);

A[0] = malloc(r * c * sizeof(**A) );
if(!A[0])exit(1);

for(i=1; i<r; i++)
{
A[i] = A[0]+i*c;
if(!A[i])exit(1);
}

return(A);
}
/* --------------------------------- FUNCTION */
/* Do : */
/* ----------------------------------------------- */
void f_mR(
double **A
)
{
free((void *)A[0]);
free((void *)A);
}
/* --------------------------------- MAIN */
int main(void)
{
int r;
int c;

double **A;
double **B;
/*------------------------- INITIALISATION */
r = 7;
c = 9;

A = i_mR(r,c);
B = i_mR(r,c);
/*-------------------------------- PROGRAM */

f_mR(A);
f_mR(B);

printf("\n Press Return to continue");
fflush(stdout);
getchar();

return 0;
}
Aug 4 '06 #10

P: n/a
In article <eb**********@news.tiscali.fr>
Bernard Xhumga <xh****@tiscali.frwrote:
>Now [this code] is perfect ?
It seems nothing is ever *perfect* but at least it is all correct :-)

[snippage]
>double **A = malloc(r * sizeof( *A) );
if(!A)exit(1);
In a more-general-purpose routine, it would be fairly rude to exit
(with no error message, at that) here -- instead, the function
should return some sort of error indication to its caller, saying
"I was unable to allocate the memory you requested". For quite a
few purposes, simply exiting (preferably with a message first) is
sufficient, though.
A[0] = malloc(r * c * sizeof(**A) );
if(!A[0])exit(1);
This part is good enough, with the same caveat.
for(i=1; i<r; i++)
{
A[i] = A[0]+i*c;
if(!A[i])exit(1);
This test ("if(!A[i])") is unnecessary, though harmless. If A[0]
is not NULL, A[0] + i * c should never be NULL either.
>return(A);
}
[snippage]
>void f_mR(
double **A
)
{
free((void *)A[0]);
free((void *)A);
}
The two casts are unnecessary but harmless (since <stdlib.hwas
included in code I snipped above).
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Aug 7 '06 #11

P: n/a
Chris Torek <no****@torek.netwrites:
In article <eb**********@news.tiscali.fr>
Bernard Xhumga <xh****@tiscali.frwrote:
>>Now [this code] is perfect ?

It seems nothing is ever *perfect* but at least it is all correct :-)

[snippage]
>>double **A = malloc(r * sizeof( *A) );
if(!A)exit(1);

In a more-general-purpose routine, it would be fairly rude to exit
(with no error message, at that) here -- instead, the function
should return some sort of error indication to its caller, saying
"I was unable to allocate the memory you requested". For quite a
few purposes, simply exiting (preferably with a message first) is
sufficient, though.
[...]

And if you do want to call exit(), use exit(EXIT_FAILURE) rather than
exit(1); the latter is non-portable, and may not indicate failure on
some systems.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Aug 7 '06 #12

P: n/a
Thank for your help.
I have learned a lof of in few time.
Now I will try to rewrite my work.

Thank.

Aug 7 '06 #13

This discussion thread is closed

Replies have been disabled for this discussion.