import { Matrix } from "./Matrix" ; class Vector { data : Array < number > ; param { Array < number > } . . data constructor ( [ ... data ] ) { this . data = data ; } param { vector } data return { Array < number > } add ( data ) { let tempAry = [ ] ; if ( data instanceof Vector && data . data . length == this . data . length ) { for ( let i = 0 ; i < data . data . length ; i ++ ) { tempAry . push ( this . data [ i ] + data . data [ i ] ) ; } return tempAry ; } } param { vector } data return { Array < Number > } mult ( data ) { let tempAry = [ ] ; if ( data instanceof Vector && data . data . length == this . data . length ) { for ( let i = 0 ; i < data . data . length ; i ++ ) { tempAry . push ( data . data [ i ] - this . data [ i ] ) ; } return ( this , tempAry ) ; } else { throw Error ( "参数必须是向量实例或两个向量长度需相同" ) ; } } param { Array < number > | Vector } data return { number } mod ( data ) { let total = 0 ; if ( data instanceof Array || data instanceof Vector ) { if ( data instanceof Array ) { for ( let i = 0 ; i < data . length ; i ++ ) { total += data [ i ] ** 2 ; } return ( this , Math . sqrt ( total ) ) ; } if ( data instanceof Vector ) { return this . mod ( data . data ) ; } else { throw TypeError ( "参数类型错误,参数必须是向量类或者数组类" ) ; } } } param { number | Array < number > | vector } data return { number } dotProduct ( data ) { let total = 0 ; if ( typeof data === "number" ) { for ( let i = 0 ; i < this . data . length ; i ++ ) { this . data [ i ] *= data ; } } if ( data instanceof Array ) { if ( this . data . length == data . length ) { for ( let i = 0 ; i < data . length ; i ++ ) { total += ( this . data [ i ] * data [ i ] ) ; } return ( this , total ) ; } else { throw TypeError ( "参数长度不一致!" ) ; } } if ( data instanceof Vector ) { return ( this , this . dotProduct ( data . data ) ) ; } } param { number | Array < number > | Vector } data return { any } product ( data ) { } param { vector | Array < number > } dada return { boolean } isVertical ( dada ) { if ( dada instanceof Array ) { if ( this . dotProduct ( dada ) == 0 ) { return true ; } } if ( dada instanceof Vector ) { this . isVertical ( dada . data ) ; } return ( this , false ) ; } param { Vector | Array < number > } data return { boolean } isHorizontal ( data ) { let product = this . mod ( this . data ) * this . mod ( data ) * this . angle ( data ) ; if ( product == 0 ) { return true ; } else { return false ; } } param { vector | Array < number > } data return { number } angle ( data ) { if ( data instanceof Array ) { var total = 0 ; if ( data . length == this . data . length ) { for ( let i = 0 ; i < data . length ; i ++ ) { total += ( this . data [ i ] * data [ i ] ) ; } } return ( this , total / ( this . mod ( data ) * this . mod ( this . data ) ) ) ; } if ( data instanceof Vector ) { this . angle ( data . data ) ; } } } export { Vector } ;