473,770 Members | 4,999 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4851
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.comwri tes:
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
multidimensiona l arrays.

--
Keith Thompson (The_Other_Keit h) 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*size of(double));

if(P_A==NULL){e xit(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("\nMatri x A :\n");
p_mR(&A);

copy_mR(&A,&B);

printf("\nMatri x 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*size of(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

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

Similar topics

6
1487
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 numarray. I intend to treat the function as a variable, so ufuncs are probably not appropriate-- I
2
3004
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 CComplexMatrixTemp x = chol(eye(6)), similar to MATLAB syntax.
3
1394
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 templatised so that it can be a matrix of any type. It has three data members, two integers for the number of rows and columns in it, and a vector of size rows*columns to hold all the data.
3
1474
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 ought to make it a member function. ie.
4
1400
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 subsequent Matrix class. In MatrixInterface class, I overloaded the << operator by calling a pure virtual function PrintDebugMessage(ostream &os); then I can implement the function on individual Matrix classes later.
7
1915
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 121, in b_maker b = map(function,b) TypeError: 'matrix' object is not callable This is on a windows32 system, using python 2.5 with the numpy library. In the following code data is a nested list of type float, target is a list of type float...
1
2213
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 embedding it). I have a function that executes a string buffer of python code, fetches a function from its global dictionary then calls it. When the function code returns a local variable, PyObject_Call() appears to be returning garbage. ...
5
1679
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 an array of triangles for the first param I get a compile error: error: no match for call to ‘(point) (triangle&, line&)’ Any ideas what may be causing this? Compiled (or not) with G++: g++ -Wall "MovesShootDetect.cpp" -lGL -lGLU `sdl-config...
8
1763
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 vectors and returns the result. u and v are two 3x1 matrix. when I import the function, error message show like this Traceback (most recent call last): File "<stdin>", line 1, in ?
0
9592
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
10231
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
10059
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...
1
10005
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9871
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
8887
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5313
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5452
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2817
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.