首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过转置将给定的矩阵相乘?

如何通过转置将给定的矩阵相乘?
EN

Stack Overflow用户
提问于 2014-01-09 11:15:15
回答 1查看 4.9K关注 0票数 1

我必须创建一种方法,通过它的转置来乘以给定的矩阵。

假设我的矩阵是2d数组double [][] matrix;

代码语言:javascript
复制
public double[][] MultiplyByTranspose(double[][] matrix) 
{  
    return newMatrix;
}

如果需要我的矩阵类,请在answer of this question中检查它

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-01-09 11:22:07

最简单的方法就是实现转置和乘法。

当然,结合在一起时,可以提高效率,但我认为您需要在代码中将转置和乘法作为分离的例程(您已经询问了矩阵旋转的问题)。

代码语言:javascript
复制
public static Boolean IsRectangle(Double[][] value) {
  if (Object.ReferenceEquals(null, value))
    return false;
  else if (value.Length <= 0)
    return false;

  Double[] line = value[value.Length - 1];

  if (Object.ReferenceEquals(null, line))
    return false;

  int size = line.Length;

  for (int i = value.Length - 2; i >= 0; --i)
    if (Object.ReferenceEquals(null, value[i]))
      return false;
    else if (value[i].Length != size)
      return false;

  return true;
}

public static Double[][] Transpose(Double[][] value) {
  if (Object.ReferenceEquals(null, value))
    throw new ArgumentNullException("value");

  if (!IsRectangle(value))
    throw new ArgumentException("value should be a rectangular matrix.", "value");

  int colCount = value.Length;
  int rowCount = value[value.Length - 1].Length;

  Double[][] result = new Double[rowCount][];

  for (int i = rowCount - 1; i >= 0; --i) {
    Double[] line = new Double[colCount];
    result[i] = line;

    for (int j = colCount - 1; j >= 0; --j)
      line[j] = value[j][i];
  }

  return result;
}

// Simple quibic algorithm
public static Double[][] Multiply(Double[][] left, Double[][] right) {
  if (Object.ReferenceEquals(null, left))
    throw new ArgumentNullException("left");
  else if (Object.ReferenceEquals(null, right))
    throw new ArgumentNullException("right");

  if (!IsRectangle(left))
    throw new ArgumentException("left should be a rectangular matrix", "left");
  else if (!IsRectangle(right))
    throw new ArgumentException("right should be a rectangular matrix", "right");

  int leftRows = left.Length;
  int leftCols = left[0].Length;

  int rightRows = right.Length;
  int rightCols = right[0].Length;

  if (leftCols != rightRows)
    throw new ArgumentOutOfRangeException("right");

  Double[][] result = new Double[leftRows][];

  for (int r = leftRows - 1; r >= 0; --r) {
    Double[] leftLine = left[r];
    Double[] line = new Double[rightCols];
    result[r] = line;

    for (int c = rightCols - 1; c >= 0; --c) {
      Double s = 0.0;

      for (int i = leftCols - 1; i >= 0; --i)
        s += leftLine[i] * right[i][c];

      line[c] = s;
    }
  }

  return result;
}

...

public double[][] MultiplyByTranspose(double[][] matrix) {  
  //TODO: Check the order! Which matrix should be the first and which the second,
  // In Linear Algebra A * B != B * A 
  return Multiply(matrix, Transpose(matrix));
  // Or 
  // return Multiply(Transpose(matrix), matrix);
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21018631

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档