57 lines
No EOL
1.5 KiB
Text
57 lines
No EOL
1.5 KiB
Text
|
|
|
|
class Vec{
|
|
static add(first: List, second: List){
|
|
var count = first.count.min(second.count)
|
|
var result = []
|
|
for(i in 0...count){
|
|
result.add(first[i] + second[i])
|
|
}
|
|
return result
|
|
}
|
|
|
|
static sub(first: List, second: List){
|
|
var count = first.count.min(second.count)
|
|
var result = []
|
|
for(i in 0...count){
|
|
result.add(first[i] - second[i])
|
|
}
|
|
return result
|
|
}
|
|
|
|
static mul(first: List, second: Any){
|
|
if(first is Num && second is List) return mul(second, first)
|
|
var secondNum = second is Num
|
|
var count = secondNum ? first.count : (first.count.min(second.count))
|
|
var result = []
|
|
for(i in 0...count){
|
|
result.add(first[i] * (secondNum?second:second[i]))
|
|
}
|
|
return result
|
|
}
|
|
|
|
static div(first: List, second: Any){
|
|
var secondNum = second is Num
|
|
var count = secondNum ? first.count : first.count.min(second.count)
|
|
var result = []
|
|
for(i in 0...count){
|
|
result.add(first[i] / secondNum?second:second[i])
|
|
}
|
|
return result
|
|
}
|
|
|
|
static approximately(first, second){
|
|
if(!(first is List) || !(second is List)) return false
|
|
if(first.count != second.count) return false
|
|
var count = first.count
|
|
if(count < 1) return true
|
|
if((first[0] - second[0]).abs > 0.001) return false
|
|
if(count < 2) return true
|
|
if((first[1] - second[1]).abs > 0.001) return false
|
|
if(count < 3) return true
|
|
if((first[2] - second[2]).abs > 0.001) return false
|
|
if(count < 4) return true
|
|
if((first[3] - second[3]).abs > 0.001) return false
|
|
return true
|
|
}
|
|
} |