Skip to main content

List Of Syntax Used In Mathematica

Mathematica for many is a complex programming language. It is the language used in many scientific, engineering, mathematical and computing fields and is clearly one of the much respected languages. And cheat sheets for any programming language is anytime a treat!


  Here's a syntax cheat sheet for mathematica programmers.

Various


nothing needed breaking lines (useful when end-of-line and/or indentation has a special meaning)
(* ... *) commenting (nestable)
< > <= >= comparison
Min / Max comparison (min / max (binary or more))
Err:510 equality / inequality (deep)
( ... ) grouping expressions
ToExpression runtime evaluation
case-sensitive tokens (case-sensitivity (keywords, variable identifiers...))
[a-zA-Z][a-zA-Z0-9]* tokens (variable identifier regexp)
CamelCase or camelCase tokens (what is the standard way for scrunching together multiple words)
= variable assignment or declaration (assignment)
:= variable assignment or declaration (assignment)
Module[{x, y = v}, ... ] variable assignment or declaration (declaration)
Block[{x, y = v}, ... ] variable assignment or declaration (declaration)
With[{c1 = v1, c2 = v2, ... }, ...] variable assignment or declaration (declaration)


Functions


Function[{a, b}, ....](2) anonymous function
f[a,b,...] function call
f@a(3) function call
a // f(3) function call
a ~ f ~ b(4) function call
f[] function call (with no parameter)
Composition function composition
f[para1_, para2_, ...] := ... para1 ... function definition
f := ... ## & function definition (variable number of arguments)
f[params___] := ... params ... function definition (variable number of arguments)
Return function return value (breaks the control flow)
no syntax needed(5) function return value (function body is the result)
Identity identity function


Control Flow


Continue / Break breaking control flow (continue / break)
Goto breaking control flow (goto (unconditional jump))
Return breaking control flow (returning a value)
Catch exception (catching)
Throw exception (throwing)
If[c, ...] if_then
If[c, b1, b2] if_then_else
Which[c, b1, c2, b2, True, b3] if_then_else
While[...; c] loop (do something until condition)
for loop (for "a la C" (while + initialisation))
Do[..., {i, 10, 1, -1}] loop (for each value in a numeric range, 1 decrement)
Do[..., {i, 1, 10}](6) loop (for each value in a numeric range, 1 increment (see also the entries about ranges))
Do[..., {i, 1, 10, 2}] loop (for each value in a numeric range, free increment)
While[c, ...] loop (while condition do something)
Switch[val, v1, ..., v2, ..., _, ...] multiple selection (switch)
; sequence


Types


_t(7) annotation (or variable declaration)


Package, Module


Begin["p`"] ... End[] declare
BeginPackage["p`"] ... EndPackage[] declare
<< p` import (everything into current namespace)
Get["p`"] import (everything into current namespace)
Needs["p`"] import (everything into current namespace)
` package scope


Strings


StringTake[s, {n}] accessing n-th character
FromCharacterCode ascii to character
ToCharacterCode character to ascii
ToString convert something to a string (see also string interpolation)
StringTake[s, {n, m}] extract a substring
StringPosition locate a substring
all strings allow multi-line strings multi-line
Print simple print (on any objects)
StringJoin string concatenation
Err:510 string equality & inequality
StringLength string size
\n strings (end-of-line (without writing the real CR or LF character))
... strings (with no interpolation of variables)
ToUpperCase / ToLowerCase uppercase / lowercase / capitalized string


Booleans


False false value
! logical not
|| / && logical or / and (short circuit)
True true value


Bags and Lists


Transpose 2 lists from a list of couples
Insert adding an element at index (return the new list (no side-effect))
Prepend adding an element at the beginning (list cons) (return the new list (no side-effect))
PrependTo adding an element at the beginning (list cons) (side-effect)
Append adding an element at the end (return the new list (no side-effect))
AppendTo adding an element at the end (side-effect)
Rest all but the first element
Most all but the last element
Fold f(... f(f(init, e1), e2) ..., en)
Position find an element
First(8) first element
Scan for each element do something
MemberQ is an element in the list
MapIndexed iterate with index
StringJoin @@ Riffle[l, s] join a list of strings in a string using a glue string
Select / Case keep elements (matching)
Last last element
Join list concatenation
{ a, b, c }(9) list constructor
Flatten list flattening (recursive)
Transpose list of couples from 2 lists
Length list size
a[[i]] list/array indexing
/. lookup an element in a association list
Union remove duplicates
Reverse reverse
Min / Max smallest / biggest element
Sort sort
Split split a list (into a list of lists of same value)
Split split a list (into sublists based on a predicate)
Map transform a list (or bag) in another one
/@ transform a list (or bag) in another one
List type name


Various Data Types


f @@ t computable tuple (these are a kind of immutable lists playing a special role in parameter passing) (using a tuple for a function call)
Null(10) optional value (null value)
Range range (inclusive .. inclusive)


Mathematics


+ / - / * or nothing / / addition / subtraction / multiplication / division
BitAnd / BitOr / BitXor bitwise operators (and / or / xor)
BitNot bitwise operators (bitwise inversion)
^ exponentiation (power)
Log[10, val] logarithm (base 10)
Log[2, val] logarithm (base 2)
Log logarithm (base e)
Mod modulo (modulo of -3 / 2 is 1)
-0 negation
1000., 1*^3, 1000` numbers syntax (floating point)
2^^1, 8^^7, 16^^f numbers syntax (integers in base 2, octal and hexadecimal)
1000 numbers syntax (integers)
mathematical operator priorities and associativities (addition vs multiplication)
mathematical operator priorities and associativities (exponentiation vs negation (is -3^2 equal to 9 or -9))
Random, RandomReal, RandomInteger random (random number)
SeedRandom random (seed the pseudo random generator)
Sqrt / Exp / Abs square root / e-exponential / absolute value
Sin / Cos / Tan trigonometry (basic)
ArcSin / ArcCos / ArcTan trigonometry (inverse)
IntegerPart / Round / Floor / Ceiling truncate / round / floor / ceil
Real, Rational type name (floating point)
Integer type name (integers)

 

Comments

Popular posts from this blog

Run command

"Use Run Command make life easy" Sometime we use many run command in our windows base computer. Run command is very useful for easy work. There are some "run command",

Cloud computing

  Cloud computing is a technology model that enables access to a shared pool of computing resources and services over the Internet. Instead of owning and maintaining physical servers and data centers, organizations can use cloud computing services provided by cloud service providers. These services include computing power, storage, databases, networking, software, and more. Cloud computing services can be categorized into several main models: Infrastructure as a Service (IaaS): IaaS provides virtualized computing resources over the Internet. Users can rent virtual machines, storage, and networking components, allowing them to run and manage their operating systems and applications. Platform as a Service (PaaS): PaaS offers a platform that includes the underlying infrastructure, development tools, and services to build, deploy, and manage applications. Users focus on coding and application development while the platform handles the underlying infrastructure. Software as a Service ...

AWS Free Tier

The AWS Free Tier is designed to give you hands-on experience with a range of Amazon Web Services (AWS) products and services without charging you for usage up to a specific limit. This tier primarily benefits new AWS customers, allowing them to try different AWS services and gain practical experience before committing to more extensive usage. The Free Tier includes offers that are available for 12 months following your AWS sign-up date, as well as offers that are always free. Here are the main components of the AWS Free Tier: 12-Months Free: These offers are available to new AWS customers and are valid for 12 months following your AWS sign-up date. After the 12-month free usage term, you pay standard, pay-as-you-go service rates. Always Free: These offers do not expire and are available to all AWS customers. They provide limited access to a range of AWS services for free forever. However, the usage limits reset monthly. Trials: Short-term trial offers start when you activate a part...