Vector: Difference between revisions

7,955 bytes added ,  1 month ago
m
Update Lang example: Use new struct definition syntax
m (syntax highlighting fixup automation)
m (Update Lang example: Use new struct definition syntax)
 
(19 intermediate revisions by 10 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 931 ⟶ 1,017:
[5, 7] / 2 = [2.5, 3.5]
</pre>
? "------------------------------------------------"
 
'compare with:
 
'----------------------------------------------------------------------------------------------------------------
 
dim shared as integer v01(2),v02(2),v03(2),v05(2)
 
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
 
sub v02_(x as integer,y as integer,z as integer):v02(0)=x:v02(1)=y:v02(2)=z:end sub
 
sub v03_(x as integer,y as integer,z as integer):v03(0)=x:v03(1)=y:v03(2)=z:end sub
 
sub v04_(x as single,y as single,z as single):v04(0)=x:v04(1)=y:v04(2)=z:end sub
 
sub p(v() as integer):? "[";v(0);"/";v(1);"/";v(2);"]":end sub
 
sub ps(v() as single):? "[";v(0);"/";v(1);"/";v(2);"]":end sub
 
'
v01_(5,7,0):?"v01=";:p(v01())
 
v02_(2,3,0):?"v02=";:p(v02())
 
v03_(v01(0)+v02(0),v01(1)+v02(1),v01(2)+v02(2)) :?"v03=v01+v02=";:p(v03())
 
v03_(v01(0)-v02(0),v01(1)-v02(1),v01(2)-v02(2)) :?"v03=v01-v02=";:p(v03())
 
v03_(v01(0)*11,v01(1)*11,v01(2)*11) :?"v03=v01*11=" ;:p(v03()) '? integer
 
v04_(v01(0)/2,v01(1)/2,v01(2)/2) :?"v04=v01/2=" ;:ps(v04()) '? single
 
? "------------------------------------------------"
 
do:loop
 
'----------------------------------------------------------------------------------------------------------------
'
 
=={{header|Go}}==
Line 1,351 ⟶ 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,392 ⟶ 1,575:
(55, 77)
(2.5, 3.5)</pre>
 
=={{header|M2000 Interpreter}}==
Adapted from C
 
<syntaxhighlight lang="m2000 interpreter">
class vector {
private:
double x, y
public:
class literal {
double v
class:
module Literal(.v) {
}
}
operator "+" (b as vector){
.x+=b.x
.y+=b.y
}
operator "-" (b as vector){
.x-=b.x
.y-=b.y
}
operator "*" (b as literal){
.x*=b.v
.y*=b.v
}
operator "/" (b as literal){
.x/=b.v
.y/=b.v
}
property printVector {
value {
link parent x, y to x, y
value=format$(.fm$, str$(round(x,.r), .Lcid),if$(y>=0->"+", "-"),str$(abs(round(y,.r)),.lcid))
}
}="" // make type string
// added members to printVector (is a group type)
group printVector {
integer Lcid=1033
fm$="{0} î {1}{2} û"
r=6
}
class:
module vector(r as double, theta as double, Lcid=1033) {
def deg(rad)=rad*180@/pi
.printVector.Lcid<=Lcid
.x<=r*cos(deg(theta))
.y<=r*sin(deg(theta))
}
}
document s$
a=vector(3,pi/6)
s$="Vector a : "+a.printVector+{
}
b=vector(5,2*pi/3)
s$="Vector b : "+b.printVector+{
}
sum_a_b=a+b
s$="Sum of vectors a and b : "+sum_a_b.printVector+{
}
diff_a_b=a-b
s$="Difference of vectors a and b : "+diff_a_b.printVector+{
}
mul_a_3=a*a.literal(3)
s$="Multiplying vector a by 3 : "+mul_a_3.printVector+{
}
div_b_2.5=b/b.literal(2.5)
s$="Dividing vector b by 2.5 : "+div_b_2.5.printVector+{
}
report s$
clipboard s$
</syntaxhighlight>
{{out}}
<pre>
Vector a : 2.598076 î +1.5 û
Vector b : -2.5 î +4.330127 û
Sum of vectors a and b : 0.098076 î +5.830127 û
Difference of vectors a and b : 5.098076 î -2.830127 û
Multiplying vector a by 3 : 7.794229 î +4.5 û
Dividing vector b by 2.5 : -1 î +1.732051 û
</pre>
 
 
=={{header|Maple}}==
Line 1,639 ⟶ 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,864 ⟶ 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 1,877 ⟶ 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 2,586 ⟶ 2,920:
</pre>
 
=={{header|RPL}}==
Basic vector (and matrix) handling is wired in RPL.
{{in}}
<pre>
[10,20,30] [1,2,3] +
[10,20,30] [1,2,3] -
[10,20,30] 5 *
[10,20,30] 5 /
</pre>
{{out}}
<pre>
4: [ 11 22 33 ]
3: [ 9 18 27 ]
2: [ 50 100 150 ]
1: [ 2 4 6 ]
</pre>
If the user wants to handle 2D vectors with possibility to use either polar or rectangular coordinates, vectors must be expressed as complex numbers, for which RPL provides <code>R→P</code> and <code>P→R</code> conversion instructions
(1,2) R→P
1: (2.2360679775,1.10714871779)
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">class Vector
Line 3,100 ⟶ 3,453:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">class Vector2D {
construct new(x, y) {
_x = x
Line 3,146 ⟶ 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