September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -1,23 +0,0 @@
(deftype Complex [real imag]
Object
(toString [this] (str real " " imag "j")))
(defn c-add [^Complex a ^Complex b]
(Complex. (+ (.real a) (.real b))
(+ (.imag a) (.imag b))))
(defn c-mul [^Complex a ^Complex b]
(Complex. (- (* (.real a) (.real b)) (* (.imag a) (.imag b)))
(+ (* (.real a) (.imag b)) (* (.imag a) (.real b)))))
(defn c-neg [^Complex a]
(Complex. (- (.real a)) (- (.imag a))))
(defn c-inv [^Complex a]
(let [r (.real a)
i (.imag a)
m (+ (* r r) (* i i))]
(Complex. (/ r m) (- (/ i m)))))
(defn c-conj [^Complex a]
(Complex. (.real a) (- (.imag a))))

View file

@ -1,17 +0,0 @@
(Complex. 1 1)
#<Complex 1 1j>
(c-add (Complex. 3.17 7) (Complex. 1 1.77))
#<Complex 4.17 8.77j>
(c-mul (Complex. 0 1) (Complex. 0 1))
#<Complex -1 0j>
(c-neg (Complex. 1 2))
#<Complex -1 -2j>
(c-inv (Complex. 1 1))
#<Complex 1/2 -1/2j>
(c-conj (Complex. 1 1))
#<Complex 1 -1j>

View file

@ -1,60 +0,0 @@
# create an immutable Complex type
class Complex
constructor: (@r=0, @i=0) ->
@magnitude = @r*@r + @i*@i
plus: (c2) ->
new Complex(
@r + c2.r,
@i + c2.i
)
times: (c2) ->
new Complex(
@r*c2.r - @i*c2.i,
@r*c2.i + @i*c2.r
)
negation: ->
new Complex(
-1 * @r,
-1 * @i
)
inverse: ->
throw Error "no inverse" if @magnitude is 0
new Complex(
@r / @magnitude,
-1 * @i / @magnitude
)
toString: ->
return "#{@r}" if @i == 0
return "#{@i}i" if @r == 0
if @i > 0
"#{@r} + #{@i}i"
else
"#{@r} - #{-1 * @i}i"
# test
do ->
a = new Complex(5, 3)
b = new Complex(4, -3)
sum = a.plus b
console.log "(#{a}) + (#{b}) = #{sum}"
product = a.times b
console.log "(#{a}) * (#{b}) = #{product}"
negation = b.negation()
console.log "-1 * (#{b}) = #{negation}"
diff = a.plus negation
console.log "(#{a}) - (#{b}) = #{diff}"
inverse = b.inverse()
console.log "1 / (#{b}) = #{inverse}"
quotient = product.times inverse
console.log "(#{product}) / (#{b}) = #{quotient}"

View file

@ -1,7 +0,0 @@
> coffee complex.coffee
(5 + 3i) + (4 - 3i) = 9
(5 + 3i) * (4 - 3i) = 29 - 3i
-1 * (4 - 3i) = -4 + 3i
(5 + 3i) - (4 - 3i) = 1 + 6i
1 / (4 - 3i) = 0.16 + 0.12i
(29 - 3i) / (4 - 3i) = 5 + 3i

View file

@ -1,6 +1,5 @@
include complex.seq
: ZNEGATE ( r i -- -r -i ) fswap fnegate fswap fnegate ;
S" fsl-util.fs" REQUIRED
S" complex.fs" REQUIRED
zvariable x
zvariable y
@ -9,5 +8,6 @@ pi 1.2e y z!
x z@ y z@ z+ z.
x z@ y z@ z* z.
1e 0e zconstant 1+0i
1+0i x z@ z/ z.
x z@ znegate z.

View file

@ -12,7 +12,7 @@ program cdemo2
abquot = a / b
abpow = a ** b
areal = real(a) ! Real part
aimag = imag(a) ! Imaginary part
aimag = imag(a) ! Imaginary part. Function imag(a) is possibly not recognised. Use aimag(a) if so.
newc = cmplx(x,y) ! Creating a complex on the fly from two reals intrinsically
! (initializer only works in declarations)
newc = x + y*i ! Creating a complex on the fly from two reals arithmetically

View file

@ -1,14 +0,0 @@
import Data.Complex
main = do
let a = 1.0 :+ 2.0 -- complex number 1+2i
let b = 4 -- complex number 4+0i
-- 'b' is inferred to be complex because it's used in
-- arithmetic with 'a' below.
putStrLn $ "Add: " ++ show (a + b)
putStrLn $ "Subtract: " ++ show (a - b)
putStrLn $ "Multiply: " ++ show (a * b)
putStrLn $ "Divide: " ++ show (a / b)
putStrLn $ "Negate: " ++ show (-a)
putStrLn $ "Inverse: " ++ show (recip a)
putStrLn $ "Conjugate:" ++ show (conjugate a)

View file

@ -1,8 +0,0 @@
*Main> main
Add: 5.0 :+ 2.0
Subtract: (-3.0) :+ 2.0
Multiply: 4.0 :+ 8.0
Divide: 0.25 :+ 0.5
Negate: (-1.0) :+ (-2.0)
Inverse: 0.2 :+ (-0.4)
Conjugate:1.0 :+ (-2.0)

View file

@ -0,0 +1,39 @@
class Complex(private val real: Double, private val imag: Double) {
operator fun plus(other: Complex) = Complex(real + other.real, imag + other.imag)
operator fun times(other: Complex) = Complex(
real * other.real - imag * other.imag,
real * other.imag + imag * other.real
)
fun inv(): Complex {
val denom = real * real + imag * imag
return Complex(real / denom, -imag / denom)
}
operator fun unaryMinus() = Complex(-real, -imag)
operator fun minus(other: Complex) = this + (-other)
operator fun div(other: Complex) = this * other.inv()
fun conj() = Complex(real, -imag)
override fun toString() =
if (imag >= 0.0) "$real + ${imag}i"
else "$real - ${-imag}i"
}
fun main(args: Array<String>) {
val x = Complex(1.0, 3.0)
val y = Complex(5.0, 2.0)
println("x = $x")
println("y = $y")
println("x + y = ${x + y}")
println("x - y = ${x - y}")
println("x * y = ${x * y}")
println("x / y = ${x / y}")
println("-x = ${-x}")
println("1 / x = ${x.inv()}")
println("x* = ${x.conj()}")
}

View file

@ -1,10 +1,10 @@
import complex
var a: TComplex = (1.0,1.0)
var b: TComplex = (3.1415,1.2)
var a: Complex = (1.0,1.0)
var b: Complex = (3.1415,1.2)
echo ("a : " & $a)
echo ("b : " & $b)
echo ("a + b: " & $(a + b))
echo ("a * b: " & $(a * b))
echo ("1/a : " & $(1/a))
echo ("-a : " & $(-a))
echo("a : " & $a)
echo("b : " & $b)
echo("a + b: " & $(a + b))
echo("a * b: " & $(a * b))
echo("1/a : " & $(1/a))
echo("-a : " & $(-a))

View file

@ -1,10 +0,0 @@
import complex
var a: TComplex = (1.0,1.0)
var b: TComplex = (3.1415,1.2)
echo ("a : " & $a)
echo ("b : " & $b)
echo ("a + b: " & $(a + b))
echo ("a * b: " & $(a * b))
echo ("1/a : " & $(1/a))
echo ("-a : " & $(-a))

View file

@ -0,0 +1,23 @@
(define A 0+1i)
(define B 1+0i)
(print (+ A B))
; <== 1+i
(print (- A B))
; <== -1+i
(print (* A B))
; <== 0+i
(print (/ A B))
; <== 0+i
(define C 2/7-3i)
(print "real part of " C " is " (car C))
; <== real part of 2/7-3i is 2/7
(print "imaginary part of " C " is " (cdr C))
; <== imaginary part of 2/7-3i is -3

View file

@ -0,0 +1,137 @@
c1 = .complex~new(1, 2)
c2 = .complex~new(3, 4)
r = 7
say "c1 =" c1
say "c2 =" c2
say "r =" r
say "-c1 =" (-c1)
say "c1 + r =" c1 + r
say "c1 + c2 =" c1 + c2
say "c1 - r =" c1 - r
say "c1 - c2 =" c1 - c2
say "c1 * r =" c1 * r
say "c1 * c2 =" c1 * c2
say "inv(c1) =" c1~inv
say "conj(c1) =" c1~conjugate
say "c1 / r =" c1 / r
say "c1 / c2 =" c1 / c2
say "c1 == c1 =" (c1 == c1)
say "c1 == c2 =" (c1 == c2)
::class complex
::method init
expose r i
use strict arg r, i = 0
-- complex instances are immutable, so these are
-- read only attributes
::attribute r GET
::attribute i GET
::method negative
expose r i
return self~class~new(-r, -i)
::method add
expose r i
use strict arg other
if other~isa(.complex) then
return self~class~new(r + other~r, i + other~i)
else return self~class~new(r + other, i)
::method subtract
expose r i
use strict arg other
if other~isa(.complex) then
return self~class~new(r - other~r, i - other~i)
else return self~class~new(r - other, i)
::method times
expose r i
use strict arg other
if other~isa(.complex) then
return self~class~new(r * other~r - i * other~i, r * other~i + i * other~r)
else return self~class~new(r * other, i * other)
::method inv
expose r i
denom = r * r + i * i
return self~class~new(r/denom,-i/denom)
::method conjugate
expose r i
return self~class~new(r, -i)
::method divide
use strict arg other
-- this is easier if everything is a complex number
if \other~isA(.complex) then other = .complex~new(other)
-- division is multiplication with the inversion
return self * other~inv
::method "=="
expose r i
use strict arg other
if \other~isa(.complex) then return .false
-- Note: these are numeric comparisons, so we're using the "="
-- method so those are handled correctly
return r = other~r & i = other~i
::method "\=="
use strict arg other
return \self~"\=="(other)
::method "="
-- this is equivalent of "=="
forward message("==")
::method "\="
-- this is equivalent of "\=="
forward message("\==")
::method "<>"
-- this is equivalent of "\=="
forward message("\==")
::method "><"
-- this is equivalent of "\=="
forward message("\==")
-- some operator overrides -- these only work if the left-hand-side of the
-- subexpression is a quaternion
::method "*"
forward message("TIMES")
::method "/"
forward message("DIVIDE")
::method "-"
-- need to check if this is a prefix minus or a subtract
if arg() == 0 then
forward message("NEGATIVE")
else
forward message("SUBTRACT")
::method "+"
-- need to check if this is a prefix plus or an addition
if arg() == 0 then
return self -- we can return this copy since it is immutable
else
forward message("ADD")
::method string
expose r i
return r self~formatnumber(i)"i"
::method formatnumber private
use arg value
if value > 0 then return "+" value
else return "-" value~abs
-- override hashcode for collection class hash uses
::method hashCode
expose r i
return r~hashcode~bitxor(i~hashcode)

View file

@ -1,5 +1,5 @@
function show([System.Numerics.Complex]$c) {
if(0 -ge $c.Imginary) {
if(0 -le $c.Imaginary) {
return "$($c.Real)+$($c.Imaginary)i"
} else {
return "$($c.Real)$($c.Imaginary)i"

View file

@ -1,11 +1,11 @@
var a = 1:1; # Complex(1, 1)
var b = 3.14159:1.25; # Complex(3.14159, 1.25)
var a = 1:1 # Complex(1, 1)
var b = 3.14159:1.25 # Complex(3.14159, 1.25)
[ a + b, # addition
a * b, # multiplication
-a, # negation
1 / a, # multiplicative inverse
~a, # complex conjugate
a.inv, # multiplicative inverse
a.conj, # complex conjugate
a.abs, # abs
a.sqrt, # sqrt
b.re, # real

View file

@ -0,0 +1,17 @@
' complex numbers are native for "smart BASIC"
A=1+2i
B=3-5i
' all math operations and functions work with complex numbers
C=A*B
PRINT SQR(-4)
' example of solving quadratic equation with complex roots
' x^2+2x+5=0
a=1 ! b=2 ! c=5
x1=(-b+sqr(b^2-4*a*c))/(2*a)
x2=(-b-sqr(b^2-4*a*c))/(2*a)
print x1,x2
' gives output
-1+2i -1-2i

View file

@ -0,0 +1,42 @@
mata
C(2,3)
2 + 3i
a=2+3i
b=1-2*i
a+b
-5 + 3i
a-b
9 + 3i
a*b
-14 - 21i
a/b
-.285714286 - .428571429i
-a
-2 - 3i
1/a
.153846154 - .230769231i
conj(a)
2 - 3i
abs(a)
3.605551275
arg(a)
.9827937232
exp(a)
-7.31511009 + 1.04274366i
log(a)
1.28247468 + .982793723i
end

View file

@ -0,0 +1,8 @@
var [const] GSL=Import("zklGSL"); // libGSL (GNU Scientific Library)
(GSL.Z(3,4) + GSL.Z(1,2)).println(); // (4.00+6.00i)
(GSL.Z(3,4) - GSL.Z(1,2)).println(); // (2.00+2.00i)
(GSL.Z(3,4) * GSL.Z(1,2)).println(); // (-5.00+10.00i)
(GSL.Z(3,4) / GSL.Z(1,2)).println(); // (2.20-0.40i)
(GSL.Z(1,0) / GSL.Z(1,1)).println(); // (0.50-0.50i) // inversion
(-GSL.Z(3,4)).println(); // (-3.00-4.00i)
GSL.Z(3,4).conjugate().println(); // (3.00-4.00i)