【问题标题】:how to multiply a given Matrix by its transpose?如何将给定的矩阵乘以其转置?
【发布时间】:2014-01-09 11:15:15
【问题描述】:

我必须创建一个方法来将给定的矩阵乘以它的转置。

考虑我的矩阵是二维数组double [][] matrix;

public double[][] MultiplyByTranspose(double[][] matrix) 
{  
    return newMatrix;
}

如果您需要我的 Matrix 课程,请在 answer of this question 中查看

【问题讨论】:

  • @Octoshape 如果我不知道如何旋转矩阵,我可以尝试什么!如果你帮不上忙,请停止发布同样的旧无用评论
  • 对不起,我试图适应本网站的规则,但您似乎没有尝试过任何事情(因此可能是反对票,顺便说一句,这不是我的)。
  • 你根本不需要旋转矩阵,旋转只是一个简单的工具来可视化转置矩阵的样子。转置矩阵时,将列视为行,将行视为列,即第一行成为第一列,依此类推。
  • @Dirk 那是真的.. 我现在正在努力

标签: c# matrix transpose


【解决方案1】:

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

当然,当组合时,它可以做到更高效,但我认为你需要将转置和乘法作为分离的例程 进一步在您的代码中(您已询问矩阵旋转

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);
}

【讨论】:

  • 为什么它只适用于矩形矩阵?可能是一个愚蠢的问题,但只是想知道..
  • @Octoshape:据我所知,线性代数(转置、乘法等)只使用矩形矩阵,而不是锯齿形矩阵。
  • @Octoshape 数学上只有矩形矩阵,方形矩阵是一种特殊情况。但是使用double[][] 来描述矩阵并不能保证它是矩形的。为此,您必须使用double[,]
  • “转置”索引实际上不会比真正的转置更容易吗?
  • 矩阵乘法需要矩阵,所以左和右应该是Double[][]。此外,向量(即Double[])有两种可能:列向量或行向量,分别为Double[2][1]和Double[1][2]。如果是 2x1 矩阵(两行,每行一项) Double[][] v = new Double[] {new Double[] {a11}, new Double[] {a21}};
猜你喜欢
  • 2021-10-15
  • 2017-05-30
  • 2018-04-11
  • 2014-12-16
  • 1970-01-01
  • 2015-11-24
  • 2017-03-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多