473,320 Members | 1,876 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,320 software developers and data experts.

Can a function returns a matrix?

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
12 4806
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
"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

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
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
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
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
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
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
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
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 (40°39.22'N, 111°50.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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Matt Feinstein | last post by:
Is there an optimal way to apply a function to the elements of a two-d array? What I'd like to do is define some function: def plone(x): return x+1 and then apply it elementwise to a 2-D...
2
by: Ryan Mitchley | last post by:
Hi all I have the functions friend CComplexMatrixTemp eye(const size_t nN); and friend CComplexMatrixTemp & chol(CComplexMatrixTemp &z); I would like create expressions of the form...
3
by: Craig Nicol | last post by:
Hi, Although I've been using C++ for a while, I've only recently started writing my own template classes so forgive me if this is a silly question. I have a matrix class, mgMatrix, that is...
3
by: bluekite2000 | last post by:
I d rather have a Matlab syntax such as: // create a Matrix of size 3,4 and fills it w/ random values. Matrix<double> M=rand(3,4) But since this function changes the state of the object, I...
4
by: Jason | last post by:
/* I have never use template before, so bear with me. Here is what I am trying to do: I declare an abstract base class MatrixInterface with template to define an interface for all my...
7
by: kulpojke | last post by:
I am trying to map a function to the contents of an array using the map() function. However, whenever the following function is called an error message is returned complaining that: ... line...
1
by: joeedh | last post by:
Hi I'm getting extremely odd behavior. First of all, why isn't PyEval_EvalCode documented anywhere? Anyway, I'm working on blender's python integration (it embeds python, as opposed to python...
5
by: Envergure | last post by:
I wrote a function to find the point of intersection of a line and a plane in three-space. This function works fine and returns the correct result. However, when I call it using an element from...
8
by: aeneng | last post by:
Hello everyone, I am just starting to use python in numerical cacluation. I need you to help me to see what's wrong with the following piece of codes, which computes the cross product of two...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.