- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我想为我冗长的问题道歉,这只是我第二次使用这个网站。在不久的将来,我会确保我的问题得到有序的综合。
我正在编写一个 n x m 矩阵类,其中包括重载乘法。目前,仅当第一个输入矩阵的列与第二个矩阵的行匹配时,代码才适用于 n x n 和 n x m 矩阵乘法,而不是相反。例如:
a1 a2 * b1
b2
这会起作用(抱歉,我没有足够的声誉点来发布图像,但可以将其视为 1x2 矩阵 * 2x1 矩阵)。
a1 * b1 b2
a2
但是这种类型的输入不起作用。
我的矩阵类由:
#include<iostream>
#include <stdlib.h>
using namespace std;
class dymatrix
{
friend ostream & operator << (ostream &os, const dymatrix &om);
friend istream & operator >> (istream &is, dymatrix &om);
private:
int rows;
int columns;
double *matrix;
public:
dymatrix(){cout<<"Default constructor called"<<endl; columns = 0; rows=0; matrix=0;}
~dymatrix(){cout<<"Destructor called"<<endl;delete[] matrix;}
dymatrix(int inrows, int incolumns) //Parametrised objects, matrix database is defined here to store data.
{
rows = inrows;
columns = incolumns;
matrix = new double [inrows*incolumns];
for (int i=0; i<inrows*incolumns; i++)
{
matrix[i]=0;
}
}
int lengthr() const {return rows;} //Returns number of rows.
int lengthc() const {return columns;} //Return number of columns.
dymatrix &operator=( dymatrix &arr); //This is the assignment member function.
dymatrix(dymatrix&); //This is the deep copy member function.
dymatrix operator*(const dymatrix& arr)const ; //Overloading "*" operator.
double & operator[](const int n) //Overloading "[]" operator, this is important !!!
{
if(n<0 || n>=rows*columns)
{
cout<<"Error: trying to access matrix element out of bounds"<<endl;
exit(1);
}
return matrix[n];
}
int index(int j, int i) const //This member function returns the position of each index.
{
if (j > 0 && j <=rows && i > 0 && i <=columns)
{
//cout<<"i is "<<i<<" j is "<<j<<endl<<endl<<"row is "<<rows<<" columns is "<<columns<<endl;
return (i-1)+(j-1)*columns;
}
else {cout<<"Error, out of range"<<endl<<"i is "<<i<<" j is "<<j<<endl<<endl<<"row is "<<rows<<" columns is "<<columns<<endl; exit (1);}
}
double & operator()(int j, int i) const {return matrix[index(j,i)];} //The operator () returns the position of j and i in 1D array.
}; //Class End.
其中“index”成员函数将矩阵中每个元素的位置返回到一维数组中:
int index(int j, int i) const //This member function returns the position of each index.
{
if (j > 0 && j <=rows && i > 0 && i <=columns)
{
//cout<<"i is "<<i<<" j is "<<j<<endl<<endl<<"row is "<<rows<<" columns is "<<columns<<endl;
return (i-1)+(j-1)*columns;
}
else {cout<<"Error, out of range"<<endl<<"i is "<<i<<" j is "<<j<<endl<<endl<<"row is "<<rows<<" columns is "<<columns<<endl; exit (1);}
}
double & operator()(int j, int i) const {return matrix[index(j,i)];} //The operator () returns the position of j and i in 1D array.
这是我在类外重载乘法“*”运算符的部分:
dymatrix dymatrix:: operator * (const dymatrix &arr) const //Overloading "*" operator.
{
if (columns != arr.rows)
{
cerr<<"SIZE DO NOT MATCH, YOU FAIL"<<endl; exit(1);
}
dymatrix new_matrix(rows,arr.columns);
for (int j = 1; j <= rows; j++)
{
for (int i = 1; i <= arr.columns; i++)
{
for (int k = 1; k <= columns; k++)
{
new_matrix.matrix[index(j,i)]= new_matrix.matrix[index(j,i)]+ matrix[index(j,k)]*arr(k,i);
cout<<"new_matrix = "<<new_matrix<<endl<<endl;
}
}
}
return new_matrix;
}
这里给出了 int main:
int main()
{
dymatrix a1;
cin >> a1; //Define the rows of the matrix
cout << a1<<endl<<endl;
dymatrix a2;
cin >> a2;
cout << a2<<endl<<endl;
dymatrix resu_a3;
resu_a3 = a1*a2;
cout<<"Multiplication = "<<resu_a3<<endl;
return 0;
}
所以我在运行代码后得到的错误消息是我自己在“index”成员函数中编写的错误消息:
“错误,超出范围”。
此错误消息表明 i 和 j 分别小于 0 或大于现有列数和行数,我不明白为什么会这样。
我的完整代码在这里:
#include<iostream>
#include <stdlib.h>
using namespace std;
class dymatrix
{
friend ostream & operator << (ostream &os, const dymatrix &om);
friend istream & operator >> (istream &is, dymatrix &om);
private:
int rows;
int columns;
double *matrix;
public:
dymatrix(){cout<<"Default constructor called"<<endl; columns = 0; rows=0; matrix=0;}
~dymatrix(){cout<<"Destructor called"<<endl;delete[] matrix;}
dymatrix(int inrows, int incolumns) //Parametrised objects, matrix database is defined here to store data.
{
rows = inrows;
columns = incolumns;
matrix = new double [inrows*incolumns];
for (int i=0; i<inrows*incolumns; i++)
{
matrix[i]=0;
}
}
int lengthr() const {return rows;} //Returns number of rows.
int lengthc() const {return columns;} //Return number of columns.
dymatrix &operator=( dymatrix &arr); //This is the assignment member function.
dymatrix(dymatrix&); //This is the deep copy member function.
dymatrix operator*(const dymatrix& arr)const ; //Overloading "*" operator.
double & operator[](const int n) //Overloading "[]" operator, this is important !!!
{
if(n<0 || n>=rows*columns)
{
cout<<"Error: trying to access matrix element out of bounds"<<endl;
exit(1);
}
return matrix[n];
}
int index(int j, int i) const //This member function returns the position of each index.
{
if (j > 0 && j <=rows && i > 0 && i <=columns)
{
//cout<<"i is "<<i<<" j is "<<j<<endl<<endl<<"row is "<<rows<<" columns is "<<columns<<endl;
return (i-1)+(j-1)*columns;
}
else {cout<<"Error, out of range"<<endl<<"i is "<<i<<" j is "<<j<<endl<<endl<<"row is "<<rows<<" columns is "<<columns<<endl; exit (1);}
}
double & operator()(int j, int i) const {return matrix[index(j,i)];} //The operator () returns the position of j and i in 1D array.
}; //Class End.
dymatrix::dymatrix(dymatrix &arr) //This is the copy constructor for deep copying.
{
matrix = 0; rows = arr.lengthr(); columns = arr.lengthc();
if (rows*columns >0)
{
matrix = new double [rows*columns];
for (int i =0; i < rows*columns; i++)
{
matrix[i] = arr[i];
}
}
}
dymatrix & dymatrix ::operator = (dymatrix &arr)
{
if(&arr == this) return *this; // no self assignment
delete[] matrix; matrix=0; rows=columns=0;
rows=arr.lengthr();
columns = arr.lengthc();
if(rows*columns>0)
{
matrix=new double[rows*columns];
// Copy values into new array
for(int i=0;i<rows*columns;i++) matrix[i] = arr[i];
}
return *this; // Special pointer!!
}
istream & operator >> (istream &is, dymatrix &om) //Overloading ">>" operator here to
{
cout<<"Please enter the number of rows you want"<<endl;
is >> om.rows; //Inputting number of rows.
cout<<"Enter the number of columns you want"<<endl;
is >> om.columns; //Inputting number of columns.
cout<<"Enter matrix"<<endl;
om.matrix = new double [om.rows*om.columns]; //Making a dynamic array here to put the data in.
for (int j = 1; j <= om.rows; j++)
{
for (int i = 1; i <= om.columns; i++)
{
is >> om.matrix[om.index(j,i)]; //Putting in the data into this dynamic array for each element.
}
}
return is;
}
ostream & operator << (ostream &os, const dymatrix &om) //To output the matrix in an standard matrix way
{
for(int j= 1; j<=om.rows; j++)
{
os<<endl<<endl;
for (int i = 1; i <=om.columns;i++)
{
os << om.matrix[om.index(j,i)]<<"\t"; //Similar method used in istream.
}
}
return os;
}
dymatrix dymatrix:: operator * (const dymatrix &arr) const //Overloading "*" operator.
{
if (columns != arr.rows)
{
cerr<<"SIZE DO NOT MATCH, YOU FAIL"<<endl; exit(1);
}
dymatrix new_matrix(rows,arr.columns);
for (int j = 1; j <= rows; j++)
{
for (int i = 1; i <= arr.columns; i++)
{
for (int k = 1; k <= columns; k++)
{
new_matrix.matrix[index(j,i)]= new_matrix.matrix[index(j,i)]+ matrix[index(j,k)]*arr(k,i);
cout<<"new_matrix = "<<new_matrix<<endl<<endl;
cout<<"k "<<k<<endl<<" arr.columns is "<<arr.columns<<endl<<"columns is "<<columns<<endl<<endl;
}
}
}
return new_matrix;
}
int main()
{
dymatrix a1;
cin >> a1; //Define the rows of the matrix
cout << a1<<endl<<endl;
dymatrix a2;
cin >> a2;
cout << a2<<endl<<endl;
dymatrix resu_a3;
resu_a3 = a1*a2;
cout<<"Multiplication = "<<resu_a3<<endl;
return 0;
}
最佳答案
在您的乘法运算符代码中,这一行是错误的:
new_matrix.matrix[index(j,i)]= new_matrix.matrix[index(j,i)]+ matrix[index(j,k)]*arr(k,i);
问题是 index(j,i)
实际上是 this->index(j,i)
,而不是 new_matrix.index(j, i)
如你所料...
您应该将其替换为:
new_matrix(j,i) += matrix[index(j,k)]*arr(k,i);
但真正的问题是:您是否有充分的理由实现自己的矩阵类,而不是使用像 Armadillo 这样经过实战检验的矩阵库?
关于c++ - 重载矩阵乘法 "* "运算符有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28902362/
如果矩阵A在X中,矩阵B在Y中。 进行乘法运算只是 Z = X*Y。正确假设两个数组的大小相同。 如何使用 for 循环计算它? 最佳答案 ja72 的anwser 是错误的,请查看我在其下的评论以了
我有一个 C 程序,它有 n 次乘法(单次乘法和 n 次迭代),我发现另一个逻辑有 n/2 次迭代(1 次乘法 + 2 次加法)。我知道两者都是 O(n) 的复杂性。但就 CPU 周期而言。哪个更快?
我有一个矩阵x: x <- matrix(1:8, nrow = 2, ncol = 4, byrow = 2) # [,1] [,2] [,3] [,4] #[1,] 1 2 3
我有一个矩阵x: x <- matrix(1:8, nrow = 2, ncol = 4, byrow = 2) # [,1] [,2] [,3] [,4] #[1,] 1 2 3
我正在创建一个基于电影 InTime 的 Minecraft 插件,并尝试创建代码,在玩家死亡时玩家将失去 25% 的时间。 当前代码是: String minus = itapi.getTimeSt
我正在尝试将 2 个矩阵与重载的 * 运算符相乘并打印结果。虽然看起来我不能为重载函数提供超过 1 个参数。如何将这两个矩阵传递给重载函数?请在下面查看我的实现。 #include #include
为什么在 Java 中使用 .*?例如 double probability = 1.*count/numdata; 给出相同的输出: double probability = count/numda
如果我尝试将两个值与单位相乘,则会出现意外错误。 $test: 10px; .testing{ width: $test * $test; } result: 100px*px isn't a v
我正在尝试计算库存中所有产品的总值(value)。表中的每种产品都有价格和数量。因此,我需要将每种产品的价格乘以数量,然后将所有这些加在一起以获得所有产品的总计。根据上一个问题,我现在可以使用 MyS
我正在尝试计算库存中所有产品的总值(value)。表中的每种产品都有价格和数量。因此,我需要将每种产品的价格乘以数量,然后将所有这些加在一起以获得所有产品的总计。根据上一个问题,我现在可以使用 MyS
大家好,我有以下代码行 solution first = mylist.remove((int)(Math.random() * mylist)); 这给了我一个错误说明 The operator *
我必须做很多乘法运算。如果我考虑效率,那么我应该使用位运算而不是常规的 * 运算吗?如果有差异如何进行位运算?提前致谢.. 最佳答案 不,您应该使用乘法运算符,让优化编译器决定如何最快地完成它。 您会
两个 n 位数字 A 和 B 的乘法可以理解为移位的总和: (A << i1) + (A << i2) + ... 其中 i1, i2, ... 是 B 中设置为 1 的位数。 现在让我们用 OR
我想使用 cuda 6 进行 bool 乘法,但我无法以正确的方式做到这一点。B 是一个 bool 对称矩阵,我必须进行 B^n bool 乘法。 我的 C++ 代码是: for (m=0; m
我正在编写一个定点类,但遇到了一些问题...乘法、除法部分,我不确定如何模拟。我对部门运算符(operator)进行了非常粗暴的尝试,但我确信这是错误的。到目前为止,它是这样的: class Fixe
我有TABLE_A我需要创建 TABLE_A_FINAL 规则: 在TABLE_A_FINAL中我们有包含 ID_C 的所有可能组合的行如果在 TABLE_A与 ID_C 的组合相同我们乘以 WEIG
这个问题在这里已经有了答案: Simple way to repeat a string (32 个答案) 关闭 6 年前。 我有一个任务是重复字符乘以它例如用户应该写重复输入 3 R 输出的字母和
我最近学习了C++的基础知识。我发现了一些我不明白的东西。这是让我有点困惑的程序。 #include using namespace std; int main()
我有两个列表: list_a = list_b = list(范围(2, 6)) final_list = [] 我想知道如何将两个列表中的所有值相乘。我希望我的 final_list 包含 [2*2
如何修改此代码以适用于任何基数? (二进制、十六进制、基数 10 等) int mult(int a, int b, int base){ if((a<=base)||(b<=base)){
我是一名优秀的程序员,十分优秀!