矩阵相乘
1. 矩阵和矩阵相乘
2. 矩阵和向量相乘
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication6 { class Program { static void Main(string[] args) { Vector3 c = new Vector3(1, 2, 3); Matrix3x3 x1 = new Matrix3x3() { m11 = 1,m12 = -5,m13 = 3, m21 = 0,m22 = -2,m23 = 6, m31 = 7,m32 = 2,m33 = -4 }; Matrix3x3 x2 = new Matrix3x3() { m11 = -8, m12 = 6, m13 = 1, m21 = 7, m22 = 0, m23 = -3, m31 = 2, m32 = 4, m33 = 5 }; c = c * x1; Console.WriteLine(x1 * x2); Console.WriteLine(""); Console.WriteLine(c); Console.ReadLine(); } } class Matrix3x3 { public float m11, m12, m13; public float m21, m22, m23; public float m31, m32, m33; public Matrix3x3(){} public Matrix3x3( float m11,float m12,float m13, float m21,float m22,float m23, float m31,float m32,float m33) { this.m11 = m11; this.m12 = m12; this.m13 = m13; this.m21 = m21; this.m22 = m22; this.m23 = m23; this.m31 = m31; this.m32 = m32; this.m33 = m33; } //矩阵相乘 public static Matrix3x3 operator *(Matrix3x3 a,Matrix3x3 b) { Matrix3x3 r = new Matrix3x3(); r.m11 = a.m11 * b.m11 + a.m12 * b.m21 + a.m13 * b.m31; r.m12 = a.m11 * b.m12 + a.m12 * b.m22 + a.m13 * b.m32; r.m13 = a.m11 * b.m13 + a.m12 * b.m23 + a.m13 * b.m33; r.m21 = a.m21 * b.m11 + a.m22 * b.m21 + a.m23 * b.m31; r.m22 = a.m21 * b.m12 + a.m22 * b.m22 + a.m23 * b.m32; r.m23 = a.m21 * b.m13 + a.m22 * b.m23 + a.m23 * b.m33; r.m31 = a.m31 * b.m11 + a.m32 * b.m21 + a.m33 * b.m31; r.m32 = a.m31 * b.m12 + a.m32 * b.m22 + a.m33 * b.m32; r.m33 = a.m31 * b.m13 + a.m32 * b.m23 + a.m33 * b.m33; return r; } //向量乘以矩阵(行向量)vABC public static Vector3 operator *(Vector3 a, Matrix3x3 b) { return new Vector3 ( a.x * b.m11 + a.y * b.m21 + a.z * b.m31, a.x * b.m12 + a.y * b.m22 + a.z * b.m32, a.x * b.m13 + a.y * b.m23 + a.z * b.m33 ); } public override string ToString() { return string.Format("{0} {1} {2}\r\n{3} {4} {5}\r\n{6} {7} {8}\r\n" , m11, m12, m13, m21, m22, m23, m31, m32, m33); } } class Vector3 { public float x, y, z; public Vector3(float x,float y,float z) { this.x = x; this.y = y; this.z = z; } public override string ToString() { return string.Format("{0} {1} {2}", x, y, z); } } }
如果你感兴趣,你可以把你妹妹介绍给我