Posts Tagged ‘algebraic terms

04
Apr
09

[Javascript] Synthetic Division

An easier method of dividing polynomials.
This function uses synthetic division to divide a polynomial by a simple ax+b
The polynomial must be in the format of a1x^n + a2x^(n-1) + a3x^(n-2)…aix^(nn)

function syntheticDivision(poly, division) {
	//Format the equations correctly
	poly = poly.replace(/(--|\+\+)/g, "+");
	poly = poly.replace(/(-\+|\+-)/g, "-");
	poly = poly.replace(/^\+/g, "");
	poly = poly.replace(/\s/g, "");
	division = division.replace(/(--|\+\+)/g, "+");
	division = division.replace(/(-\+|\+-)/g, "-");
	division = division.replace(/^\+/g, "");
	division = division.replace(/\s/g, "");
	//Remove all x's and powers
	poly = poly.replace(/x\^?\d?/g, "");
	//Add spaces to the equation to break it apart
	poly = poly.replace(/([+-])/g, " $1");
	//Split the equation at the spaces
	var equ = poly.split(" ");
	//Rearrange division to equal 0
	var divide = division.split("x")[0]
	if(divide==""){ divide="1"; }
	division = (~Number(division.split("x")[1])+1)/divide;
	//Begin division
	var output = ""
	var lastTerm = "0"
	for(var i=0;i<equ.length-1;i++) {
		if(equ[i].split("x")=="+" || equ[i].split("x")=="-") { equ[i] = equ[i]+"1" }
		var dt = Number(Number(equ[i]) + Number(lastTerm));
		var x = (dt/divide);
		output += x + "x^" + (equ.length-i-2) + "+";
		lastTerm = dt * division;
	}
	//Format output
	output = output.replace(/\+([+-])/g, "$1");
	output = output.replace(/0x\^\d/g, "");
	output = output.replace(/(x\^0)?\+$/g, "");
	output = output.replace(/x\^1/g, "x");
	//Calculate remainder
	output += " : Remainder [" + String(Number(equ[equ.length-1]) + Number(lastTerm)) + "]";
	return(output);
}

Usage:

<script language="javascript" type="text/javascript">
var poly = "14x^4-5x^3-11x^2-11x+8";
var division = "2x-1";
alert(syntheticDivision(poly, division));
</script>

Would output:
7x^3 + 1x^2 – 5x -8 : Remainder [0]

04
Apr
09

[Javascript] Simple Polynomial Long Division

This function uses long division to divide a polynomial by a simple ax+b

The polynomial must be in the format of ax^n + bx^(n-1) + cx^(n-2)…zx^(nn)

The function itself relies upon 4 other functions in order to work, these are used to carry out basic operations on the terms in the polynomail:

//Used to break an { ax^n } into its compontents
function extractCompontents(Term, constantChar) {
	var Comps = new Array();
	Comps[0] = Term.split(constantChar)[0];
	Comps[1] = Term.split("^")[1];
	if(Comps[0] == "") { Comps[0]=1; }
	if(String(Comps[1]) == "undefined") { Comps[1]=1; }
	return Comps;
}
function divideTerm(Term1, Term2, constantChar) {
    var extTerm1 = extractCompontents(Term1, constantChar)
	var extTerm2 = extractCompontents(Term2, constantChar)
	return String(extTerm1[0]/extTerm2[0]) + constantChar + "^" + String(extTerm1[1]-extTerm2[1]);
}
function multiplyTerm(Term1, Term2, constantChar) {
	var extTerm1 = extractCompontents(Term1, constantChar)
	return String(extTerm1[0] * Term2) + constantChar + "^" + String(extTerm1[1]);
}
function subtractTerm(Term1, Term2, constantChar) {
    var extTerm1 = extractCompontents(Term1, constantChar)
	var extTerm2 = extractCompontents(Term2, constantChar)
	if(extTerm1[1] != extTerm2[1]) { return null; }
	return String(extTerm1[0]-extTerm2[0]) + constantChar + "^" + String(extTerm1[1]);
}

The function itself is as follows:

function longAlgebraicDivision(poly, division) {
	//Format the equations correctly
	poly = poly.replace(/(--|\+\+)/g, "+");
	poly = poly.replace(/(-\+|\+-)/g, "-");
	poly = poly.replace(/^\+/g, "");
	poly = poly.replace(/\s/g, "");
	division = division.replace(/(--|\+\+)/g, "+");
	division = division.replace(/(-\+|\+-)/g, "-");
	division = division.replace(/^\+/g, "");
	division = division.replace(/\s/g, "");
	//Add spaces to the equation to break it apart
	poly = poly.replace(/([+-])/g, " $1");
	//Split the equation at the spaces
	var equ = poly.split(" ");
	//Begin the division
	var output = ""
	var lastTerm = ""
	for(var i=0;i<equ.length-1;i++) {
		var term = equ[i];
		if(i==0) {
			var dt = divideTerm(term, division.split("x")[0], "x");
			output += dt + "+";
			dt = multiplyTerm(dt, division.split("x")[1], "x");
			lastTerm = dt;
		}else{
			var dt = subtractTerm(term, lastTerm, "x");
			dt = divideTerm(dt, division.split("x")[0], "x");
			output += dt + "+";
			dt = multiplyTerm(dt, division.split("x")[1], "x");
			lastTerm = dt;
		}
	}
	//Format output
	output = output.replace(/\+([+-])/g, "$1");
	output = output.replace(/x\^0\+$/g, "");
	output = output.replace(/x\^1/g, "x");
	//Calculate remainder
	lastTerm = lastTerm.replace(/x\^0/g, "");
	output += " : Remainder [" + String(Number(equ[equ.length-1]) - Number(lastTerm)) + "]";
	return output;

Usage:

<script language="javascript" type="text/javascript">
var poly = "14x^4-5x^3-11x^2-11x+8";
var division = "2x-1";
alert(longAlgebraicDivision(poly, division));
</script>

Would output:

7x^3 + 1x^2 – 5x -8 : Remainder [0]

04
Apr
09

[Javascript] Divide Simple Algebraic Terms

Been working on some maths things today, and wrote this function to divide basic terms.
Will work on any string in the format of:
ax^n; where a = any number, x = any constant character, n = any number

function divideTerm(Term1, Term2, constantChar) {
	//The coefficient of the first Term
	var Term1coeff = Term1.split(constantChar)[0];
	//The power of the first Term
	var Term1power = Term1.split("^")[1];
	//The coefficient of the second Term
	var Term2coeff = Term2.split(constantChar)[0];
	//The power of the second Term
	var Term2power = Term2.split("^")[1];
	//If no coefficient, set to 1
	if(Term1coeff==""){Term1coeff=1;}
	//If no power, set to 1
	if(String(Term1power)=="undefined"){Term1power=1;}
	//If no coefficient, set to 1
	if(Term2coeff==""){Term2coeff=1;}
	//If no power, set to 1
	if(String(Term2power)=="undefined"){Term2power=1;}
	//Divide the two coefficients, add the character and claret, subtract the two powers
	return String(Term1coeff/Term2coeff) + constantChar + "^" + String(Term1power-Term2power);
}

Usage:
Sample project:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Dividing Alg Terms</title>
<script language="javascript" type="text/javascript">
function divideTerm(Term1, Term2, constantChar) {
	var Term1coeff = Term1.split(constantChar)[0];
	var Term1power = Term1.split("^")[1];
	var Term2coeff = Term2.split(constantChar)[0];
	var Term2power = Term2.split("^")[1];
	if(Term1coeff==""){Term1coeff=1;}
	if(String(Term1power)=="undefined"){Term1power=1;}
	if(Term2coeff==""){Term2coeff=1;}
	if(String(Term2power)=="undefined"){Term2power=1;}
	return String(Term1coeff/Term2coeff) + constantChar + "^" + String(Term1power-Term2power);
}

function divide() {
	document.getElementById("result").value = divideTerm(
		document.getElementById("trm1").value,
		document.getElementById("trm2").value,
		document.getElementById("char").value);
}
</script>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
  <table width="297" border="0">
    <tr>
      <td width="143"><strong>Term 1: </strong></td>
      <td width="144"><input name="textfield3" type="text" id="trm1" value="147x^23" /></td>
    </tr>
    <tr>
      <td><strong>Term 2: </strong></td>
      <td><input name="textfield4" type="text" id="trm2" value="24.5x^19" /></td>
    </tr>
    <tr>
      <td><strong>Constant Character: </strong></td>
      <td><input name="textfield" type="text" id="char" value="x" /></td>
    </tr>
    <tr>
      <td colspan="2"><div align="center">
        <br />
	    <input name="button" type="button" value="Divide" onclick="divide()" />
      </div></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td><strong>Result:</strong></td>
      <td><input type="text" name="textfield2" readonly="true" id="result"/></td>
    </tr>
  </table>
  </form>
</body>
</html>

E.g.
147xy^23/24.5xy^19 = 6xy^4