我有一个看起来像下面这样的程序:
double[4][4] startMatrix;
double[4][4] inverseMatrix;
initialize(startMatrix) //this puts the information I want in startMatrix现在我想计算startMatrix的倒数并将其放入inverseMatrix中。我有一个用于此目的的库函数,其原型如下:
void MatrixInversion(double** A, int order, double** B)问题是,我需要知道如何将double4转换为双精度**,以赋给函数。我试着用“显而易见的方式”来做:
MatrixInversion((double**)startMatrix, 4, (double**)inverseMatrix))但这似乎并不管用。这真的是正确的做法吗?
发布于 2009-10-18 05:43:01
不,没有正确的方法来具体地做到这一点。不能将double[4][4]数组转换为double **指针。这是实现2D数组的两种可选的、不兼容的方法。需要进行一些更改:要么是函数的接口,要么是作为参数传递的数组结构。
完成后一种操作的最简单方法是创建double *[4]类型的临时“索引”数组,指向每个矩阵中每一行的开头,即使现有的索引数组与函数兼容
double *startRows[4] = { startMatrix[0], startMatrix[1], startMatrix[2] , startMatrix[3] };
double *inverseRows[4] = { /* same thing here */ };并传递这些"index“数组
MatrixInversion(startRows, 4, inverseRows);一旦函数完成工作,您就可以忘记startRows和inverseRows数组,因为结果将正确地放入原始的inverseMatrix数组中。
发布于 2009-10-18 11:08:41
由于二维数组(一个连续的内存块)和指针数组(不连续)是非常不同的东西,所以您不能将一个二维数组传递给一个使用指针到指针的函数。
你可以做的一件事是:模板。将第二个尺寸标注的大小作为模板参数。
#include <iostream>
template <unsigned N>
void print(double a[][N], unsigned order)
{
for (unsigned y = 0; y < order; ++y) {
for (unsigned x = 0; x < N; ++x) {
std::cout << a[y][x] << ' ';
}
std::cout << '\n';
}
}
int main()
{
double arr[3][3] = {{1, 2.3, 4}, {2.5, 5, -1.0}, {0, 1.1, 0}};
print(arr, 3);
}另一种更笨拙的方法可能是让函数接受指向一维数组的指针,并将宽度和高度都作为参数给出,然后自己将索引计算成二维表示形式。
#include <iostream>
void print(double *a, unsigned height, unsigned width)
{
for (unsigned y = 0; y < height; ++y) {
for (unsigned x = 0; x < width; ++x) {
std::cout << a[y * width + x] << ' ';
}
std::cout << '\n';
}
}
int main()
{
double arr[3][3] = {{1, 2.3, 4}, {2.5, 5, -1.0}, {0, 1.1, 0}};
print(&arr[0][0], 3, 3);
}当然,矩阵是值得拥有自己的类的东西(但如果您需要编写助手函数,上述内容可能仍然是相关的)。
发布于 2009-10-18 06:08:22
由于您使用的是C++,因此执行此类操作的正确方法是使用自定义类和一些模板。下面的示例相当粗略,但它很好地表达了基本观点。
#include <iostream>
using namespace std;
template <int matrix_size>
class SquareMatrix
{
public:
int size(void) { return matrix_size; }
double array[matrix_size][matrix_size];
void copyInverse(const SquareMatrix<matrix_size> & src);
void print(void);
};
template <int matrix_size>
void SquareMatrix<matrix_size>::copyInverse(const SquareMatrix<matrix_size> & src)
{
int inv_x;
int inv_y;
for (int x = 0; x < matrix_size; x++)
{
inv_x = matrix_size - 1 - x;
for (int y = 0; y < matrix_size; y++)
{
inv_y = matrix_size - 1 - y;
array[x][y] = src.array[inv_x][inv_y];
}
}
}
template <int matrix_size>
void SquareMatrix<matrix_size>::print(void)
{
for (int y = 0; y < 4; y++)
{
for (int x = 0; x < 4; x++)
{
cout << array[x][y] << " ";
}
cout << endl;
}
}
template <int matrix_size>
void Initialize(SquareMatrix<matrix_size> & matrix);
int main(int argc, char * argList[])
{
SquareMatrix<4> startMatrix;
SquareMatrix<4> inverseMatrix;
Initialize(startMatrix);
inverseMatrix.copyInverse(startMatrix);
cout << "Start:" << endl;
startMatrix.print();
cout << "Inverse:" << endl;
inverseMatrix.print();
return 0;
}
template <int matrix_size>
void Initialize(SquareMatrix<matrix_size> & matrix)
{
for (int x = 0; x < matrix_size; x++)
{
for (int y = 0; y < matrix_size; y++)
{
matrix.array[x][y] = (x+1)*10+(y+1);
}
}
}https://stackoverflow.com/questions/1584100
复制相似问题