#include
#include
; vector 类定义
func vectorcreate($a)
local $vector = objcreate("scripting.dictionary")
$vector.item("coords") = $a
$vector.item("n") = ubound($a)
$vector.__index = vectormethod
return $vector
endfunc
func vectormethod($vector, $name, $other = null)
switch $name
case "getitem"
return $vector.item("coords")
case "add"
return vectoradd($vector, $other)
case "sub"
return vectorsub($vector, $other)
case "scale"
return vectorscale($vector, $other)
case "dot"
return vectordot($vector, $other)
case "abs"
return vectorabs($vector)
; todo: 您可以继续添加其他向量操作
endswitch
endfunc
func vectoradd($vector, $other)
local $result[ubound($vector.item("coords"))]
for $i = 0 to ubound($vector.item("coords")) - 1
$result[$i] = $vector.item("coords")[$i] $other.item("coords")[$i]
next
return vectorcreate($result)
endfunc
func vectorsub($vector, $other)
local $result[ubound($vector.item("coords"))]
for $i = 0 to ubound($vector.item("coords")) - 1
$result[$i] = $vector.item("coords")[$i] - $other.item("coords")[$i]
next
return vectorcreate($result)
endfunc
func vectorscale($vector, $factor)
local $result[ubound($vector.item("coords"))]
for $i = 0 to ubound($vector.item("coords")) - 1
$result[$i] = $vector.item("coords")[$i] * $factor
next
return vectorcreate($result)
endfunc
func vectordot($vector, $other)
local $dotproduct = 0
for $i = 0 to ubound($vector.item("coords")) - 1
$dotproduct = $vector.item("coords")[$i] * $other.item("coords")[$i]
next
return $dotproduct
endfunc
func vectorabs($vector)
local $sumsquare = 0
for $i = 0 to ubound($vector.item("coords")) - 1
$sumsquare = $vector.item("coords")[$i] * $vector.item("coords")[$i]
next
return sqrt($sumsquare)
endfunc
; 测试代码
func main()
local $xcoords[3] = [2.0, 2.0, 2.0]
local $ycoords[3] = [5.0, 5.0, 0.0]
local $x = vectorcreate($xcoords)
local $y = vectorcreate($ycoords)
consolewrite("x = " & _arraytostring(vectormethod($x, "getitem")) & ", y = " & _arraytostring(vectormethod($y, "getitem")) & @crlf)
consolewrite("x y = " & _arraytostring(vectormethod(vectormethod($x, "add", $y), "getitem")) & @crlf)
consolewrite("10x = " & _arraytostring(vectormethod(vectormethod($x, "scale", 10), "getitem")) & @crlf)
consolewrite("|x| = " & vectormethod($x, "abs") & @crlf)
consolewrite(" = " & vectormethod($x, "dot", $y) & @crlf)
consolewrite("|x - y| = " & vectormethod(vectormethod($x, "sub", $y), "abs") & @crlf)
endfunc
main()
|