返回
TypeScript实现向量与矩阵,步步拆解,轻松理解!
前端
2023-10-17 16:11:39
TypeScript实现向量与矩阵,打造开发利器
前言
作为一名开发者,线性代数可能是一个既熟悉又陌生的领域。虽然我们经常在各种算法和数学计算中遇到向量和矩阵,但对于它们的基本概念和操作却可能知之甚少。本文将站在开发者的角度,用通俗易懂的语言讲解向量和矩阵,并用TypeScript实现它们,让大家快速掌握这些基础知识,为开发工作打下坚实的基础。
向量:一组有序的数
向量是线性代数中最基本的概念之一,它将一组有序的数放在一起,用一个符号来表示。向量可以是行向量,也可以是列向量。行向量是用逗号分隔的数的列表,而列向量是用分号分隔的数的列表。例如,以下是一个行向量:
(1, 2, 3)
以下是一个列向量:
[1; 2; 3]
矩阵:由数字排列成的矩形阵列
矩阵是另一个重要的线性代数概念,它由数字排列成的矩形阵列组成。矩阵可以用大写字母表示,例如A、B、C等。矩阵的元素可以用下标来访问,例如A[i][j]表示矩阵A中第i行第j列的元素。例如,以下是一个2x3矩阵:
A =
[1 2 3;
4 5 6]
TypeScript实现向量与矩阵
现在,我们来看看如何用TypeScript实现向量和矩阵。首先,我们创建一个Vector类来表示向量,然后创建一个Matrix类来表示矩阵。
class Vector {
private _elements: number[];
constructor(...elements: number[]) {
this._elements = elements;
}
get elements(): number[] {
return this._elements;
}
add(other: Vector): Vector {
const newElements = [];
for (let i = 0; i < this._elements.length; i++) {
newElements.push(this._elements[i] + other._elements[i]);
}
return new Vector(...newElements);
}
subtract(other: Vector): Vector {
const newElements = [];
for (let i = 0; i < this._elements.length; i++) {
newElements.push(this._elements[i] - other._elements[i]);
}
return new Vector(...newElements);
}
multiply(scalar: number): Vector {
const newElements = [];
for (let i = 0; i < this._elements.length; i++) {
newElements.push(this._elements[i] * scalar);
}
return new Vector(...newElements);
}
dotProduct(other: Vector): number {
let dotProduct = 0;
for (let i = 0; i < this._elements.length; i++) {
dotProduct += this._elements[i] * other._elements[i];
}
return dotProduct;
}
magnitude(): number {
let magnitude = 0;
for (let i = 0; i < this._elements.length; i++) {
magnitude += this._elements[i] ** 2;
}
return Math.sqrt(magnitude);
}
normalize(): Vector {
const magnitude = this.magnitude();
const newElements = [];
for (let i = 0; i < this._elements.length; i++) {
newElements.push(this._elements[i] / magnitude);
}
return new Vector(...newElements);
}
toString(): string {
return `(${this._elements.join(', ')})`;
}
}
class Matrix {
private _rows: number;
private _columns: number;
private _elements: number[][];
constructor(rows: number, columns: number, elements: number[][]) {
this._rows = rows;
this._columns = columns;
this._elements = elements;
}
get rows(): number {
return this._rows;
}
get columns(): number {
return this._columns;
}
get elements(): number[][] {
return this._elements;
}
add(other: Matrix): Matrix {
if (this._rows !== other._rows || this._columns !== other._columns) {
throw new Error('Matrices must have the same dimensions');
}
const newElements = [];
for (let i = 0; i < this._rows; i++) {
const newRow = [];
for (let j = 0; j < this._columns; j++) {
newRow.push(this._elements[i][j] + other._elements[i][j]);
}
newElements.push(newRow);
}
return new Matrix(this._rows, this._columns, newElements);
}
subtract(other: Matrix): Matrix {
if (this._rows !== other._rows || this._columns !== other._columns) {
throw new Error('Matrices must have the same dimensions');
}
const newElements = [];
for (let i = 0; i < this._rows; i++) {
const newRow = [];
for (let j = 0; j < this._columns; j++) {
newRow.push(this._elements[i][j] - other._elements[i][j]);
}
newElements.push(newRow);
}
return new Matrix(this._rows, this._columns, newElements);
}
multiply(other: Matrix | number): Matrix {
if (typeof other === 'number') {
const newElements = [];
for (let i = 0; i < this._rows; i++) {
const newRow = [];
for (let j = 0; j < this._columns; j++) {
newRow.push(this._elements[i][j] * other);
}
newElements.push(newRow);
}
return new Matrix(this._rows, this._columns, newElements);
} else {
if (this._columns !== other._rows) {
throw new Error('Matrices cannot be multiplied');
}
const newElements = [];
for (let i = 0; i < this._rows; i++) {
const newRow = [];
for (let j = 0; j < other._columns; j++) {
let sum = 0;
for (let k = 0; k < this._columns; k++) {
sum += this._elements[i][k] * other._elements[k][j];
}
newRow.push(sum);
}
newElements.push(newRow);
}
return new Matrix(this._rows, other._columns, newElements);