Skip to content

Instantly share code, notes, and snippets.

@vazexqi
Last active June 30, 2016 14:33
Show Gist options
  • Save vazexqi/5316339 to your computer and use it in GitHub Desktop.
Save vazexqi/5316339 to your computer and use it in GitHub Desktop.
Simple Linear System Solver
<!--
Code by the Illinois VLSI CAD teaching staff.
Released under the University of Illinois/NCSA Open Source License
-->
<div class="alert alert-info">
<p>
Fill in the matrix A and the vector b and click "Solve!".
</p>
</div>
<div class="row offset1">
<div class="span4">
<b>Input matrix A</b>
<br>
<textarea rows="5" id="matrixA" style="width:250px;">
1 2
3 4
</textarea>
</div>
</div>
<div class="row offset1">
<div class="span4">
<b>Input vector b</b>
<br>
<textarea rows ="5" id="vectorB" style="width:250px;">
17
39
</textarea>
</div>
</div>
<div class="row offset1">
<div class="span4">
<b>Solution vector x</b>
<br>
<textarea readonly rows ="5" id="vectorX" style="width:250px;">
</textarea>
</div>
</div>
<div class="row offset1">
<div class="span4">
<p>
<button class="btn btn-primary" type="button" id="solveButton">Solve!</button>
</p>
<br>
<br>
Solver uses the <a href="http://www.numericjs.com/documentation.html">numerical.js</a> library by Sebastien Loisel.
</div>
</div>
<!-- Serve this through a https:// or else Chrome will complain -->
<!-- Ideally, I don't want to load jQuery since Coursera has its own version. However, Coursera chooses to load its script at the end so $(...) isn't available when we need it. -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://spark-public.s3.amazonaws.com/vlsicad/javascript_tools/numeric-1.2.6.min.js"></script>
<script>
// Create namespace to be safe - don't want any collisions with Coursera's code
var edu = {
illinois: {
vlsicad: (function() {
var solveEquation = function() {
var matrixA = matrixToArray($("#matrixA").val());
var vectorB = columnToArray($("#vectorB").val());
var solution = numeric.solve(matrixA, vectorB);
displaySolution(solution);
return solution; // For checking in the debugger
}
var matrixToArray = function(matrixString) {
var rows = matrixString.split("\n");
var filteredRows = rows.filter(function(e) { return !isBlank(e) });
return filteredRows.map(rowToArray);
}
var rowToArray = function(rowString) {
return rowString.split(/\s+/).map(parseFloat);
}
var columnToArray = function(columnString) {
var rows = columnString.split("\n");
var filteredRows = rows.filter(function(e) { return !isBlank(e)});
return filteredRows.map(parseFloat);
}
var displaySolution = function(solutionArray) {
var stringToDisplay = "";
solutionArray.forEach(function(e) {
stringToDisplay += e.toFixed(3) + "\n";
});
$("#vectorX").val(stringToDisplay);
}
var isBlank = function(string) {
return (!string || /^\s*$/.test(string));
}
// Only expose the solveEquation function
return {
solveEquation: solveEquation
};
})()
}
}
$(function() {
$('#solveButton').bind('click', edu.illinois.vlsicad.solveEquation);
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment