矩阵类-实现矩阵的基本变换

一个js模拟的矩阵类,包含遍历元素、矩阵加、减、乘,以及图形输出

 

//矩阵类
class Matrix {
    //将一个数组构建成一个矩阵,行Row、列Column
    constructor(data,Row,Column){
        this.data=data||[];
        this.Row=Row;//
        this.Column=Column;//
    }
    //根据行、列返回矩阵元素
    getItem(r,c){
        return this.data[r*this.Column+c]||0;
    }
    //根据行、列设置矩阵元素
    setItem(r,c,item){
        this.data[r*this.Column+c]=item;
    }
    //换行
    swapRow(r1,r2){
        for(let c=0;c<this.Column;c++){
            const cache=this.getItem(r1,c)
            this.setItem(r1,c,this.getItem(r2,c))
            this.setItem(r2,c,cache);
        }
    }
    oneRowEach(r,callback){
        for(let c=0;c<this.Column;c++){
            callback(this.getItem(r,c),r,c)
        }
    }
    //按行遍历矩阵元素,返回元素item,行r,列c
    rowEach(callback){
        for(let r=0;r<this.Row;r++){
            for(let c=0;c<this.Column;c++){
                callback(this.getItem(r,c),r,c)
            }
        }
    }
    //按竖遍历矩阵元素,返回元素item,行r,列c
    columnEach(callback){
        for(let c=0;c<this.Column;c++){
            for(let r=0;r<this.Row;r++){
                callback(this.getItem(r,c),r,c)
            }
        }
    }
    //行循环
    oneRowMap(r,callback){
        this.oneRowEach(r,(item,r,c)=> {
            this.setItem(r,c,callback(item,r,c));
        })
    }
    //按行map矩阵元素
    rowMap(callback){
        this.rowEach((item,r,c)=> {
            this.setItem(r,c,callback(item,r,c));
        })
    }
    //相加
    add(matrix){
        if(matrix instanceof Matrix&& this.Row === matrix.Row && this.Column === matrix.Column){
            const nMatrix=new Matrix([],this.Row,this.Column)
            this.rowEach(function (item,r,c) {
                nMatrix.setItem(r,c,item+matrix.getItem(r,c))
            })
            return nMatrix;
        }else{
            throw '方法plus 参数错误';
        }
    }
    //相减
    sub(matrix){
        if(matrix instanceof Matrix&& this.Row === matrix.Row && this.Column === matrix.Column){
            const nMatrix=new Matrix([],this.Row,this.Column)
            this.rowEach(function (item,r,c) {
                nMatrix.setItem(r,c,item-matrix.getItem(r,c))
            })
            return nMatrix;
        }else{
            throw '方法minus 参数错误';
        }
    }
    //相乘
    multiply(obj){
        if(obj instanceof Matrix){
            return this.multiplyMatrix(obj)
        }else if(typeof obj=='number'){
            return this.multiplyNumber(obj)
        }else{
            throw 'multiply 输入的参数类型错误';
        }
    }
    //矩阵与数相乘,返回一个新的矩阵
    multiplyNumber(number){
        const nMatrix=new Matrix([],this.Row,this.Column)
        this.rowEach((item,r,c)=> {
            nMatrix.setItem(r,c,item*number)
        })
        return nMatrix;
    }
    //矩阵与矩阵相乘 矩阵A的行必须与矩阵B的列数相等
    multiplyMatrix(matrix){
        if(this.Row!==matrix.Column){
            throw '矩阵A的行必须与矩阵B的列数相等';
        }
        const nMatrix=new Matrix([],this.Row,matrix.Column)
        for(let r=0;r<this.Row;r++){
            for(let mc=0;mc<matrix.Column;mc++){
                let num=0;
                for(let c=0;c<this.Column;c++){
                    num=num+this.getItem(r,c)*matrix.getItem(c,mc)
                }
                nMatrix.setItem(r,mc,num)
            }
        }
        return nMatrix;

    }
    //转换成字符图形
    toString(){
        let str='[';
        for(let r=0;r<this.Row;r++){
            str=str+'\n'
            for(let c=0;c<this.Column;c++){
                if(r==this.Row-1&&c==this.Column-1){
                    str=str+this.getItem(r,c);
                }else{
                    str=str+this.getItem(r,c)+',';
                }
            }
        }
        str=str+'\n]'
        return str;
    }
}
module.exports=Matrix;
// //demo
// const m=new Matrix([1,2,3,4,5,6],3,2);
// const m2=new Matrix([1,2,3,4,5,6],2,3);
// console.log('按行遍历矩阵元素,返回元素item,行r,列c')
// m.rowEach(function (item,r,c) {
//     console.log(item,r,c)
// })
// console.log('按竖遍历矩阵元素,返回元素item,行r,列c')
// m.columnEach(function (item,r,c) {
//     console.log(item,r,c)
// })
//
// console.log('输出矩阵图形');
// console.log(m.toString())
// console.log(m2.toString())
//
// console.log('矩阵与数相乘');
// const m4=m.multiply(3)
// console.log(m4.toString())
//
// console.log('矩阵与矩阵相乘');
// const m3=m.multiply(m2)
// console.log(m3.toString())
//

 

 

结果

按行遍历矩阵元素,返回元素item,行r,列c
1 0 0
2 0 1
3 1 0
4 1 1
5 2 0
6 2 1
按竖遍历矩阵元素,返回元素item,行r,列c
1 0 0
3 1 0
5 2 0
2 0 1
4 1 1
6 2 1
输出矩阵图形
[
1,2,
3,4,
5,6
]
[
1,2,3,
4,5,6
]
矩阵与矩阵相乘
[
9,12,15,
19,26,33,
29,40,51
]
矩阵与数相乘
[
3,6,
9,12,
15,18
]

原文链接: https://www.cnblogs.com/caoke/p/10413836.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    矩阵类-实现矩阵的基本变换

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/290208

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月15日 下午12:42
下一篇 2023年2月15日 下午12:43

相关推荐