Vector: Difference between revisions

Content added Content deleted
No edit summary
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 145:
Dividing vector b by 2.5 : -1.000000 î + 1.732051 û
</pre>
 
=={{header|C sharp|C#}}==
<lang csharp>using System;
using System.Collections.Generic;
using System.Linq;
 
namespace RosettaVectors
{
public class Vector
{
public double[] store;
public Vector(IEnumerable<double> init)
{
store = init.ToArray();
}
public Vector(double x, double y)
{
store = new double[] { x, y };
}
static public Vector operator+(Vector v1, Vector v2)
{
return new Vector(v1.store.Zip(v2.store, (a, b) => a + b));
}
static public Vector operator -(Vector v1, Vector v2)
{
return new Vector(v1.store.Zip(v2.store, (a, b) => a - b));
}
static public Vector operator *(Vector v1, double scalar)
{
return new Vector(v1.store.Select(x => x * scalar));
}
static public Vector operator /(Vector v1, double scalar)
{
return new Vector(v1.store.Select(x => x / scalar));
}
public override string ToString()
{
return string.Format("[{0}]", string.Join(",", store));
}
}
class Program
{
static void Main(string[] args)
{
var v1 = new Vector(5, 7);
var v2 = new Vector(2, 3);
Console.WriteLine(v1 + v2);
Console.WriteLine(v1 - v2);
Console.WriteLine(v1 * 11);
Console.WriteLine(v1 / 2);
// Works with arbitrary size vectors, too.
var lostVector = new Vector(new double[] { 4, 8, 15, 16, 23, 42 });
Console.WriteLine(lostVector * 7);
Console.ReadLine();
}
}
}
</lang>
{{out}}
<pre>[7,10]
[3,4]
[55,77]
[2.5,3.5]
[28,56,105,112,161,294]</pre>
 
=={{header|C++}}==
Line 219 ⟶ 283:
X: 1 Y: 1
</pre>
 
=={{header|C sharp|C#}}==
<lang csharp>using System;
using System.Collections.Generic;
using System.Linq;
 
namespace RosettaVectors
{
public class Vector
{
public double[] store;
public Vector(IEnumerable<double> init)
{
store = init.ToArray();
}
public Vector(double x, double y)
{
store = new double[] { x, y };
}
static public Vector operator+(Vector v1, Vector v2)
{
return new Vector(v1.store.Zip(v2.store, (a, b) => a + b));
}
static public Vector operator -(Vector v1, Vector v2)
{
return new Vector(v1.store.Zip(v2.store, (a, b) => a - b));
}
static public Vector operator *(Vector v1, double scalar)
{
return new Vector(v1.store.Select(x => x * scalar));
}
static public Vector operator /(Vector v1, double scalar)
{
return new Vector(v1.store.Select(x => x / scalar));
}
public override string ToString()
{
return string.Format("[{0}]", string.Join(",", store));
}
}
class Program
{
static void Main(string[] args)
{
var v1 = new Vector(5, 7);
var v2 = new Vector(2, 3);
Console.WriteLine(v1 + v2);
Console.WriteLine(v1 - v2);
Console.WriteLine(v1 * 11);
Console.WriteLine(v1 / 2);
// Works with arbitrary size vectors, too.
var lostVector = new Vector(new double[] { 4, 8, 15, 16, 23, 42 });
Console.WriteLine(lostVector * 7);
Console.ReadLine();
}
}
}
</lang>
{{out}}
<pre>[7,10]
[3,4]
[55,77]
[2.5,3.5]
[28,56,105,112,161,294]</pre>
 
=={{header|D}}==
Line 1,284:
a/2: (2.5,3.5)
</pre>
 
=={{header|Perl 6}}==
 
<lang perl6>class Vector {
has Real $.x;
has Real $.y;
 
multi submethod BUILD (:$!x!, :$!y!) {
*
}
multi submethod BUILD (:$length!, :$angle!) {
$!x = $length * cos $angle;
$!y = $length * sin $angle;
}
multi submethod BUILD (:from([$x1, $y1])!, :to([$x2, $y2])!) {
$!x = $x2 - $x1;
$!y = $y2 - $y1;
}
method length { sqrt $.x ** 2 + $.y ** 2 }
method angle { atan2 $.y, $.x }
method add ($v) { Vector.new(x => $.x + $v.x, y => $.y + $v.y) }
method subtract ($v) { Vector.new(x => $.x - $v.x, y => $.y - $v.y) }
method multiply ($n) { Vector.new(x => $.x * $n, y => $.y * $n ) }
method divide ($n) { Vector.new(x => $.x / $n, y => $.y / $n ) }
method gist { "vec[$.x, $.y]" }
}
 
multi infix:<+> (Vector $v, Vector $w) is export { $v.add: $w }
multi infix:<-> (Vector $v, Vector $w) is export { $v.subtract: $w }
multi prefix:<-> (Vector $v) is export { $v.multiply: -1 }
multi infix:<*> (Vector $v, $n) is export { $v.multiply: $n }
multi infix:</> (Vector $v, $n) is export { $v.divide: $n }
 
 
#####[ Usage example: ]#####
 
say my $u = Vector.new(x => 3, y => 4); #: vec[3, 4]
say my $v = Vector.new(from => [1, 0], to => [2, 3]); #: vec[1, 3]
say my $w = Vector.new(length => 1, angle => pi/4); #: vec[0.707106781186548, 0.707106781186547]
 
say $u.length; #: 5
say $u.angle * 180/pi; #: 53.130102354156
 
say $u + $v; #: vec[4, 7]
say $u - $v; #: vec[2, 1]
say -$u; #: vec[-3, -4]
say $u * 10; #: vec[30, 40]
say $u / 2; #: vec[1.5, 2]</lang>
 
=={{header|Phix}}==
Line 1,748 ⟶ 1,697:
-X: 2.42535625036333
-Y: 9.70142500145332</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
 
<lang perl6>class Vector {
has Real $.x;
has Real $.y;
 
multi submethod BUILD (:$!x!, :$!y!) {
*
}
multi submethod BUILD (:$length!, :$angle!) {
$!x = $length * cos $angle;
$!y = $length * sin $angle;
}
multi submethod BUILD (:from([$x1, $y1])!, :to([$x2, $y2])!) {
$!x = $x2 - $x1;
$!y = $y2 - $y1;
}
method length { sqrt $.x ** 2 + $.y ** 2 }
method angle { atan2 $.y, $.x }
method add ($v) { Vector.new(x => $.x + $v.x, y => $.y + $v.y) }
method subtract ($v) { Vector.new(x => $.x - $v.x, y => $.y - $v.y) }
method multiply ($n) { Vector.new(x => $.x * $n, y => $.y * $n ) }
method divide ($n) { Vector.new(x => $.x / $n, y => $.y / $n ) }
method gist { "vec[$.x, $.y]" }
}
 
multi infix:<+> (Vector $v, Vector $w) is export { $v.add: $w }
multi infix:<-> (Vector $v, Vector $w) is export { $v.subtract: $w }
multi prefix:<-> (Vector $v) is export { $v.multiply: -1 }
multi infix:<*> (Vector $v, $n) is export { $v.multiply: $n }
multi infix:</> (Vector $v, $n) is export { $v.divide: $n }
 
 
#####[ Usage example: ]#####
 
say my $u = Vector.new(x => 3, y => 4); #: vec[3, 4]
say my $v = Vector.new(from => [1, 0], to => [2, 3]); #: vec[1, 3]
say my $w = Vector.new(length => 1, angle => pi/4); #: vec[0.707106781186548, 0.707106781186547]
 
say $u.length; #: 5
say $u.angle * 180/pi; #: 53.130102354156
 
say $u + $v; #: vec[4, 7]
say $u - $v; #: vec[2, 1]
say -$u; #: vec[-3, -4]
say $u * 10; #: vec[30, 40]
say $u / 2; #: vec[1.5, 2]</lang>
 
=={{header|Red}}==
Line 2,087 ⟶ 2,088:
println(s"\nSuccessfully completed without errors. [total ${scala.compat.Platform.currentTime - executionStart} ms]")
}</lang>
 
=={{header|Sidef}}==
{{trans|Perl 6}}
Line 2,240 ⟶ 2,242:
scalar multiplication: ( 2,500; 4,330) * 10 =( 25,000; 43,301)
scalar division : ( 2,500; 4,330) / 10 =( 0,250; 0,433)</pre>
 
=={{header|Visual Basic .NET}}==
{{trans|C#}}