函数可以返回矩阵吗? [英] Can a function returns a matrix?
问题描述
大家好,
我想要一个以矩阵作为输入并返回
矩阵的函数。 (比如,执行矩阵乘法的函数)。我怎么能这样做?
怎么办呢?有人可以帮帮我吗?
Balaji.V
Balaji .V写道:
大家好,
我想要一个以矩阵为输入并返回
$ b $的函数b矩阵。 (比如,执行矩阵乘法的函数)。我怎么能这样做?
怎么办呢?有人可以帮助我吗?
Balaji.V
定义矩阵数据结构。将指向结构的指针传递给
函数。
标准库中可能已经有矩阵代数实现,所以看看......
MQ
" MQ" < mi ************** @ gmail.comwrites:
Balaji.V写道:
>我想要一个以矩阵作为输入并返回
矩阵的函数。 (比如,执行矩阵乘法的函数)。我怎么能这样做呢?有谁能够帮我?
定义矩阵数据结构。将指向结构的指针传递给
函数。
标准库中可能已经有矩阵代数实现,所以看看...
没有矩阵代数函数C标准库。
第三方库中可能有这样的功能(
的详细信息在这里是偏离主题的。)
>
没有一种方法可以表示矩阵;如果您要使用这些库中的一个
,那么您必须遵守它所希望的表示。
如果你想自己动手,FAQ的第6部分,
< http://www.c-faq.com/>,有一些关于分配的好信息
多维数组。
-
Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>
圣地亚哥超级计算机中心< *< http: //users.sdsc.edu/~kst>
我们必须做点什么。这是事情。因此,我们必须这样做。
你可以尝试我的工作,我举个例子。
Windows:Dev-C ++ 4
Linux:gcc abc.c -lm返回
a.out返回
第一步:
http://www.geocities.com /xhungab/tutorial/t04a.zip
第二步:
http://www.geocities.com/xhungab/tutorial/t04b.zip
示例
/ * http:// groups。 yahoo.com/group/mathc/ * /
/ * -------------------------- ---------------- * /
#include< stdio.h / * getchar(); * /
/ * --------------------------矩阵结构* /
typedef struct
{
int rows;
int cols;
double * pb; / *内存块上的指针* /
} mR,/ *类型mR * /
* PmR; / *指针类型mR * /
/ * --------------------------- ------功能* /
/ *做:创建一个没有初始化的矩阵。* /
/ * ----------- -------------------------------- * /
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);
}
/ * ---------------------------------功能* /
/ * Do:打印矩阵A. * /
/ * --------------------------- ------------- - * /
void p_mR(
PmR A / *矩阵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" );
}
/ * ------------------------- --------功能* /
/ *做:将A复制到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(无效)
{
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(" \\\
Matrix A:\ n");
p_mR(& A);
copy_mR(& A,& B);
printf(" \\\
Matrix B:\ n");
p_mR(& B);
免费(B.pb);
printf(" \ n按Return键继续);
getchar();
返回0;
}
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
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.VDefine 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
"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.
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;
}
这篇关于函数可以返回矩阵吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!