Skip to content

Instantly share code, notes, and snippets.

@JerryWiltz
Last active May 30, 2019 00:33
Show Gist options
  • Save JerryWiltz/51b63f59e98f13ac71eb7c6f9024e995 to your computer and use it in GitHub Desktop.
Save JerryWiltz/51b63f59e98f13ac71eb7c6f9024e995 to your computer and use it in GitHub Desktop.
Complex Arithmetic and Matrix Algebra
license: gpl-3.0
height: 600
scrolling: yes
border: yes
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Complex Numbers and Martices</title>
</head>
<body>
<h3>Complex Numbers and Matrices</h3>
<script src="https://cdn.jsdelivr.net/gh/JerryWiltz/nP@master/dist/nP.js"></script>
<script>
// define log function
function log (text) {
var p = document.createElement('p');
p.innerHTML = text + '<br>';
document.body.appendChild(p);
}
log('Part 1. Complex Numbers');
log('Basic add, subtract, multiply, and divide');
// how to define complex numbers in nPort, for any c = x + iy, c = nP.complex(x, y)
var c1 = nP.complex(5, 1);
var c2 = nP.complex(1, 3);
var cAdd = c1.add(c2);
var cSub = c1.sub(c2);
var cMul = c1.mul(c2);
var c3 = nP.complex(9, -8);
var c4 = nP.complex(5, 2);
var cDiv = c3.div(c4);
log('cAdd is (' + cAdd.x.toString() + ', i' + cAdd.y.toString() + ')');
log('cSub is (' + cSub.x.toString() + ', i' + cSub.y.toString() + ')');
log('cMul is (' + cMul.x.toString() + ', i' + cMul.y.toString() + ')');
log('cDiv is (' + cDiv.x.toString() + ', i' + cDiv.y.toString() + ')');
log('--------------------------------------------------------------------');
log('Basic method chaining example shows cMethodChain is equal to c1');
var cMethodChain = c1.add(c2).sub(c2).mul(c3).div(c3);
log('cMethodChain is (' + cMethodChain.x.toString() + ', i' + cMethodChain.y.toString() + ')');
log('====================================================================');
log('Part 2. Complex matrix add, subtract, multipley, and invert');
// put them in a 2 by 2 matrix
var m1 = nP.matrix([
[c1, c2],
[c3, c4]
]);
// display m1
log('The original matrix, m1');
nP.showMatrixCplx(m1.m);
// invert matrix, m1
log('The inverted matrix, m2');
var m2 = m1.invertCplx();
// display m2
nP.showMatrixCplx(m2.m);
// multipliy m1 by m2
var m3 = m1.mulCplx(m2);
// display m3
log('The product of m1 and m2, m3');
nP.showMatrixCplx(m3.m);
log('--------------------------------------------------------------------');
log('Basic method chaining for Matices')
var m4 = m3.mulCplx(m2).subCplx(m1).addCplx(m1);
log('The result of method chaining');
nP.showMatrixCplx(m4.m);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment