Skip to content

Commit

Permalink
style: Update comments and text
Browse files Browse the repository at this point in the history
  • Loading branch information
ntrang086 committed Apr 21, 2018
1 parent fe8211c commit 657cac4
Showing 1 changed file with 22 additions and 24 deletions.
46 changes: 22 additions & 24 deletions Sympy_Intro.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,16 @@
"editable": true
},
"source": [
"# Warmup - Introduction to SymPy\n",
"This lab exercise uses the [SymPy](http://www.sympy.org/en/index.html) symbolic math library to model constraints in the problem. To do that, we will use symbols (`sympy.Symbol`), functions (`sympy.Function`), and expressions (`sympy.Expr`) from sympy, and then we'll combine the function and expression classes to make constraints -- evaluatable symbolic functions.\n",
"# Introduction to SymPy\n",
"This project uses the [SymPy](http://www.sympy.org/en/index.html) symbolic math library to model constraints in a problem. To do that, we will use symbols (`sympy.Symbol`), functions (`sympy.Function`), and expressions (`sympy.Expr`) from sympy, and then we'll combine the function and expression classes to make constraints -- evaluatable symbolic functions.\n",
"\n",
"In this warmup, you will be introduced to the syntax and functionality of SymPy:\n",
"We will look at examples to understand the syntax and functionality of SymPy:\n",
"- [Example 1](#Example-1:-Symbols): Creating [symbols](http://docs.sympy.org/dev/modules/core.html#module-sympy.core.symbol)\n",
"- [Example 2](#Example-2:-Expressions): Creating & manipulating [expressions](http://docs.sympy.org/dev/modules/core.html#id16) with [arithmetic & logical operators](http://docs.sympy.org/dev/modules/core.html#sympy-core)\n",
"- [Example 3](#Example-3:-Symbolic-substitution-and-expression-evaluation): Symbolic [substitution & evaluation](http://docs.sympy.org/dev/modules/core.html#subs)\n",
"- [Exercises](#SymPy-Exercises): Creating & manipulating constraints & [functions](http://docs.sympy.org/dev/modules/functions/index.html)\n",
"\n",
"(See a list of common \"gotchas\" for sympy in their documentation: http://docs.sympy.org/dev/gotchas.html)\n",
"\n",
"Start by reading and running the example cells, then complete the steps in the warmup cell."
"(See a list of common \"gotchas\" for sympy in their documentation: http://docs.sympy.org/dev/gotchas.html)"
]
},
{
Expand Down Expand Up @@ -48,7 +46,7 @@
},
"source": [
"## Example 1: Symbols\n",
"**Sympy provides the `Symbol` class to create symbolic variables. Create individual symbols by calling the constructor with a symbol name.** (Tip: Use the `display()` function to pretty-print symbolic terms.)"
"**Sympy provides the `Symbol` class to create symbolic variables. Create individual symbols by calling the constructor with a symbol name.**"
]
},
{
Expand Down Expand Up @@ -86,7 +84,7 @@
"editable": true
},
"source": [
"**You can also create symbols from an iterable sequence using the `symbols()` function.**"
"**We can also create symbols from an iterable sequence using the `symbols()` function.**"
]
},
{
Expand Down Expand Up @@ -164,8 +162,8 @@
"source": [
"## Example 2: Expressions\n",
"\n",
"**A symbol is the most basic expression.** (Tip: Jupyter notebooks show information about objects\n",
"using the `?` magic function)"
"**A symbol is the most basic expression.** Jupyter notebooks show information about objects\n",
"using the `?` magic function,"
]
},
{
Expand Down Expand Up @@ -204,7 +202,7 @@
"editable": true
},
"source": [
"**You can also define expressions with relations between symbols.** (However, notice that expressions have no _names_...)"
"**We can also define expressions with relations between symbols.**"
]
},
{
Expand Down Expand Up @@ -273,7 +271,7 @@
"source": [
"x, y = symbols(\"x y\")\n",
"y = x # now y references the same symbolic object as x\n",
"display(y) # display(y) == x ??!"
"display(y)"
]
},
{
Expand All @@ -283,7 +281,7 @@
"editable": true
},
"source": [
"**Use `sympy.Eq` for symbolic equality expressions:** (Tip: there are lots of expressions in the [sympy docs](http://docs.sympy.org/dev/modules/core.html#sympy-core))"
"**Use `sympy.Eq` for symbolic equality expressions:** There are lots of expressions in the [sympy docs](http://docs.sympy.org/dev/modules/core.html#sympy-core)."
]
},
{
Expand Down Expand Up @@ -401,7 +399,7 @@
"editable": true
},
"source": [
"**Symbolic variables can be replaced by other variables, or by concrete values.** (Tip: use positional arguments in the `subs()` method to replace one symbol)"
"**Symbolic variables can be replaced by other variables, or by concrete values.** "
]
},
{
Expand Down Expand Up @@ -438,7 +436,7 @@
"editable": true
},
"source": [
"**But keep in mind that substitution returns a _copy_ of the expression -- it doesn't operate in-place.** (Tip: as a result, you can use substitution on one expression bound to generic variables to generate new instances bound to specific variables.)\n",
"**But substitution returns a _copy_ of the expression -- it doesn't operate in-place.** As a result, we can use substitution on one expression bound to generic variables to generate new instances bound to specific variables.\n",
"\n",
"Look at what happens when we bind new variables to our equality relation:"
]
Expand Down Expand Up @@ -486,7 +484,7 @@
"editable": true
},
"source": [
"**Symbol substitution returns an expression.** (Recall that Symbols _are_ expressions)."
"**Symbol substitution returns an expression.**"
]
},
{
Expand Down Expand Up @@ -519,7 +517,7 @@
"editable": true
},
"source": [
"**But substituting values for all symbols returns a value type.** (Tip: You can substitute multiple symbols in the `subs()` command by providing a mapping (dict) from current symbols to new symbols or values.)"
"**But substituting values for all symbols returns a value type.** We can substitute multiple symbols in the `subs()` command by providing a mapping (dict) from current symbols to new symbols or values."
]
},
{
Expand Down Expand Up @@ -554,7 +552,7 @@
"source": [
"## Example 4: Constraints\n",
"\n",
"**Constraints are a construct of this lab exercise (not part of sympy) that combine symbolic Functions with Expressions for evaluation. The `constraint()` function (defined in the `util` module) takes a name and an expression and returns a \"named expression\" -- a constraint.**"
"**Constraints are a construct of this project (not part of sympy) that combine symbolic Functions with Expressions for evaluation. The `constraint()` function (defined in the `util` module) takes a name and an expression and returns a \"named expression\" -- a constraint.**"
]
},
{
Expand Down Expand Up @@ -690,7 +688,7 @@
},
"source": [
"## SymPy Exercises\n",
"Complete the following exercises to check your understanding of sympy symbols, expressions, and constraints:"
"The following exercises check our understanding of sympy symbols, expressions, and constraints:"
]
},
{
Expand Down Expand Up @@ -723,7 +721,7 @@
"source": [
"A = symbols(\"A:3\")\n",
"\n",
"# test for completion\n",
"# Test for completion\n",
"assert(len(A) == 3)\n",
"assert(all([type(v) == Symbol for v in A]))\n",
"print(\"All tests passed!\")"
Expand Down Expand Up @@ -759,7 +757,7 @@
"source": [
"E = Xor(\"a\", \"b\")\n",
"\n",
"# test for completion\n",
"# Test for completion\n",
"_vars = E.free_symbols\n",
"assert(len(_vars) == 2)\n",
"xor_table = {(0, 0): False, (0, 1): True, (1, 0): True, (1, 1): False}\n",
Expand Down Expand Up @@ -799,7 +797,7 @@
"maxAbsDiff = constraint(\"MaxAbsDiff\", LessThan(abs(a - b), c))\n",
"maxAbsDiff_copy = maxAbsDiff.subs({a: A[0], b: A[1], c: A[2]})\n",
"\n",
"# test for completion\n",
"# Test for completion\n",
"assert(maxAbsDiff.free_symbols != maxAbsDiff_copy.free_symbols)\n",
"assert(len(maxAbsDiff_copy.free_symbols) == len(maxAbsDiff_copy.args))\n",
"inputs = {(0, 6, 7): True, (6, 0, 7): True, (7, 6, 0): False}\n",
Expand All @@ -814,7 +812,7 @@
"editable": true
},
"source": [
"**(Optional) Question 4:** Create a constraint AllDiff accepting the symbols in A as arguments, returning True if they are all different, and False if any pair is the same"
"**Question 4:** Create a constraint AllDiff accepting the symbols in A as arguments, returning True if they are all different, and False if any pair is the same"
]
},
{
Expand Down Expand Up @@ -867,7 +865,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.0"
"version": "3.6.3"
}
},
"nbformat": 4,
Expand Down

0 comments on commit 657cac4

Please sign in to comment.