Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
|
|
@ -1,3 +1,19 @@
|
|||
;Related tasks:
|
||||
* [[Arrays]]
|
||||
* [[Vector]]
|
||||
** [[Dot product]]
|
||||
** [[Vector products]]
|
||||
* [[Matrices]]
|
||||
* [[Bivector]]
|
||||
* [[Antivector]]
|
||||
* [[Tensor]]
|
||||
* [[Quaternion]]
|
||||
* [[Rotor]]
|
||||
* [[Motor]]
|
||||
* [[Sedenion]]
|
||||
* [[Octonion]]
|
||||
<br>
|
||||
|
||||
;Task
|
||||
Implement a Vector class (or a set of functions) that models a Physical Vector. The four basic operations and a ''pretty print'' function should be implemented.
|
||||
|
||||
|
|
@ -12,23 +28,5 @@ The four operations to be implemented are:
|
|||
* Vector <big><b> - </b></big> Vector subtraction
|
||||
* Vector <big><b> * </b></big> scalar multiplication
|
||||
* Vector <big><b> / </b></big> scalar division
|
||||
<br><br>
|
||||
|
||||
;Related tasks:
|
||||
|
||||
* [[Array]]
|
||||
* [[Arrays]]
|
||||
* [[Vector]]
|
||||
* [[Matrices]]
|
||||
* [[Dot product]]
|
||||
* [[Vector products]]
|
||||
* [[Bivector]]
|
||||
* [[Antivector]]
|
||||
* [[Tensor]]
|
||||
* [[Quaternion]]
|
||||
* [[Rotor]]
|
||||
* [[Motor]]
|
||||
* [[Sedenion]]
|
||||
* [[Octonion]]
|
||||
<br><br>
|
||||
<br>
|
||||
|
||||
|
|
|
|||
31
Task/Vector/Ada/vector.adb
Normal file
31
Task/Vector/Ada/vector.adb
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
with Ada.Text_IO;
|
||||
|
||||
procedure Vector_Demo is
|
||||
type Vector is record
|
||||
X, Y : Float;
|
||||
end record;
|
||||
|
||||
function "+" (A, B : Vector) return Vector is
|
||||
(A.X + B.X, A.Y + B.Y);
|
||||
|
||||
function "-" (A, B : Vector) return Vector is
|
||||
(A.X - B.X, A.Y - B.Y);
|
||||
|
||||
function "*" (S : Float; A : Vector) return Vector is
|
||||
(S * A.X, S * A.Y);
|
||||
|
||||
function "/" (A : Vector; S : Float) return Vector is
|
||||
(A.X / S, A.Y / S);
|
||||
|
||||
procedure Print (A : Vector) is
|
||||
begin
|
||||
Ada.Text_IO.Put_Line ("(" & A.X'Image & "," & A.Y'Image & ")");
|
||||
end Print;
|
||||
|
||||
Example : Vector := (1.0, 1.0);
|
||||
begin
|
||||
Print (Example + Example);
|
||||
Print (Example - Example);
|
||||
Print (2.0 * Example);
|
||||
Print (Example / 2.0);
|
||||
end Vector_Demo;
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
/*
|
||||
* CXXFLAGS=-march=native -O3 -std=<c|gnu>++23 -mfpmath=<your SIMD implementation>
|
||||
* CXXFLAGS=-march=native -O3 -std=<c|gnu>++23 -mfpmath=<your SIMD implementation> -ftree-vectorize -fopenmp-simd
|
||||
* See: [https://learn.microsoft.com/en-us/cpp/parallel/openmp/openmp-simd?view=msvc-180 Microsoft Visual Studio OpenMP SIMD]
|
||||
* compatible with every compiler and any SIMD platform.
|
||||
* Based on aligned vector type using cstdout not iostream.
|
||||
*/
|
||||
|
|
|
|||
21
Task/Vector/C/vector-1.c
Normal file
21
Task/Vector/C/vector-1.c
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
#include "vec.h"
|
||||
|
||||
/* arbitrary type test macro */
|
||||
#define out_tst(a,b) \
|
||||
({ \
|
||||
out_vec("Vec a : ",(a ),2); out_end(); \
|
||||
out_vec("Vec b : ",( b ),2); out_end(); \
|
||||
out_vec("a + b : ",(a + b ),2); out_end(); \
|
||||
out_vec("a - b : ",(a - b ),2); out_end(); \
|
||||
out_vec("a * 3 : ",(a * 3 ),2); out_end(); \
|
||||
out_vec("b / 2.5 : ",(b / 2.5),2); out_end(); \
|
||||
})
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
vec(flt,2) a = { 3 * cos( pi/6), 3 * sin( pi/6) };
|
||||
vec(flt,2) b = { 5 * cos(2*pi/3), 5 * sin(2*pi/3) };
|
||||
|
||||
out_tst(a,b);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
98
Task/Vector/C/vector-2.c
Normal file
98
Task/Vector/C/vector-2.c
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
#pragma once
|
||||
|
||||
#include<stdio.h>
|
||||
#include<stdint.h>
|
||||
#include<stdlib.h>
|
||||
#include<stddef.h>
|
||||
#include<stdbool.h>
|
||||
#include<stdalign.h>
|
||||
#include<stdarg.h>
|
||||
#include<sys/param.h>
|
||||
#include<math.h>
|
||||
|
||||
/* default floating point scalar */
|
||||
typedef double flt;
|
||||
#define pi M_PI
|
||||
|
||||
#define PARENS ()
|
||||
#define EXPAND(...) EXPAND4(EXPAND4(EXPAND4(EXPAND4(__VA_ARGS__))))
|
||||
#define EXPAND4(...) EXPAND3(EXPAND3(EXPAND3(EXPAND3(__VA_ARGS__))))
|
||||
#define EXPAND3(...) EXPAND2(EXPAND2(EXPAND2(EXPAND2(__VA_ARGS__))))
|
||||
#define EXPAND2(...) EXPAND1(EXPAND1(EXPAND1(EXPAND1(__VA_ARGS__))))
|
||||
#define EXPAND1(...) __VA_ARGS__
|
||||
|
||||
#undef countof
|
||||
#define countof(x) sizeof(typeof((x)))/sizeof(typeof((x)[0]))
|
||||
#define cnt(...) sizeof((typeof(__VA_ARGS__)[]){__VA_ARGS__})/sizeof(__VA_ARGS__)
|
||||
|
||||
#define BITOP_RUP01__(x) ( (x) | ( (x) >> 1))
|
||||
#define BITOP_RUP02__(x) (BITOP_RUP01__(x) | (BITOP_RUP01__(x) >> 2))
|
||||
#define BITOP_RUP04__(x) (BITOP_RUP02__(x) | (BITOP_RUP02__(x) >> 4))
|
||||
#define BITOP_RUP08__(x) (BITOP_RUP04__(x) | (BITOP_RUP04__(x) >> 8))
|
||||
#define BITOP_RUP16__(x) (BITOP_RUP08__(x) | (BITOP_RUP08__(x) >> 16))
|
||||
|
||||
|
||||
#define BIT_CEIL(x) (BITOP_RUP16__(((uint32_t)(x)) - 1) + 1)
|
||||
|
||||
#if defined(__clang__)
|
||||
#define vec(T,N) typeof(T __attribute__((ext_vector_type(N))))
|
||||
#elif defined(__GNUC__)
|
||||
#define vec(T,N) typeof(T __attribute__((vector_size(sizeof(T) * BIT_CEIL(N)))))
|
||||
#elif defined(_MSC_VER)
|
||||
#define vec(T,N) typeof(T __declspec((align(sizeof(T)*BIT_CEIL(N))))[BIT_CEIL(N)])
|
||||
#warn "Your compiler doens't support vector extensions."
|
||||
#warn "Using aligned arrays without operators instead."
|
||||
#else
|
||||
#define vec(T,N) typeof(T __attribute__((aligned(sizeof(T)*BIT_CEIL(N))))[BIT_CEIL(N)])
|
||||
#warn "Your compiler doens't support vector extensions."
|
||||
#warn "Using aligned arrays without operators instead."
|
||||
#endif
|
||||
|
||||
#define perm(a,...) { __VA_OPT__(EXPAND(perm_helper(a,__VA_ARGS__))) }
|
||||
#define perm_helper(a,i,...) (a)[i], __VA_OPT__(perm_again PARENS (a,__VA_ARGS__))
|
||||
#define perm_again() perm_helper
|
||||
#define perm2(a,...) ((vec(typeof((a)[0]),2))perm((a),__VA_ARGS__))
|
||||
#define perm3(a,...) ((vec(typeof((a)[0]),3))perm((a),__VA_ARGS__))
|
||||
#define perm4(a,...) ((vec(typeof((a)[0]),4))perm((a),__VA_ARGS__))
|
||||
#define broadcast(n,x) (vec(typeof((a)[0]),n))(x - (vec(typeof((a)[0]),n)){})
|
||||
|
||||
#define dot(a,b,t,n,i) \
|
||||
({ \
|
||||
t dst = (t)i; \
|
||||
_Pragma("omp simd reduction(+:dst)") \
|
||||
for(size_t j = 0; j < MIN(MIN(countof(a),countof(b)),n); j++) \
|
||||
dst += (t)((a)[j] * (b)[j]); \
|
||||
dst; \
|
||||
})
|
||||
|
||||
/* default dot products for length 3/4 */
|
||||
#define dot3(a,b) (dot((a),(b),flt,3,0))
|
||||
#define dot4(a,b) (dot((a),(b),flt,4,0))
|
||||
|
||||
#define negate(a,mod,val) \
|
||||
({ \
|
||||
for(size_t i = 0; i < countof((a)); i++) \
|
||||
(a)[i] = i % mod == val ? -(a)[i] : (a)[i]; \
|
||||
(a); \
|
||||
})
|
||||
|
||||
#define cross2(a,winding) \
|
||||
({ \
|
||||
vec(typeof((a)[0]),2) id = broadcast(2,1); \
|
||||
negate(id,2,0b1^(winding & 0b1)) * perm2(a,1,0); \
|
||||
})
|
||||
|
||||
#define cross3(a,b) \
|
||||
perm3(a,1,2,0) * perm3(b,2,0,1) \
|
||||
- perm3(a,2,0,1) * perm3(b,1,2,0)
|
||||
|
||||
#define out_vec(msg,a,n) \
|
||||
({ \
|
||||
printf("%s [ ", msg); \
|
||||
for(size_t i = 0; i < n; i++) \
|
||||
{ \
|
||||
printf("%+e ", (double)((a)[i])); \
|
||||
if(i == n-1) printf("]"); \
|
||||
} \
|
||||
})
|
||||
#define out_end() puts("");
|
||||
36
Task/Vector/Crystal/vector.cr
Normal file
36
Task/Vector/Crystal/vector.cr
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
record Vector, x : Float64, y : Float64 do
|
||||
def self.polar (value, angle)
|
||||
new value*Math.cos(angle), value*Math.sin(angle)
|
||||
end
|
||||
|
||||
def + (other)
|
||||
Vector.new(x + other.x, y + other.y)
|
||||
end
|
||||
|
||||
def - ()
|
||||
Vector.new(-x, -y)
|
||||
end
|
||||
|
||||
def - (other)
|
||||
Vector.new(x - other.x, y - other.y)
|
||||
end
|
||||
|
||||
def * (scalar)
|
||||
Vector.new(x * scalar, y * scalar)
|
||||
end
|
||||
|
||||
def / (scalar)
|
||||
Vector.new(x / scalar, y / scalar)
|
||||
end
|
||||
|
||||
def to_s (io)
|
||||
xs, ys = [x, y].map &.format(delimiter: nil, decimal_places: 6, only_significant: true)
|
||||
io << "Vector{" << xs << ", " << ys << "}"
|
||||
end
|
||||
end
|
||||
|
||||
u = Vector.new 1, 2
|
||||
v = Vector.polar 2, Math::PI/3
|
||||
|
||||
puts "u = #{u}\nv = #{v}"
|
||||
puts "u + v = #{u + v}\nu - v = #{u - v}\nu * 2 = #{u * 2}\nv / 2 = #{v / 2}"
|
||||
40
Task/Vector/JavaScript/vector.js
Normal file
40
Task/Vector/JavaScript/vector.js
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
class Vector {
|
||||
constructor(x, y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.length = Math.hypot(this.x, this.y);
|
||||
this.angle = Math.atan2(this.y, this.x);
|
||||
}
|
||||
|
||||
add(v) {
|
||||
const sum = new Vector(this.x + v.x, this.y + v.y);
|
||||
return sum;
|
||||
}
|
||||
|
||||
sub(v) {
|
||||
const diff = new Vector(this.x - v.x, this.y - v.y);
|
||||
return diff;
|
||||
}
|
||||
|
||||
mul(c) {
|
||||
const scaled = new Vector(c * this.x, c * this.y);
|
||||
return scaled;
|
||||
}
|
||||
|
||||
div(c) {
|
||||
const scaled = new Vector(this.x / c, this.y / c);
|
||||
return scaled;
|
||||
}
|
||||
|
||||
dot(v) {
|
||||
return this.x * v.x + this.y * v.y;
|
||||
}
|
||||
|
||||
print() {
|
||||
console.log(`2D vector: (${this.x}, ${this.y})`);
|
||||
}
|
||||
|
||||
polar() {
|
||||
console.log(`2D vector (polar): (${this.length}, ${this.angle})`);
|
||||
}
|
||||
}
|
||||
40
Task/Vector/Lang/vector-1.lang
Normal file
40
Task/Vector/Lang/vector-1.lang
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
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))
|
||||
41
Task/Vector/Lang/vector-2.lang
Normal file
41
Task/Vector/Lang/vector-2.lang
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
class &Vector {
|
||||
+$x
|
||||
+$y
|
||||
|
||||
+construct = ($x{number}, $y{number}) -> {
|
||||
::$x $= double($x)
|
||||
::$y $= double($y)
|
||||
}
|
||||
|
||||
+op:add = ($op{OBJECT}) -> {
|
||||
return parser.op(&Vector(::$x + $op::$x, ::$y + $op::$y))
|
||||
}
|
||||
|
||||
+op:sub = ($op{OBJECT}) -> {
|
||||
return parser.op(&Vector(::$x - $op::$x, ::$y - $op::$y))
|
||||
}
|
||||
|
||||
+op:mul = ($op{number}) -> {
|
||||
return parser.op(&Vector(::$x * $op, ::$y * $op))
|
||||
}
|
||||
|
||||
+op:div = ($op{number}) -> {
|
||||
return parser.op(&Vector(::$x / $op, ::$y / $op))
|
||||
}
|
||||
|
||||
+to:text = () -> {
|
||||
return [parser.op(::$x), parser.op(::$y)]
|
||||
}
|
||||
}
|
||||
|
||||
$vec1 = &Vector(5, 7)
|
||||
$vec2 = &Vector(2, 3)
|
||||
|
||||
fn.println($vec1)
|
||||
fn.println($vec2)
|
||||
fn.println()
|
||||
|
||||
fn.println(parser.op($vec1 + $vec2))
|
||||
fn.println(parser.op($vec1 - $vec2))
|
||||
fn.println(parser.op($vec1 * 11))
|
||||
fn.println(parser.op($vec1 / 2))
|
||||
|
|
@ -2,12 +2,6 @@ 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
|
||||
|
|
@ -16,13 +10,19 @@ public:
|
|||
.x-=b.x
|
||||
.y-=b.y
|
||||
}
|
||||
operator "*" (b as literal){
|
||||
.x*=b.v
|
||||
.y*=b.v
|
||||
function mul_literal(b as double){
|
||||
ret=this
|
||||
' x in ret is private but is accessible...
|
||||
' ...from same type's member of other object.
|
||||
ret.x=.x*b
|
||||
ret.y=.y*b
|
||||
=ret
|
||||
}
|
||||
operator "/" (b as literal){
|
||||
.x/=b.v
|
||||
.y/=b.v
|
||||
function div_literal(b as double){
|
||||
ret=this
|
||||
ret.x=.x/b
|
||||
ret.y=.y/b
|
||||
=ret
|
||||
}
|
||||
property printVector {
|
||||
value {
|
||||
|
|
@ -57,10 +57,10 @@ 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)
|
||||
mul_a_3=a.mul_literal(3)
|
||||
s$="Multiplying vector a by 3 : "+mul_a_3.printVector+{
|
||||
}
|
||||
div_b_2.5=b/b.literal(2.5)
|
||||
div_b_2.5=b.div_literal(2.5)
|
||||
s$="Dividing vector b by 2.5 : "+div_b_2.5.printVector+{
|
||||
}
|
||||
report s$
|
||||
|
|
|
|||
4
Task/Vector/Maxima/vector.maxima
Normal file
4
Task/Vector/Maxima/vector.maxima
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
[10, 20]+[2, 3];
|
||||
[10, 20]-[2, 3];
|
||||
[10, 20]*5;
|
||||
[10, 20]/5;
|
||||
8
Task/Vector/PowerShell/vector.ps1
Normal file
8
Task/Vector/PowerShell/vector.ps1
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
$V1 = New-Object System.Windows.Vector ( 2.5, 3.4 )
|
||||
$V2 = New-Object System.Windows.Vector ( -6, 2 )
|
||||
$V1
|
||||
$V2
|
||||
$V1 + $V2
|
||||
$V1 - $V2
|
||||
$V1 * 3
|
||||
$V1 / 8
|
||||
4
Task/Vector/R/vector.r
Normal file
4
Task/Vector/R/vector.r
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
c(10, 20) + c(2, 3)
|
||||
c(10, 20) - c(2, 3)
|
||||
c(10, 20)*5
|
||||
c(10, 20)/5
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
-- 24 Aug 2025
|
||||
-- 4 Mar 2026
|
||||
include Setting
|
||||
|
||||
say 'VECTOR'
|
||||
|
|
@ -6,23 +6,24 @@ say version
|
|||
say
|
||||
a = '1 2 3'; b = '3 2 1'; c = 3; d = '4 5'; e = '2 1'
|
||||
say 'VALUES'
|
||||
say 'A =' Lst2FormV(a)
|
||||
say 'B =' Lst2FormV(b)
|
||||
say 'A =' Vect2form(a)
|
||||
say 'B =' Vect2form(b)
|
||||
say 'C =' c
|
||||
say 'D =' Lst2FormV(d)
|
||||
say 'E =' Lst2FormV(e)
|
||||
say 'D =' Vect2form(d)
|
||||
say 'E =' Vect2form(e)
|
||||
say
|
||||
say 'BASICS'
|
||||
say 'A+B =' Lst2FormV(AddV(a,b))
|
||||
say 'A-B =' Lst2FormV(SubV(a,b))
|
||||
say 'A*C =' Lst2FormV(ScaleV(a,c))
|
||||
say 'A/C =' Lst2FormV(ScaleV(a,1/c))
|
||||
say 'A+B =' Vect2form(AddV(a,b))
|
||||
say 'A-B =' Vect2form(SubV(a,b))
|
||||
say 'A*C =' Vect2form(ScaleV(a,c))
|
||||
say 'A/C =' Vect2form(ScaleV(a,1/c))
|
||||
say
|
||||
say 'BONUS'
|
||||
say 'Length(D) =' LenV(d)+0
|
||||
say 'Angle(D) =' AngV(d)+0
|
||||
say 'Polar(D) =' Lst2FormV(Rec2PolV(d))
|
||||
say 'Rect(E) =' Lst2FormV(Pol2RecV(e))
|
||||
say 'Polar(D) =' Vect2form(Rec2PolV(d))
|
||||
say 'Rect(E) =' Vect2form(Pol2RecV(e))
|
||||
exit
|
||||
|
||||
-- Vect2form; XxxV
|
||||
include Math
|
||||
|
|
|
|||
21
Task/Vector/Unicon/vector.unicon
Normal file
21
Task/Vector/Unicon/vector.unicon
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
procedure main()
|
||||
v1 := vector(5,7)
|
||||
v2 := vector(2,3)
|
||||
write(v1.tostring(),"+",v2.tostring()," = ",(v1 + v2).tostring())
|
||||
write(v1.tostring(),"-",v2.tostring()," = ",(v1 - v2).tostring())
|
||||
write(v1.tostring(),"*123 = ",(v1*123).tostring())
|
||||
write(v1.tostring(),"/0.5 = ",(v1*0.5).tostring())
|
||||
end
|
||||
|
||||
class vector(x,y)
|
||||
method tostring();return "["||x||","||y||"]"; end
|
||||
method add(v); return vector(x+v.x, y+v.y); end
|
||||
method minus(v); return vector(x-v.x, y-v.y); end
|
||||
method mult(z); return vector(x*z, y*z); end
|
||||
method div(z); return vector(x/z, y/z); end
|
||||
# The next four provide operator overloading, if enabled.
|
||||
method __add__(v); return add(v); end
|
||||
method __minus__(v); return minus(v); end
|
||||
method __mult__(z); return mult(z); end
|
||||
method __div__(z); return div(z); end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue