xxxxxxxxxx
94
let a
let b
let c
let d
//fuctions
function mult(a1, b1, a2, b2, log) {
//(a1+b1i)*(a2+b2i)=(a1*a2 + b1*b2, (a1*b2 + a2*b1)i)
let real = a1 * a2 - b1 * b2
let imagenary = a1 * b2 + a2 * b1
if (log) {
console.log(real + "+" + (imagenary + "i"))
}
return createVector(real, imagenary)
}
function sq(a, b, log) {
let real = (a * a) - (b * b)
let imagenary = [2 * a * b]
imagenary[1] = imagenary[0] * -1
if (log) {
if (b < 0) {
console.log("(" + a + "-" + b + "i" + ")^2 =")
} else {
console.log("(" + a + "+" + b + "i" + ")^2 =")
}
if (real == 0){
console.log(imagenary[0]+"i\n"+imagenary[1]+"i")
}
else {
if (imagenary[0] < 0) {
//if negative
console.log(real + "" + imagenary[0] + "i")
} else if (imagenary[0] > 0) {
//if positive or zero
console.log(real + "+" + imagenary[0] + "i")
}
console.log(real + "" + imagenary[1] + "i")
}
// else if (imagenary[1] < 0) {
// //if negative
// console.log(real + "" + imagenary[1] + "i")
// } else {
// //if positive
// console.log(real + "" + imagenary[1] + "i")
// }
}
return [real, imagenary[0], real, imagenary[1]]
}
function sqrt(a, b, logg, showE) {
if (showE) {
if (b < 0) {
console.log("sqrt(" + a + "+" + b + "i) =")
} else {
console.log("sqrt(" + a + "+" + b + "i) =")
}
}
var sqrtab = [sqrt((a * a) + (b * b)), sqrt((a ^ 2) + (b ^ 2)) * -1]
let real = [(1 / sqrt(2)) * sqrt(sqrtab[0] + a), ((1 / sqrt(2)) * sqrt(sqrtab[0] + a)) * -1]
let imagenary = [(b / abs(b)) * sqrt(((-1 * a) + sqrtab[0]) / 2), (b / abs(b)) * sqrt(((-1 * a) + sqrtab[0]) / 2) * -1]
if (logg) {
for (let i = 0; i <= 1; i++) {
if (imagenary[i] < 0) {
console.log(real[i] + "" + imagenary[i] + "i")
} else {
console.log(real[i] + "+" + imagenary[i] + "i")
}
}
}
// return answer
return [real[0], imagenary[0], real[1], imagenary[1]]
}
//is negative
function isneg(num){
return num < 0;
}