Permalink
Browse files

Merge and squash pull request #12246 from artcompiler/factoring-polyn…

…omials-deploy

Closes #12246

commit d6a5fb6
Author: Jeff Dyer <jeffdyer@acm.org>
Date:   Thu Jan 26 17:45:00 2012 -0800

    fix exercises for deployment

commit f91220c
Merge: 906de7f 790708d
Author: Jeff Dyer <jeffdyer@acm.org>
Date:   Wed Jan 18 16:48:34 2012 -0800

    Merge remote branch 'upstream/master' into polynomials

commit 906de7f
Author: Jeff Dyer <jeffdyer@acm.org>
Date:   Wed Jan 18 14:49:52 2012 -0800

    add basic polynomial intuition exercise

commit b5c2b40
Author: Jeff Dyer <jeffdyer@acm.org>
Date:   Wed Jan 18 11:29:06 2012 -0800

    add difference of squares and hints to polynomial intuition 1

commit 3def0b6
Author: Jeff Dyer <jeffdyer@acm.org>
Date:   Tue Jan 17 15:00:54 2012 -0800

    fix text of polynomial intuition

commit 767d7c4
Author: Jeff Dyer <jeffdyer@acm.org>
Date:   Tue Jan 17 14:58:02 2012 -0800

    add exercise on polynomial intuition

commit 0415132
Author: Jeff Dyer <jeffdyer@acm.org>
Date:   Fri Jan 13 17:41:04 2012 -0800

    cleanup

commit efbc39b
Author: Jeff Dyer <jeffdyer@acm.org>
Date:   Fri Jan 13 14:41:56 2012 -0800

    turn asserts off, fix a bug, add exer to challenge

commit 359ed1f
Author: Jeff Dyer <jeffdyer@acm.org>
Date:   Fri Jan 13 14:35:14 2012 -0800

    add exercises on factoring diff of two squares

commit 0f79058
Author: Jeff Dyer <jeffdyer@acm.org>
Date:   Thu Jan 12 14:41:34 2012 -0800

    add summative exercise for polynomial factoring

commit 13c0194
Author: Jeff Dyer <jeffdyer@acm.org>
Date:   Thu Jan 12 14:14:19 2012 -0800

    add two more exercises for factoring special polynomials

commit cbfaf28
Author: Jeff Dyer <jeffdyer@acm.org>
Date:   Wed Jan 11 14:40:57 2012 -0800

    tweak spacing

commit 68290a1
Author: Jeff Dyer <jeffdyer@acm.org>
Date:   Wed Jan 11 14:32:25 2012 -0800

    add exercises for solving polynomials by factoring

commit 32b64ee
Author: Jeff Dyer <jeffdyer@acm.org>
Date:   Wed Jan 11 11:09:07 2012 -0800

    add factoring polynomials by grouping with A!=1

commit 21bebcc
Author: Jeff Dyer <jeffdyer@acm.org>
Date:   Mon Jan 9 16:06:58 2012 -0800

    remove factoring_trinomials_1.html, will add back later

commit d53406f
Author: Jeff Dyer <jeffdyer@acm.org>
Date:   Mon Jan 9 16:05:46 2012 -0800

    remove polynomial-model.js

commit 2859b6d
Author: Jeff Dyer <jeffdyer@acm.org>
Date:   Mon Jan 9 16:04:06 2012 -0800

    more cleanup; add ast spec pdf

commit 0fa0fab
Author: Jeff Dyer <jeffdyer@acm.org>
Date:   Mon Jan 9 15:49:50 2012 -0800

    remove on exercise and add another

commit e3edafd
Author: Jeff Dyer <jeffdyer@acm.org>
Date:   Mon Jan 9 15:46:17 2012 -0800

    add exercise on factoring polynomials by a monomial

commit 9c1f42c
Author: Jeff Dyer <jeffdyer@acm.org>
Date:   Sun Jan 8 11:50:51 2012 -0800

    remove NodeClass from ast.js

commit 31d2b6f
Author: Jeff Dyer <jeffdyer@acm.org>
Date:   Sun Jan 8 11:46:31 2012 -0800

    refactor ast, adding math-model.js

commit 83f5be4
Author: Jeff Dyer <jeffdyer@acm.org>
Date:   Fri Jan 6 14:38:44 2012 -0800

    first crack at implementing the ast spec

commit fcc6888
Author: Jeff Dyer <jeffdyer@acm.org>
Date:   Wed Jan 4 14:03:34 2012 -0800

    add mh as author

commit 1aa85bc
Author: Jeff Dyer <jeffdyer@acm.org>
Date:   Wed Jan 4 13:58:11 2012 -0800

    small fixes to the ast spec

commit 4ecccfd
Author: Jeff Dyer <jeffdyer@acm.org>
Date:   Wed Jan 4 13:57:00 2012 -0800

    rename ast spec

commit e85205b
Author: Jeff Dyer <jeffdyer@acm.org>
Date:   Wed Jan 4 13:56:39 2012 -0800

    rename ast spec

commit 9520eea
Author: Jeff Dyer <jeffdyer@acm.org>
Date:   Wed Jan 4 10:54:26 2012 -0800

    ast spec rev 2

commit b7ac719
Author: Jeff Dyer <jeffdyer@acm.org>
Date:   Tue Jan 3 14:09:53 2012 -0800

    add ast spec

commit 00c5760
Author: Jeff Dyer <jeffdyer@acm.org>
Date:   Wed Dec 14 14:51:53 2011 -0800

    fixes for marcia's feedback of 12/13/2011

commit e32be5f
Author: Jeff Dyer <jeffdyer@acm.org>
Date:   Mon Dec 12 10:00:52 2011 -0800

    simplify polynomial model

commit 01ae8e3
Author: Jeff Dyer <jeffdyer@acm.org>
Date:   Fri Dec 9 14:50:38 2011 -0800

    new exercise: factoring_trinomials_1.html
  • Loading branch information...
1 parent b0f3703 commit 8cee3be1c28941ed5dcac8529f8d0484c4ed73dd @jeffdyer jeffdyer committed with beneater Jan 27, 2012
Binary file not shown.
@@ -0,0 +1,159 @@
+\documentclass{article}
+\usepackage{fullpage}
+\usepackage{geometry}
+\usepackage{fancyhdr}
+\usepackage{amsmath, amsthm, amssymb}
+\usepackage{graphicx}
+\usepackage{hyperref}
+\usepackage{parskip}
+\newcommand{\var}[1]{\emph{#1}}
+
+\title{Abstract Syntax Trees\\ for Khan Exercises}
+\begin{document}
+\maketitle
+\begin{verbatim}
+Authors: Jeff Dyer <jeffdyer@acm.org>
+ Mathias Hiron <mathias@khanacademy.org>
+
+History: 04-Jan-2012, second draft, reviewed by Mathias
+ 03-Jan-2012, first draft, unreviewed
+\end{verbatim}
+
+\section{Overview}
+
+In this note we define an abstract syntax tree (AST) form for encoding information used in Khan
+exercises. We also define a mapping from ASTs to LaTeX formatted strings. ASTs and LaTeX have wide
+applicability in the Khan exercise framework, and so by defining and implementing a core library
+for handling them we avoid duplication of effort and gain the benefits normally associated by
+software reuse.
+
+\section{Abstract Syntax Trees}
+
+We use trees to represent expressions, equations, and other structured data that underlies Khan
+exercises. The trees consist of nodes containing an operator string and zero or more child nodes.
+
+The nodes are encoded as JavaScript objects of the form:
+
+\begin{quote}
+\verb'{'{\bf op}{\tt :} {\em opstr}{\tt ,} {\bf args}{\tt :} {\tt [}$nd1${\tt ,}\dots $ndN${\tt ]}$\verb'}'$
+\end{quote}
+
+where $opstr$ is an \emph{operator string} and $nd1, \dots ndN$ is a sequence of child nodes.
+
+An \emph{operator string} is a string value that, along with the number of arguments, distinguishes
+one node kind from another. AST nodes get their meaning from the parsing, formatting and evaluating
+procedures that construct and interpret them, and otherwise have no special meaning within the AST
+definition and implementation.
+
+Primitive (non-object) JavaScript values are used to represent literal numbers and strings
+in the AST. Variables are encoded as nodes with the unary ``var'' operator with a literal string
+operand.
+
+We define a core set of operators here, but this set is not fixed. As new exercises require new
+operators, additional strings to be contrived to represent those operators, and interpreters can be
+defined to format and evaluate nodes with those operators.
+
+\section{Predefined Operators}
+
+The following set of operators are implemented by the AST module (see below). Additional operators
+may be added to this core set over time, and others may be added by problem specific utility
+modules.
+
+\vspace{20pt}
+
+\begin{tabular}{ | l | l | p{10cm} |}
+\hline
+{\bf Operator}& {\bf Sample} & {\bf Comment} \\ \hline
+\verb'+' & $x+y$ & addition, unary plus\\ \hline
+\verb'-' & $x-y$ & subtraction, unary minus\\ \hline
+\verb'times' & $x \times y$ & multiplication\\ \hline
+\verb'div' & $x \div y$ & division\\ \hline
+\verb'frac' & $\frac{x}{y}$ & fraction\\ \hline
+\verb'^' & $x^y$ & exponent\\ \hline
+\verb'sqrt' & $\sqrt{x}$ & unary square root\\ \hline
+\verb'sqrt' & $\sqrt[n]{x}$ & binary square root\\ \hline
+\verb'pm' & $x \pm y$ & plus or minus, unary plus or minus\\ \hline
+\verb'sin' & $\sin{\theta}$ & sine\\ \hline
+\verb'cos' & $\cos{\theta}$ & cosine\\ \hline
+\verb'tan' & $\tan{\theta}$ & tangent\\ \hline
+\verb'sec' & $\sec{\theta}$ & secant\\ \hline
+\verb'ln' & $\ln{x}$ & natural logarithm\\ \hline
+\verb'()' & $(expr)$ & parenthesis\\ \hline
+\verb'var' & $\var{x}$ & variable (unknown)\\ \hline
+\end{tabular}
+
+
+\pagebreak
+
+\section{LaTeX Translation}
+
+In addition to the internal AST form, we define a mapping from ASTs to LaTeX. The following table
+shows the mapping between the AST nodes and LaTeX.
+
+\vspace{20pt}
+
+\begin{tabular}{ | l | l |}
+\hline
+{\bf AST} & {\bf LaTeX} \\ \hline
+\verb'{op: "+", args: [1, 2]}' & \verb'1+2' \\ \hline
+\verb'{op: "-", args: [1, 2]}' & \verb'1-2' \\ \hline
+\verb'{op: "times", args: [1, 2]}' & \verb'1 \times 2' \\ \hline
+\verb'{op: "div", args: [1, 2]}' & \verb'1 \div 2' \\ \hline
+\verb'{op: "frac", args: [1, 2]}' & \verb'\frac{1}{2}' \\ \hline
+\verb'{op: "^", args: [1, 2]}' & \verb'1^2' \\ \hline
+\verb'{op: "sqrt", args: [9]}' & \verb'\sqrt{9}' \\ \hline
+\verb'{op: "sqrt", args: [3, 8]}' & \verb'\sqrt[3]{8}' \\ \hline
+\verb'{op: "pm", args: [2, 1]}' & \verb'2 \pm 1' \\ \hline
+\verb'{op: "sin", args: [0]}' & \verb'\sin{0}' \\ \hline
+\verb'{op: "cos", args: [0]}' & \verb'\cos{0}' \\ \hline
+\verb'{op: "tan", args: [0]}' & \verb'\tan{0}' \\ \hline
+\verb'{op: "sec", args: [0]}' & \verb'\sec{0}' \\ \hline
+\verb'{op: "ln", args: [1]}' & \verb'\ln{1}' \\ \hline
+\verb'{op: "()", args: [10]}' & \verb'(10)' \\ \hline
+\verb'{op: "var", args: ["x"]}' & \verb'\var{x}' \\ \hline
+\end{tabular}
+
+
+\vspace{20pt}
+
+The AST library provides functions for translating between ASTs and LaTeX formatted strings.
+These formatting and parsing algorithms conform to common practice including:
+
+\begin{itemize}
+\item Insert \verb'\left' and \verb'\right' to auto size brackets.
+\item Rewrite addition of a negative number as subtraction.
+\item Elide \verb'\times' when it occurs before a non-numeric symbol during formatting, and construct
+ a \verb'\times' node when a non-numeric symbol occurs after a non-operator symbol.
+\item Insert parenthesis when formatting a binary expression of lower precedence that occurs in an
+ expression of higher precedence.
+\item (more here)
+\end{itemize}
+
+\pagebreak
+\section{AST API}
+
+%We provide an API for constructing, formatting, evaluating, and interning ASTs. Some of these
+%functions take a optional \emph{model} object which interprets the meaning of operators that are not
+%defined by the AST library, or which override those that are. When the \emph{model} parameter is
+%given, the AST function delegates the call to that object, which may call back to the AST function
+%to handle operators it does not distinguish.
+
+The AST API is implemented as a module that can be loaded into an exercise. All functions are
+instance methods of the {\bf ast} property added to {\bf KhanUtil}, and therefore are called through
+that property (e.g. {\bf KhanUtil.ast.fromLaTeX}(``1+2''), or {\bf ast.fromLaTeX}(``1+2'') when
+called from exercise HTML that loads the AST module).
+
+Below is the list of API methods implemented by {\tt ast.js}.
+
+\begin{tabular}{ | l | p{10cm} |}
+\hline
+{\bf Name} & {\bf Description} \\ \hline
+{\bf fromLaTeX}(\emph{str}) & Return an AST node constructed from a LaTeX formatted string \\ \hline
+{\bf toLaTeX}(\emph{node}) & Return the LaTeX formatted string correponding to an AST node \\ \hline
+{\bf eval}(\emph{node}) & Return the result of evaluating an AST \\ \hline
+{\bf intern}(\emph{node}) & Add an AST node and its child nodes to the node pool and return the root node id (nid) \\ \hline
+{\bf node}(\emph{nid}) & Reconstitute an AST from its node id \\ \hline
+\end{tabular}
+
+
+\end{document}
@@ -0,0 +1,77 @@
+<!DOCTYPE html>
+<html data-require="math graphie ast math-model">
+ <head>
+ <meta charset="UTF-8" />
+ <title>Factoring Difference of Two Squares, Polynomials with A = 1</title>
+ <script src="../khan-exercise.js"></script>
+ </head>
+ <body>
+ <div class="exercise">
+ <div class="problems">
+ <div>
+ <div class="vars">
+ <var id="MATH">MathModel.init()</var>
+ <var id="b">randRange(2, 9)</var>
+ <var id="A">1</var>
+ <var id="B">0</var>
+ <var id="C">-b*b</var>
+ <var id="PROBLEM">MATH.polynomial([A, B, C], "x")</var>
+ <!-- use parse so that the ast matches the parsed user input -->
+ <var id="SOLUTION">MATH.parse("(x+"+b+")(x-"+b+")")</var>
+ </div>
+ <div>
+ <p class="question">
+ Factor the following expression:
+ </p>
+ <p>
+ <code><var>MATH.format(PROBLEM, "large")</var></code>
+ </p>
+ </div>
+
+ <div class="solution" data-type="custom">
+ <div class="instruction">
+ Enter the factored expression here:<br>
+ <input name="response" type="text" onblur="window._guess=value"></input>
+ </div>
+ <div class="guess">jQuery( "div.instruction input" ).val()</div>
+ <div class="validator-function">
+ return MATH.isEqual(MATH.parse(guess), SOLUTION)
+ </div>
+ <div class="show-guess">
+ </div>
+ <div class="show-guess-solutionarea">
+ jQuery( "div.instruction input" ).val( guess )
+ </div>
+ <div class="example">(x+2)(x-2)</div>
+ </div>
+
+ <div class="hints">
+ <p>
+ Recognize that the expression is of the form
+ <code><var>MATH.format("a^2-b^2", "normalsize", KhanUtil.BLUE)</var></code>
+ , which can be factored as
+ <code><var>MATH.format("(a+b)(a-b)", "normalsize", KhanUtil.BLUE)</var></code>.
+ </p>
+ <p>
+ First, determine the values of
+ <code><var>MATH.format("a", "normalsize", KhanUtil.BLUE)</var></code> and
+ <code><var>MATH.format("b", "normalsize", KhanUtil.BLUE)</var></code>.
+ </p>
+ <p>
+ <code><var>MATH.format("a="+"x", "normalsize", KhanUtil.BLUE)</var></code><br><br>
+ <code><var>MATH.format("b=\\sqrt{"+b*b+"}="+b, "normalsize", KhanUtil.BLUE)</var></code>
+ </p>
+ <p>
+ Replace
+ <code><var>MATH.format("a", "normalsize", KhanUtil.BLUE)</var></code> and
+ <code><var>MATH.format("b", "normalsize", KhanUtil.BLUE)</var></code> in the factored equation to find the answer.
+ </p>
+ <p>
+ <code><var>MATH.format("(x+"+b+")(x-"+b+")", "large", KhanUtil.ORANGE)</var></code>
+ </p>
+ </div>
+ </div>
+ </div>
+ </div>
+ </body>
+</html>
@@ -0,0 +1,78 @@
+<!DOCTYPE html>
+<html data-require="math graphie ast math-model">
+ <head>
+ <meta charset="UTF-8" />
+ <title>Factoring Difference of Two Squares, Polynomials with A != 1</title>
+ <script src="../khan-exercise.js"></script>
+ </head>
+ <body>
+ <div class="exercise">
+ <div class="problems">
+ <div>
+ <div class="vars">
+ <var id="MATH">MathModel.init()</var>
+ <var id="a">randRange(2, 9)</var>
+ <var id="b">randRange(1, 9)</var>
+ <var id="A">a*a</var>
+ <var id="B">0</var>
+ <var id="C">-b*b</var>
+ <var id="PROBLEM">MATH.polynomial([A, B, C], "x")</var>
+ <!-- use parse so that the ast matches the parsed user input -->
+ <var id="SOLUTION">MATH.parse("("+a+"x+"+b+")("+a+"x-"+b+")")</var>
+ </div>
+ <div>
+ <p class="question">
+ Factor the following expression:
+ </p>
+ <p>
+ <code><var>MATH.format(PROBLEM, "large")</var></code>
+ </p>
+ </div>
+
+ <div class="solution" data-type="custom">
+ <div class="instruction">
+ Enter the factored expression here:<br>
+ <input name="response" type="text" onblur="window._guess=value"></input>
+ </div>
+ <div class="guess">jQuery( "div.instruction input" ).val()</div>
+ <div class="validator-function">
+ return MATH.isEqual(MATH.parse(guess), SOLUTION)
+ </div>
+ <div class="show-guess">
+ </div>
+ <div class="show-guess-solutionarea">
+ jQuery( "div.instruction input" ).val( guess )
+ </div>
+ <div class="example">(3x+2)(3x-2)</div>
+ </div>
+
+ <div class="hints">
+ <p>
+ Recognize that the expression is of the form
+ <code><var>MATH.format("a^2-b^2", "normalsize", KhanUtil.BLUE)</var></code>
+ , which can be factored as
+ <code><var>MATH.format("(a+b)(a-b)", "normalsize", KhanUtil.BLUE)</var></code>.
+ </p>
+ <p>
+ First, determine the values of
+ <code><var>MATH.format("a", "normalsize", KhanUtil.BLUE)</var></code> and
+ <code><var>MATH.format("b", "normalsize", KhanUtil.BLUE)</var></code>.
+ </p>
+ <p>
+ <code><var>MATH.format("a=\\sqrt{"+a*a+"x^2}="+a+"x", "normalsize", KhanUtil.BLUE)</var></code><br><br>
+ <code><var>MATH.format("b=\\sqrt{"+b*b+"}="+b, "normalsize", KhanUtil.BLUE)</var></code>
+ </p>
+ <p>
+ Replace
+ <code><var>MATH.format("a", "normalsize", KhanUtil.BLUE)</var></code> and
+ <code><var>MATH.format("b", "normalsize", KhanUtil.BLUE)</var></code> in the factored equation to find the answer.
+ </p>
+ <p>
+ <code><var>MATH.format("("+a+"x+"+b+")("+a+"x-"+b+")", "large", KhanUtil.ORANGE)</var></code>
+ </p>
+ </div>
+ </div>
+ </div>
+ </div>
+ </body>
+</html>
Oops, something went wrong.

0 comments on commit 8cee3be

Please sign in to comment.