Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
56
Task/Arithmetic-Complex/AWK/arithmetic-complex.awk
Normal file
56
Task/Arithmetic-Complex/AWK/arithmetic-complex.awk
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
# simulate a struct using associative arrays
|
||||
function complex(arr, re, im) {
|
||||
arr["re"] = re
|
||||
arr["im"] = im
|
||||
}
|
||||
|
||||
function re(cmplx) {
|
||||
return cmplx["re"]
|
||||
}
|
||||
|
||||
function im(cmplx) {
|
||||
return cmplx["im"]
|
||||
}
|
||||
|
||||
function printComplex(cmplx) {
|
||||
print re(cmplx), im(cmplx)
|
||||
}
|
||||
|
||||
function abs2(cmplx) {
|
||||
return re(cmplx) * re(cmplx) + im(cmplx) * im(cmplx)
|
||||
}
|
||||
|
||||
function abs(cmplx) {
|
||||
return sqrt(abs2(cmplx))
|
||||
}
|
||||
|
||||
function add(res, cmplx1, cmplx2) {
|
||||
complex(res, re(cmplx1) + re(cmplx2), im(cmplx1) + im(cmplx2))
|
||||
}
|
||||
|
||||
function mult(res, cmplx1, cmplx2) {
|
||||
complex(res, re(cmplx1) * re(cmplx2) - im(cmplx1) * im(cmplx2), re(cmplx1) * im(cmplx2) + im(cmplx1) * re(cmplx2))
|
||||
}
|
||||
|
||||
function scale(res, cmplx, scalar) {
|
||||
complex(res, re(cmplx) * scalar, im(cmplx) * scalar)
|
||||
}
|
||||
|
||||
function negate(res, cmplx) {
|
||||
scale(res, cmplx, -1)
|
||||
}
|
||||
|
||||
function conjugate(res, cmplx) {
|
||||
complex(res, re(cmplx), -im(cmplx))
|
||||
}
|
||||
|
||||
function invert(res, cmplx) {
|
||||
conjugate(res, cmplx)
|
||||
scale(res, res, 1 / abs(cmplx))
|
||||
}
|
||||
|
||||
BEGIN {
|
||||
complex(i, 0, 1)
|
||||
mult(i, i, i)
|
||||
printComplex(i)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue