Search
Expressions
Expressions (the Wolfram Language for Students - Personal Use Only : www.wolfram.com)

What exactly are expressions?

Expressions are built by combining more basic expressions into more complicated expressions

e.g.

Expressions_1.png

Expressions_2.png

can be thought of as being made up of expressions for a, b, and +

So a complicated expression can be broken down into simpler expressions

At a certain point, we obtain simple, irreducible expressions that cannot be broken down anymore

These irreducible expressions are called atoms

There are three main kinds of atoms:

Numbers

Strings

Symbols

We have seen many examples of numbers: these can be integers or inexact (floating point) numbers

Strings are more-or-less self-explanatory:

Expressions_3.png

Expressions_4.png

Symbols are what distinguish Mathematica from other kinds of languages

In the example above, a and b are symbols

The + sign is also a notation for the Plus symbol

We can decompose an expression into its atoms using FullForm

Expressions_5.png

Expressions_6.png

Variables and expressions

Here we begin to see what makes Mathematica different

Unlike in Julia, variables may be used before they are assigned a value

Mathematica will manipulate the expression with variables held in an unevaluated form

Expressions_8.gif

Expressions_9.png

Expressions_10.png

Mathematical expressions

Using variables in this way lets us manipulate mathematical objects such as equations and polynomials

Note: using some shortcuts, you can use “fancy” notation in input cells (e.g. superscripts for powers, fractions for division, etc.)

Expressions_11.png

Expressions_12.png

Expressions_13.png

Expressions_14.png

Creating and assigning variables

Variables are assigned using the equal sign = (Set)

lhs = rhs

will evaluate the expression ‘rhs’ and assign the result to ‘lhs’. The right-hand side can be any Mathematica expression

Expressions_15.png

Expressions_16.png

Expressions_17.png

Expressions_18.png

Expressions_19.png

Expressions_20.png

Variables can be unset using Clear

Expressions_21.png

Expressions_22.png

Expressions_23.png

Brackets

There are four kinds of bracketing in Mathematica:

(Parentheses) for grouping mathematical expressions

Expressions_24.png

Expressions_25.png

Expressions_26.png

Expressions_27.png

[Square brackets] for functions calls

Expressions_28.png

Expressions_29.png

{Braces} for lists

Expressions_30.png

Expressions_31.png

[[Double brackets]] for indexing

Expressions_32.png

Expressions_33.png

Algebraic manipulation and simplification

This is one of the most powerful features of Mathematica

Expressions_34.png

Expressions_35.png

Expressions_36.png

Expressions_37.png

In particular, Simplify is one of the most useful single functions

Expressions_38.png

Expressions_39.png

Sometimes FullSimplify can simplify more complicated expressions (using an expanded set of rules)

Expressions_40.png

Expressions_41.png

Expressions_42.png

Expressions_43.png

Expressions_44.png

Expressions_45.png

Expressions_46.png

Expressions_47.png

The percent symbol can be used as a shortcut for the output of the last expression

Expressions_48.png

Expressions_49.png

Expressions_50.png

Expressions_51.png

Expressions_52.png

Expressions_53.png

Substitution rules

Given an expression such as:

Expressions_54.png

Expressions_55.png

We can evaluate this expression with the variables replaced by numbers (or any other expressions) using substitution rules

The replacement operator is typed slash-dot, and rules are typed ->

Expressions_56.png

Expressions_57.png

Multiple substitutions can be made at once using lists of rules

Expressions_58.png

Expressions_59.png

More algebraic manipulations

Simplify tends not to make assumptions about the variables

Expressions_60.png

Expressions_61.png

Assumptions can be provided as a second argument to Simplify:

Expressions_62.png

Expressions_63.png

Algebraic expressions can be analyzed programmatically:

Expressions_64.png

Expressions_65.png

Expressions_66.png

Expressions_67.png

Expressions_68.png

Expressions_69.png

Expressions_70.png

Expressions_71.png

Expressions_72.png

Expressions_73.png

Expressions_74.png

Expressions_75.png

Expressions_76.png

Expressions_77.png

Expressions_78.png

Example: binomial theorem

Recall the binomial theorem:

Expressions_79.png

Mathematica immediately simplifies using this identity:

Expressions_80.png

Expressions_81.png

We can evaluate for a specific n using a replacement rule

Expressions_82.png

Expressions_83.png

Expressions_84.png

Expressions_85.png

Equations

Just like in Julia, equality is tested with ==

Expressions_86.png

Expressions_87.png

Expressions_88.png

Expressions_89.png

Expressions_90.png

Expressions_91.png

Expressions_92.png

Equations are themselves Mathematica expressions

Expressions_93.png

Expressions_94.png

Equations can be solved using Solve

Expressions_95.png

Expressions_96.png

Expressions_97.png

Expressions_98.png

Expressions_99.png

Expressions_100.png

NSolve (for “numeric solve”) will look for numeric rather than symbolic solutions

Expressions_101.png

Expressions_102.png

For linear, quadratic, cubic, and quartic polynomials, Mathematica will always give a closed-form solution

Expressions_103.png

Expressions_104.png

Some equations do not admit closed-form solutions

Expressions_105.png

Expressions_106.png

Expressions_107.png

Expressions_108.png

You can also solve for simultaneous systems of equations

Expressions_109.png

Expressions_110.png

Set and SetDelayed

Up until now, we have assigned expressions to variables using =, which is called Set

Whenever we write lhs = rhs, the rhs is evaluated immediately

Sometimes we don’t want to evaluate the rhs immediately. Instead, we want to wait until we encounter lhs somewhere else in our program. When we encounter lhs, it is replaced by rhs and only then evaluated

This is denoted SetDelayed, and is written :=

For example, we will use Set for x, and SetDelayed for y

Expressions_111.gif

Expressions_112.png

Expressions_113.png

Expressions_114.png

Expressions_115.png

Expressions_116.png

Expressions_117.png

Expressions_118.png

Expressions_119.png

Expressions_120.png

Expressions_121.png

Expressions_122.png

Expressions_123.png

Expressions_124.png