有人可以帮助实现strassen算法吗? [英] Is anyone can help for implementing strassen algorithm?

查看:67
本文介绍了有人可以帮助实现strassen算法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些帮助来理解这些代码

这里有人可以向我解释这些代码的重要模块吗?


请我真的需要帮助


 //  ---------------- --------------------------- 
#include   ><  > 
 #include   <   stdlib.h  > 
 #include   <   conio.h  > 
 //  -------------------- ------------------ 
#define BREAK 2
 #define a11 a-> p [0]
 #define a12 a-> p [1]
 #define a21 a-> p [2]
 #define a22 a-> p [3]
 #define b11 b-> p [0]
 #define b12 b-> p [1]
 #define b21 b-> p [2]
 #define b22 b-> p [3]
 #define c11 c-> p [0]
 #define c12 c-> p [1]
 #define c21 c-> p [2]
 #define c22 c-> p [3]
 #define d11 d-> p [0]
 #define d12 d-> p [1]
 #define d21 d-> p [2]
 #define d22 d-> p [3]
 //  -------------------- ----------------------------- 
 typedef   double  **矩阵;
 typedef  联盟 _strassen_matrix
{
    矩阵d;
    联盟 _strassen_matrix ** p;
} * strassen_matrix;
//  ------------------------ ---------------------- 
矩阵new_matrix( int );
strassen_matrix new_strassen( int );
 void  normal_to_strassen(matrix,strassen_matrix, int );
 void  strassen_to_normal(strassen_matrix,matrix, int );
矩阵strassen_submatrix(strassen_matrix, int  int  int );
 void  copy_matrix(matrix,matrix, int );
 void  add_matrix(matrix,matrix,matrix, int );
 void  sub_matrix(矩阵,矩阵,矩阵, int );
 void  copy_strassen(strassen_matrix,strassen_matrix, int );
无效 add_strassen(strassen_matrix,strassen_matrix,strassen_matrix, int );
 void  sub_strassen(strassen_matrix,strassen_matrix,strassen_matrix, int );
 void  mul_matrix(矩阵,矩阵,矩阵, int );
 void  mul_strassen(strassen_matrix,strassen_matrix,strassen_matrix,strassen_matrix, int );
 void  print_matrix(matrix, int );
 int  minimum_power_of_two( int );

//  ------------------------ ------------------------ 
矩阵new_matrix( int  n)
{
  矩阵a =(矩阵)malloc( sizeof ( double  *)* n);
    for ( int  j =  0 ; j < n; j ++)
a [j] =( double  *)malloc( sizeof ( double )* n);
返回(a);
}
//  ------------------------ ------------------------ 
strassen_matrix new_strassen( int  n)
{
    strassen_matrix a;
    a =(strassen_matrix)malloc( sizeof (* a));
    如果(n< = BREAK)
      a-> d =(matrix)new_matrix(n);
   其他
{
注册  int  m = n/2;
      a-> p =(strassen_matrix *)malloc( 4  *  sizeof (strassen_matrix));
a11 = new_strassen(m);
a12 = new_strassen(m);
a21 = new_strassen(m);
a22 = new_strassen(m);
    }
    返回
}
//  ------------------------ ---------------------------- 
矩阵strassen_submatrix(strassen_matrix a,
 int 我,
 int  j,
 int  n
)
{
如果(n< = BREAK)
返回(a-&d; d);
其他
{
 int  cur_bit,bit_num;
strassen_matrix cur_ptr = a;
      bit_num = minimum_power_of_two(n)- 1 ;
cur_bit = n/2;
同时(cur_bit> = BREAK)
{
cur_ptr = cur_ptr-> p [(((j& cur_bit)|((i& cur_bit)*  2 ))>> bit_num)];
cur_bit>> =  1 ;
bit_num--;
}
返回(cur_ptr-> d);
}
}
//  ------------------------ ---------------------------------------- 
无效 normal_to_strassen(矩阵a,strassen_matrix b, int  n)
{
如果(n< = BREAK)
copy_matrix(a,b-> d,n);
其他
{
 int  i,j,ii,jj;
矩阵子;

 for (i =  0 ; i< n;> {
 for (j =  0 ; j< n;> {
sub = strassen_submatrix(b,i,j,n);
 (ii =  0 ; ii< break;>  for (jj =  0 ; jj< break;> sub [ii] [jj] = a [i + ii] [j + jj];
         }
}
}
}
//  ------------------------ ------------------------------------ 
无效 strassen_to_normal(strassen_matrix a,matrix b, int  n)
{
如果(n< = BREAK)
copy_matrix(a-> d,b,n);
其他
{
矩阵子;
      for ( int  i =  0 ; i< ; n;> {
 for ( int  j =  0 ; j< ; n;> {
sub = strassen_submatrix(a,i,j,n);
 for ( int  ii =  0 ; ii< ; break;>  for ( int  jj =  0 ; jj< break;> b [i + ii] [j + jj] = sub [ii] [jj];
}
}
}
}
//  ------------------------ -------------------------------- 
无效 copy_matrix(矩阵a,矩阵b, int  n)
{
   for ( int  i =  0 ; i< ; n;>  for ( int  j =  0 ; j nb b [i] [j] = a [i] [j];
}
无效 add_matrix(矩阵a,矩阵b,矩阵c, int  n)
{
 for ( int  i =  0 ; i< ; n;>  for ( int  j =  0 ; j n [c] [i] [j] = b [i] [j] + a [i] [j];
}
//  ------------------------ ------------------------------- 
无效 sub_matrix(矩阵a,矩阵b,矩阵c, int  n)
{
  for ( int  i =  0 ; i< ; n;>  for ( int  j =  0 ; j n [c] [i] [j] = a [i] [j]-b [i] [j];
}
//  ------------------------ ----------------------------------- 
无效 add_strassen(strassen_matrix a,strassen_matrix b,strassen_matrix c, int  n)
{
如果(n< = BREAK)
add_matrix(a-> d,b-> d,c-> d,n);
其他
{
 int  m = n/2;
add_strassen(a11,b11,c11,m);
add_strassen(a12,b12,c12,m);
add_strassen(a21,b21,c21,m);
add_strassen(a22,b22,c22,m);
}
}
//  ------------------------ ----------------------------------- 
 void  sub_strassen(strassen_matrix a,strassen_matrix b,strassen_matrix c, int  n)
{
如果(n< = BREAK)
{
sub_matrix(a-> d,b-> d,c-> d,n);
}
其他
{
 int  m = n/2;
sub_strassen(a11,b11,c11,m);
sub_strassen(a12,b12,c12,m);
sub_strassen(a21,b21,c21,m);
sub_strassen(a22,b22,c22,m);
}
}
//  ------------------------ ---------------------------------- 
无效 mul_matrix(矩阵a,矩阵b,矩阵c, int  n)
{
 for ( int  i =  0 ; i< ; n;>  for ( int  j =  0 ; j< n;> {
c [i] [j] =  0 . 0 ;
 for ( int  k =  0 ; k< ; n;>}
}
//  ------------------------ -------------------------------------------- 
无效 mul_strassen(strassen_matrix a,strassen_matrix b,strassen_matrix c,strassen_matrix d, int  n)
{
如果(n< = BREAK)mul_matrix(a-> d,b-> d,c-> d,n);
其他
{
 int  m = n/2;
sub_strassen(a12,a22,d11,m);
add_strassen(b21,b22,d12,m);
mul_strassen(d11,d12,c11,d21,m);
sub_strassen(a21,a11,d11,m);
add_strassen(b11,b12,d12,m);
mul_strassen(d11,d12,c22,d21,m);
add_strassen(a11,a12,d11,m);
mul_strassen(d11,b22,c12,d12,m);
sub_strassen(c11,c12,c11,m);
sub_strassen(b21,b11,d11,m);
mul_strassen(a22,d11,c21,d12,m);
add_strassen(c21,c11,c11,m);
sub_strassen(b12,b22,d11,m);
mul_strassen(a11,d11,d12,d21,m);
add_strassen(d12,c12,c12,m);
add_strassen(d12,c22,c22,m);
add_strassen(a21,a22,d11,m);
mul_strassen(d11,b11,d12,d21,m);
add_strassen(d12,c21,c21,m);
sub_strassen(c22,d12,c22,m);
add_strassen(a11,a22,d11,m);
add_strassen(b11,b22,d12,m);
mul_strassen(d11,d12,d21,d22,m);
add_strassen(d21,c11,c11,m);
add_strassen(d21,c22,c22,m);
}
}
//  ------------------------ ------------------------------------------ 
无效 print_matrix(矩阵a, int  n)
{
 for ( int  i =  0 ; i< ; n; i ++)>
{
 for ( int  j =  0 ; j< ; n; j ++)> cout<< endl;
}
}
//  ------------------------ --------------------------------------------- 
 int  minimum_power_of_two( int  n)
{
 int  i =  1 ,k =  1  ;
如果(n ==  1 )返回 ( 0 );
 while ((k<< =  1 )< n)i ++;
返回(i);
}
//  ------------------------ --------------------------- 
无效 readMatrix(矩阵a, int  n)
{
    for ( int  i =  0 ; i< ; n;>  for ( int  j =  0 ; j n cin> a [i] [j];
}
//  ------------------------ --------------------------- 
无效 main()
{
clrscr();
 int  n;
矩阵a1,a2;
strassen_matrix b1,b2,b3,b4;
cout<< " ;
cin> n;
a1 =(矩阵)new_matrix(n);
a2 =(矩阵)new_matrix(n);
b1 =(strassen_matrix)new_strassen(n);
b2 =(strassen_matrix)new_strassen(n);
b3 =(strassen_matrix)new_strassen(n);
b4 =(strassen_matrix)new_strassen(n);
cout<< " ;
readMatrix(a1,n);
cout<< " ;
readMatrix(a2,n);
normal_to_strassen(a1,b1,n);
normal_to_strassen(a2,b2,n);
mul_strassen(b1,b2,b3,b4,n);
strassen_to_normal(b3,a2,n);
cout<< " ;
print_matrix(a2,n);
getch();
}
</conio.h></stdlib.h></iostream.h> 

解决方案

是这里的某人可以向我解释每一行代码?"
编号

解释每一行-即使是一个非常琐碎的程序,也不是完全-需要一行左右的文本.这需要很多时间,而我们通常没有这样的指导.

相反,要么去找您的导师,然后让他们去做,要么回到您从那里获得代码的地方,然后(很好地)问他们是否可以尝试使用明智的变量名,并以合理的标准进行注释. /blockquote>

肯定有人可以按照您的要求解释每一行,但我怀疑有人会.您会看到,您粘贴了250多个行的程序并要求完整的解释-这将花费大量的时间和精力,您不是这样认为的吗?这里的人还有许多其他事情要做,当然可以将这几个小时花在对他们来说更重要的事情上.因此,恐怕只有一些无聊的计算机科学/数学老师有很多业余时间才能看到您的消息,您才能得到完整的解释.这不太可能.
当然,这并不意味着没有人会帮助您.花一些时间自己研究,读一本有关C和矩阵的好书,尝试理解该程序,最后您会遇到一些问题,这里的人们将很乐意为您提供帮助.


这是快速解答部分,发布问题后,您真的应该阅读说明其含义的文字.它说您的问题应该是 short specific .您的问题都不是.除非您大幅缩短问题并限制自己提出一个或几个特定问题,否则没人会给您答案.

此外,即使有人做了您要问的事情,代码也不是很好,并且您从中得到的有用信息也很少.

考虑一下您真正需要知道的是什么.我们不会为您的作业提供完整的解决方案,但是如果您觉得自己无法完成作业,请考虑是什么使您无法完成作业,然后询问该特定事项.无论如何,如果您的问题表明您不花心思尝试自己理解它,那么这里的人们至少将不愿付出任何努力来回答您的问题.首先,您需要学习.


i need some help me to understand these code

is someone here can to explain important module of these codes to me??


please i really need help


//-------------------------------------------
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
//--------------------------------------
#define BREAK 2
#define a11 a->p[0]
#define a12 a->p[1]
#define a21 a->p[2]
#define a22 a->p[3]
#define b11 b->p[0]
#define b12 b->p[1]
#define b21 b->p[2]
#define b22 b->p[3]
#define c11 c->p[0]
#define c12 c->p[1]
#define c21 c->p[2]
#define c22 c->p[3]
#define d11 d->p[0]
#define d12 d->p[1]
#define d21 d->p[2]
#define d22 d->p[3]
//-------------------------------------------------
typedef double **matrix;
typedef union _strassen_matrix
{
    matrix d;
    union _strassen_matrix **p;
} *strassen_matrix;
//----------------------------------------------
matrix  new_matrix(int);
strassen_matrix new_strassen(int);
void normal_to_strassen(matrix, strassen_matrix, int);
void strassen_to_normal(strassen_matrix, matrix, int);
matrix  strassen_submatrix(strassen_matrix, int, int, int);
void copy_matrix(matrix, matrix, int);
void add_matrix(matrix, matrix, matrix, int);
void sub_matrix(matrix, matrix, matrix, int);
void copy_strassen(strassen_matrix, strassen_matrix, int);
void add_strassen(strassen_matrix, strassen_matrix, strassen_matrix, int);
void sub_strassen(strassen_matrix, strassen_matrix, strassen_matrix, int);
void mul_matrix(matrix, matrix, matrix, int);
void mul_strassen(strassen_matrix, strassen_matrix, strassen_matrix, strassen_matrix, int);
void print_matrix(matrix, int);
int least_power_of_two(int);

//------------------------------------------------
matrix new_matrix(int n)
{
  matrix a = (matrix) malloc(sizeof(double *) * n);
   for (int j = 0; j < n; j++)
		a[j] = (double *) malloc(sizeof(double) * n);
	return(a);
}
//------------------------------------------------
strassen_matrix new_strassen(int n)
{
    strassen_matrix	a;
    a = (strassen_matrix)malloc(sizeof(*a));
    if (n <= BREAK)
      	a->d = (matrix ) new_matrix(n);
   else
	{
		register int 	m = n/2;
      a->p = (strassen_matrix *)malloc(4*sizeof(strassen_matrix));
		a11 = new_strassen(m);
		a12 = new_strassen(m);
		a21 = new_strassen(m);
		a22 = new_strassen(m);
    }
    return a;
}
//----------------------------------------------------
matrix strassen_submatrix(strassen_matrix a,
	int i,
	int j,
	int n
)
{
	if (n <= BREAK)
			return(a->d);
	else
	{
		int cur_bit, bit_num;
		strassen_matrix cur_ptr = a;
      bit_num = least_power_of_two(n)-1;
		cur_bit = n/2;
		while (cur_bit >= BREAK)
		{
			cur_ptr = cur_ptr->p[(((j & cur_bit) | ((i & cur_bit)*2)) >> bit_num)];
			cur_bit >>= 1;
			bit_num--;
		}
		return (cur_ptr->d);
	}
}
//----------------------------------------------------------------
void normal_to_strassen(matrix a,strassen_matrix b,int n)
{
	if (n <= BREAK)
		copy_matrix(a,b->d,n);
	else
	{
		int		i,j,ii,jj;
		matrix 	sub;

		for (i=0; i<n;>		{
			for (j=0; j<n;>			{
				sub = strassen_submatrix(b,i,j,n);
				for (ii=0; ii<break;>					for (jj=0; jj<break;>								sub[ii][jj] = a[i+ii][j+jj];
         }
		}
	}
}
//------------------------------------------------------------
void strassen_to_normal(strassen_matrix a,matrix b,int n)
{
	if (n <= BREAK)
	copy_matrix(a->d,b, n);
	else
	{
	matrix 	sub;
     	for (int i=0; i<n;>		{
			for (int j=0; j<n;>			{
				sub = strassen_submatrix(a,i,j,n);
				for (int ii=0; ii<break;>			  			for (int jj=0; jj<break;>								b[i+ii][j+jj] = sub[ii][jj];
			}
		}
	}
}
//--------------------------------------------------------
void copy_matrix(matrix a,matrix b,int n)
{
  	for(int i=0; i<n;> 		for(int j=0; j<n;>				b[i][j] = a[i][j];
}
void add_matrix(matrix a,matrix b,matrix c,int n)
{
	for (int i=0; i<n;>		for (int j=0; j<n;>			c[i][j] = b[i][j] + a[i][j];
}
//-------------------------------------------------------
void sub_matrix(matrix a,matrix b,matrix c,int n)
{
 for (int i=0; i<n;>  	for (int j=0; j<n;>		c[i][j] = a[i][j] - b[i][j];
}
//-----------------------------------------------------------
void add_strassen(strassen_matrix a,strassen_matrix b,strassen_matrix c,int n)
{
	if (n <= BREAK)
			add_matrix(a->d, b->d, c->d, n);
	else
	{
		int m=n/2;
		add_strassen(a11, b11, c11, m);
		add_strassen(a12, b12, c12, m);
		add_strassen(a21, b21, c21, m);
		add_strassen(a22, b22, c22, m);
	}
}
//-----------------------------------------------------------
void sub_strassen(strassen_matrix a,strassen_matrix b,strassen_matrix c,int n)
{
	if (n <= BREAK)
	{
		sub_matrix(a->d, b->d, c->d, n);
	}
	else
	{
		int m = n/2;
		sub_strassen(a11, b11, c11, m);
		sub_strassen(a12, b12, c12, m);
		sub_strassen(a21, b21, c21, m);
		sub_strassen(a22, b22, c22, m);
	}
}
//----------------------------------------------------------
void mul_matrix(matrix 	a,matrix b,matrix c,int n)
{
	for(int i=0; i<n;>	for(int j=0; j<n;>	{
		c[i][j] = 0.0;
		for(int k=0; k<n;>     	}
}
//--------------------------------------------------------------------
void mul_strassen(strassen_matrix a,strassen_matrix b,strassen_matrix c,strassen_matrix d,int n)
{
	if (n <= BREAK) mul_matrix(a->d,b->d,c->d,n);
	else
	{
		int m = n/2;
		sub_strassen(a12, a22, d11, m);
		add_strassen(b21, b22, d12, m);
		mul_strassen(d11, d12, c11, d21, m);
		sub_strassen(a21, a11, d11, m);
		add_strassen(b11, b12, d12, m);
		mul_strassen(d11, d12, c22, d21, m);
		add_strassen(a11, a12, d11, m);
		mul_strassen(d11, b22, c12, d12, m);
		sub_strassen(c11, c12, c11, m);
		sub_strassen(b21, b11, d11, m);
		mul_strassen(a22, d11, c21, d12, m);
		add_strassen(c21, c11, c11, m);
		sub_strassen(b12, b22, d11, m);
		mul_strassen(a11, d11, d12, d21, m);
		add_strassen(d12, c12, c12, m);
		add_strassen(d12, c22, c22, m);
		add_strassen(a21, a22, d11, m);
		mul_strassen(d11, b11, d12, d21, m);
		add_strassen(d12, c21, c21, m);
		sub_strassen(c22, d12, c22, m);
		add_strassen(a11, a22, d11, m);
		add_strassen(b11, b22, d12, m);
		mul_strassen(d11, d12, d21, d22, m);
		add_strassen(d21, c11, c11, m);
		add_strassen(d21, c22, c22, m);
	}
}
//------------------------------------------------------------------
void print_matrix(matrix a,int n)
{
for(int i=0;i<n;i++)>
	{
		for(int j=0;j<n;j++)>		cout<<endl;
	}
}
//---------------------------------------------------------------------
int least_power_of_two(int n )
{
	int i = 1, k = 1;
	if (n==1) return (0);
	while ((k <<= 1) < n) i++;
	return(i);
}
//---------------------------------------------------
void readMatrix(matrix a,int n)
{
   for(int i=0; i<n;>	for(int j=0; j<n;>		cin>>a[i][j];
}
//---------------------------------------------------
void main()
{
	clrscr();
	int n;
	matrix  a1, a2;
	strassen_matrix b1, b2, b3, b4;
	cout<<"Enter Size Of Matrix(Power Of 2):\n";
	cin>>n;
	a1 = (matrix) new_matrix(n);
	a2 = (matrix) new_matrix(n);
	b1 = (strassen_matrix) new_strassen(n);
	b2 = (strassen_matrix) new_strassen(n);
	b3 = (strassen_matrix) new_strassen(n);
	b4 = (strassen_matrix) new_strassen(n);
	cout<<"Enter Matrix One\n";
	readMatrix(a1,n);
	cout<<"Enter Matrix Two\n";
	readMatrix(a2,n);
	normal_to_strassen(a1,b1,n);
	normal_to_strassen(a2,b2,n);
	mul_strassen(b1,b2,b3,b4,n);
	strassen_to_normal(b3,a2,n);
	cout<<"Result Is: \n";
	print_matrix(a2,n);
	getch();
}
</conio.h></stdlib.h></iostream.h>

解决方案

"is someone here can to explain each line of code to me??"
No.

Explaining each line - even of a really trivial program, which that isn''t, quite - needs a paragraph or so of text. This takes a lot of time, which we generally don''t have for such mentoring.

Instead, either go to your tutor, and ask them to do it, or go back to where you got the code from, and ask them (nicely) if they could try using sensible variable names, and commenting things to a reasonable standard.


There definitely are people that CAN explain each line as you requested, but I doubt anyone WILL. You see, you paste a 250+ lines program and ask for complete explanation - that would take quite a lot of time and effort, don''t you think so? People here have many other things to do, and surely can spend these several hours on something more important for them. So I''m afraid your could only get complete explanation if some bored computer science/math teacher with a lot of spare time will see your message. Which is unlikely.
This doesn''t mean noone will help you, of course. Spend some effort on research yourself, read a good book on C and matrices, try to understand this program, and finally you''ll come with just a handful of question people here will gladly help with.


This is the Quick Answers section, and, upon posting a question, you really should read the text explaining what that means. Among other things it says your question should be short and specific. Your question is neither. Unless you considerably shorten your question and restrict yourself to asking one or a few specific questions, no one will give you the answer.

Besides, even if someone did what you ask, the code is not very good, and you would get very little useful information from it.

Consider what it really is that you need to know. We won''t give you a full solution to your homework, but if you feel unable to do it by yourself, consider what it is that prevents you from doing so, and ask about that specific thing. In any case, if your question reveals that you didn''t bother to put any effort into trying to understand it by yourself, then people here will at the very least be reluctant to put in the effort to answer your question. You need to want to learn, first.


这篇关于有人可以帮助实现strassen算法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆