Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View file

@ -1,23 +1,23 @@
/*REXX program to show how to support math functions for complex numbers*/
x = '(5,3i)' /*this little piggy uses "I" (or "i") ···*/
y = '( .5, 6j)' /*this little piggy uses "J" (or "j") ···*/
/*REXX pgm demonstrates how to support some math functions for complex numbers*/
x = '(5,3i)' /*define X ─── can use I i J or j */
y = "( .5, 6j)" /*define Y " " " " " " " */
sum = Cadd(x,y) ; say ' addition: ' x " + " y ' = ' sum
dif = Csub(x,y) ; say ' subtraction: ' x " + " y ' = ' dif
prod = Cmul(x,y) ; say 'multiplication: ' x " * " y ' = ' prod
quot = Cdiv(x,y) ; say ' division: ' x " ÷ " y ' = ' quot
inv = Cinv(x) ; say ' inverse: ' x " = " inv
cnjX = Ccnj(x) ; say ' conjugate of: ' x " = " cnjX
negX = Cneg(x) ; say ' negation of: ' x " = " negX
exit /*stick a fork in it, we're done.*/
/*─────────────────────────────────────one─liners──────────────────────────────────────────────*/
Ccnj: procedure; arg a ',' b,c ',' d; call Cg; r1=a; r2=-b; return Cr()
Cadd: procedure; arg a ',' b,c ',' d; call Cg; r1=a+c; r2=b+d; return Cr()
Csub: procedure; arg a ',' b,c ',' d; call Cg; r1=a-c; r2=b-d; return Cr()
Cmul: procedure; arg a ',' b,c ',' d; call Cg; r1=a*c-b*d; r2=b*c+a*d; return Cr()
Cdiv: procedure; arg a ',' b,c ',' d; call Cg;_=c*c+d*d;r1=(a*c+b*d)/_;r2=(b*c-a*d)/_;return Cr()
Cdej: return word(translate(arg(1), , '{[(JI)]}') 0, 1)
Cg: a=Cdej(a); b=Cdej(b); c=Cdej(c); d=Cdej(d); return
say ' addition: ' x " + " y ' = ' Cadd(x,y)
say ' subtraction: ' x " - " y ' = ' Csub(x,y)
say 'multiplication: ' x " * " y ' = ' Cmul(x,y)
say ' division: ' x " ÷ " y ' = ' Cdiv(x,y)
say ' inverse: ' x " = " Cinv(x,y)
say ' conjugate of: ' x " = " Conj(x,y)
say ' negation of: ' x " = " Cneg(x,y)
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────one─liner subroutines─────────────────────*/
Conj: procedure; arg a ',' b,c ',' d; call C#; return C$( a, -b)
Cadd: procedure; arg a ',' b,c ',' d; call C#; return C$(a+c, b+d)
Csub: procedure; arg a ',' b,c ',' d; call C#; return C$(a-c, b-d)
Cmul: procedure; arg a ',' b,c ',' d; call C#; return C$(ac-bd, bc+ad)
Cdiv: procedure; arg a ',' b,c ',' d; call C#; return C$((ac+bd)/s, (bc-ad)/s)
Cinv: return Cdiv(1, arg(1))
Cneg: return Cmul(arg(1), -1)
Cr: _='['r1; if r2\=0 then _=_','r2"j"; return _']'
C_: arg __; return word(translate(__, , '{[(JI)]}') 0, 1) /*get # or 0*/
C#: a=C_(a);b=C_(b);c=C_(c);d=C_(d);ac=a*c;ad=a*d;bc=b*c;bd=b*d;s=c*c+d*d;return
C$: parse arg r,c;_='['r; if c\=0 then _=_','c"j"; return _']' /*uses j*/

View file

@ -0,0 +1,4 @@
require "cmath"
CMath.sqrt(-9) #=> 0+3.0i
CMath.acos(0+3.0i) #=> (1.5707963267948966-1.8184464592320668i)
#etc

View file

@ -1,16 +1,16 @@
extern crate num;
use num::complex::Cmplx;
use num::complex::Complex;
fn main() {
let a = Cmplx::new(-4.0, 5.0);
let b = Cmplx::new(1.0, 1.0);
// two valid forms of definition
let a = Complex {re:-4.0, im: 5.0};
let b = Complex::new(1.0, 1.0);
println!("a = {}", a);
println!("b = {}", b);
println!("a + b = {}", a + b);
println!("a * b = {}", a * b);
println!("1 / a = {}", Cmplx::new(1.0, 0.0) / a);
println!("-a = {}", -a);
println!("conj a = {}", a.conj());
println!(" a = {}", a);
println!(" b = {}", b);
println!(" a + b = {}", a + b);
println!(" a * b = {}", a * b);
println!(" 1 / a = {}", a.inv());
println!(" -a = {}", -a);
println!("conj(a) = {}", a.conj());
}