Vector: Difference between revisions

4,461 bytes added ,  1 month ago
m
Update Lang example: Use new struct definition syntax
No edit summary
m (Update Lang example: Use new struct definition syntax)
 
(13 intermediate revisions by 8 users not shown)
Line 167:
</pre>
 
=={{header|BASIC256Arturo}}==
<syntaxhighlight lang="arturo">define :vector [
x y
][
print: -> render "(|this\x|, |this\y|)" ; prettyprint function
]
 
ensureVector: function [block][
ensure -> every? @block => [is? :vector &]
]
 
vadd: function [a b][
ensureVector [a b]
to :vector @[a\x + b\x, a\y + b\y]
]
 
vsub: function [a b][
ensureVector [a b]
to :vector @[a\x - b\x, a\y - b\y]
]
 
vmul: function [a n][
ensureVector [a]
to :vector @[a\x * n, a\y * n]
]
 
vdiv: function [a n][
ensureVector [a]
to :vector @[a\x // n, a\y // n]
]
 
; test our vector object
a: to :vector [5 7]
b: to :vector [2 3]
print [a '+ b '= vadd a b]
print [a '- b '= vsub a b]
print [a '* 11 '= vmul a 11]
print [a '/ 11 '= vdiv a 2]</syntaxhighlight>
 
{{out}}
 
<pre>(5, 7) + (2, 3) = (7, 10)
(5, 7) - (2, 3) = (3, 4)
(5, 7) * 11 = (55, 77)
(5, 7) / 11 = (2.5, 3.5)</pre>
 
=={{header|BASIC}}==
==={{header|BASIC256}}===
{{trans|Ring}}
<syntaxhighlight lang="freebasic">arraybase 1
Line 216 ⟶ 263:
[5, 7] * 11 = [55, 77]
[5, 7] / 2 = [2.5, 3.5]</pre>
 
 
=={{header|BQN}}==
Line 658 ⟶ 704:
(55,0 + i77,0)
(2,5 + i3,5)
</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight>
func[] vadd a[] b[] .
for i to len a[]
r[] &= a[i] + b[i]
.
return r[]
.
func[] vsub a[] b[] .
for i to len a[]
r[] &= a[i] - b[i]
.
return r[]
.
func[] vmul a[] b .
for i to len a[]
r[] &= a[i] * b
.
return r[]
.
func[] vdiv a[] b .
for i to len a[]
r[] &= a[i] / b
.
return r[]
.
print vadd [ 5 7 ] [ 2 3 ]
print vsub [ 5 7 ] [ 2 3 ]
print vmul [ 5 7 ] 11
print vdiv [ 5 7 ] 2
</syntaxhighlight>
 
{{out}}
<pre>
[ 7 10 ]
[ 3 4 ]
[ 55 77 ]
[ 2.50 3.50 ]
</pre>
 
Line 940 ⟶ 1,026:
 
dim shared as single v04(2)
 
'
sub v01_(x as integer,y as integer,z as integer):v01(0)=x:v01(1)=y:v01(2)=z:end sub
Line 971 ⟶ 1,058:
 
'----------------------------------------------------------------------------------------------------------------
 
'
 
Line 1,393 ⟶ 1,479:
11 * v2 = (22.0, 33.0)
v1 / 2 = (2.5, 3.5)
</pre>
 
=={{header|Lang}}==
<syntaxhighlight lang="lang">
struct &Vector {
$x
$y
}
 
fp.initVector = ($x, $y) -> {
return &Vector(fn.double($x), fn.double($y))
}
 
fp.addVector = ($a, $b) -> {
return parser.op(&Vector($a::$x + $b::$x, $a::$y + $b::$y))
}
 
fp.subVector = ($a, $b) -> {
return parser.op(&Vector($a::$x - $b::$x, $a::$y - $b::$y))
}
 
fp.mulVector = ($vec, $scalar) -> {
return parser.op(&Vector($vec::$x * $scalar, $vec::$y * $scalar))
}
 
fp.divVector = ($vec, $scalar) -> {
return parser.op(&Vector($vec::$x / $scalar, $vec::$y / $scalar))
}
 
fp.printVector = ($vec) -> {
fn.println([parser.op($vec::$x), parser.op($vec::$y)])
}
 
$vec1 = fp.initVector(5, 7)
$vec2 = fp.initVector(2, 3)
 
fp.printVector($vec1)
fp.printVector($vec2)
fn.println()
 
fp.printVector(fp.addVector($vec1, $vec2))
fp.printVector(fp.subVector($vec1, $vec2))
fp.printVector(fp.mulVector($vec1, 11))
fp.printVector(fp.divVector($vec1, 2))
</syntaxhighlight>
 
{{out}}
<pre>
[5.0, 7.0]
[2.0, 3.0]
 
[7.0, 10.0]
[3.0, 4.0]
[55.0, 77.0]
[2.5, 3.5]
</pre>
 
Line 1,764 ⟶ 1,905:
(4, -2) / 2 = (2, -1)
(3.0, 2.0) / 2 = (1.5, 1.0)</pre>
 
=={{header|Oberon-2}}==
{{trans|Modula-2}}
<syntaxhighlight lang="oberon2">MODULE Vector;
 
IMPORT Out;
TYPE
Vector = POINTER TO VectorDesc;
VectorDesc = RECORD
x,y:REAL;
END;
 
VAR
a,b:Vector;
PROCEDURE Add*(a,b:Vector):Vector;
VAR res:Vector;
BEGIN
NEW(res);
res.x := a.x+b.x;
res.y := a.y+b.y;
RETURN res;
END Add;
 
PROCEDURE Sub*(a,b:Vector):Vector;
VAR res:Vector;
BEGIN
NEW(res);
res.x := a.x-b.x;
res.y := a.y-b.y;
RETURN res;
END Sub;
 
PROCEDURE Mul*(v:Vector;r:REAL):Vector;
VAR res:Vector;
BEGIN
NEW(res);
res.x := v.x*r;
res.y := v.y*r;
RETURN res;
END Mul;
 
PROCEDURE Div*(v:Vector;r:REAL):Vector;
VAR res:Vector;
BEGIN
NEW(res);
res.x := v.x/r;
res.y := v.y/r;
RETURN res;
END Div;
 
PROCEDURE Print*(op:ARRAY OF CHAR;v:Vector);
BEGIN
Out.String(op);
Out.String("(");
Out.Real(v.x,0);
Out.String(", ");
Out.Real(v.y,0);
Out.String(")");
END Print;
 
BEGIN
NEW(a); NEW(b);
a.x := 5.0; a.y := 7.0;
b.x := 2.0; b.y := 3.0;
Print("Add: ",Add(a,b));
Out.Ln;
Print("Sub: ",Sub(a,b));
Out.Ln;
Print("Mul: ",Mul(a,11.0));
Out.Ln;
Print("Div: ",Div(a,2.0));
Out.Ln
END Vector.
</syntaxhighlight>
 
{{out}}
<pre>Add: (7.0E+00, 1.0E+01)
Sub: (3.0E+00, 4.0E+00)
Mul: (5.5E+01, 7.7E+01)
Div: (2.5E+00, 3.5E+00)
</pre>
 
=={{header|Objeck}}==
Line 1,989 ⟶ 2,213:
=={{header|Perl}}==
Typically we would use a module, such as [https://metacpan.org/pod/Math::Vector::Real Math::Vector::Real] or [https://metacpan.org/pod/Math::Complex Math::Complex]. Here is a very basic Moose class.
<syntaxhighlight lang="perl">packageuse Vectorv5.36;
 
package Vector;
use Moose;
use featureoverload 'say+'; => \&add,
'-' => \&sub,
 
use overload '+*' => \&addmul,
'-/' => \&subdiv,
'*' => \&mul,
'/' => \&div,
'""' => \&stringify;
 
Line 2,002 ⟶ 2,226:
has 'y' => (is =>'rw', isa => 'Num', required => 1);
 
sub add ($a, $b, $) { Vector->new( x => $a->x + $b->x, y => $a->y + $b->y) }
sub add {
sub sub ($a, $b, $) { Vector->new( x => $a->x - $b->x, y => $a->y - $b->y) }
my($a, $b) = @_;
sub mul ($a, $b, $) { Vector->new( x => $a->x +* $b->x, y => $a->y +* $b->y); }
sub div ($a, $b, $) { Vector->new( x => $a->x / $b, y => $a->y / $b) }
}
sub stringify ($self, $, $) { '(' . $self->x . ',' . $self->y . ')' }
sub sub {
my($a, $b) = @_;
Vector->new( x => $a->x - $b->x, y => $a->y - $b->y);
}
sub mul {
my($a, $b) = @_;
Vector->new( x => $a->x * $b, y => $a->y * $b);
}
sub div {
my($a, $b) = @_;
Vector->new( x => $a->x / $b, y => $a->y / $b);
}
sub stringify {
my $self = shift;
"(" . $self->x . "," . $self->y . ')';
}
 
package main;
Line 3,244 ⟶ 3,453:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">class Vector2D {
construct new(x, y) {
_x = x
Line 3,290 ⟶ 3,499:
v1 / 2 = (2.5, 3.5)
</pre>
 
{{libheader|Wren-vector}}
Alternatively, using the above module and producing exactly the same output as before:
<syntaxhighlight lang="wren">import "./vector" for Vector2
 
var v1 = Vector2.new(5, 7)
var v2 = Vector2.new(2, 3)
var v3 = Vector2.fromPolar(2.sqrt, Num.pi / 4)
System.print("v1 = %(v1)")
System.print("v2 = %(v2)")
System.print("v3 = %(v3)")
System.print()
System.print("v1 + v2 = %(v1 + v2)")
System.print("v1 - v2 = %(v1 - v2)")
System.print("v1 * 11 = %(v1 * 11)")
System.print("11 * v2 = %(Vector2.scale(11, v2))")
System.print("v1 / 2 = %(v1 / 2)")</syntaxhighlight>
 
=={{header|XPL0}}==
168

edits