Initial data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 72d218235f
commit f23f22d71c
199087 changed files with 3378941 additions and 0 deletions

View file

@ -0,0 +1,44 @@
def polar(r; angle):
[ r*(angle|cos), r*(angle|sin) ];
# If your jq allows multi-arity functions, you may wish to uncomment the following line:
# def polar(r): [r, 0];
def polar2vector: polar(.[0]; .[1]);
def vector(x; y):
if (x|type) == "number" and (y|type) == "number" then [x,y]
else error("TypeError")
end;
# Input: an array of same-dimensional vectors of any dimension to be added
def sum:
def sum2: .[0] as $a | .[1] as $b | reduce range(0;$a|length) as $i ($a; .[$i] += $b[$i]);
if length <= 1 then .
else reduce .[1:][] as $v (.[0] ; [., $v]|sum2)
end;
def multiply(scalar): [ .[] * scalar ];
def negate: multiply(-1);
def minus(v): [., (v|negate)] | sum;
def divide(scalar):
if scalar == 0 then error("division of a vector by 0 is not supported")
else [ .[] / scalar ]
end;
def r: (.[0] | .*.) + (.[1] | .*.) | sqrt;
def atan2:
def pi: 1 | atan * 4;
def sign: if . < 0 then -1 elif . > 0 then 1 else 0 end;
.[0] as $x | .[1] as $y
| if $x == 0 then $y | sign * pi / 2
else ($y / $x) | if $x > 0 then atan elif . > 0 then atan - pi else atan + pi end
end;
def angle: atan2;
def topolar: [r, angle];

View file

@ -0,0 +1,23 @@
def examples:
def pi: 1 | atan * 4;
[1,1] as $v
| [3,4] as $w
| polar(1; pi/2) as $z
| polar(-2; pi/4) as $z2
| "v is \($v)",
" w is \($w)",
"v + w is \([$v, $w] | sum)",
"v - w is \( $v |minus($w))",
" - v is \( $v|negate )",
"w * 5 is \($w | multiply(5))",
"w / 2 is \($w | divide(2))",
"v|topolar is \($v|topolar)",
"w|topolar is \($w|topolar)",
"z = polar(1; pi/2) is \($z)",
"z|topolar is \($z|topolar)",
"z2 = polar(-2; pi/4) is \($z2)",
"z2|topolar is \($z2|topolar)",
"z2|topolar|polar is \($z2|topolar|polar2vector)" ;
examples

View file

@ -0,0 +1,15 @@
$ jq -r -n -f vector.jq
v is [1,1]
w is [3,4]
v + w is [4,5]
v - w is [-2,-3]
- v is [-1,-1]
w * 5 is [15,20]
w / 2 is [1.5,2]
v|topolar is [1.4142135623730951,0.7853981633974483]
w|topolar is [5,0.9272952180016122]
z = polar(1; pi/2) is [6.123233995736766e-17,1]
z|topolar is [1,1.5707963267948966]
z2 = polar(-2; pi/4) is [-1.4142135623730951,-1.414213562373095]
z2|topolar is [2,-2.356194490192345]
z2|topolar|polar is [-1.414213562373095,-1.4142135623730951]