Community RSS Feed
http://community.wolfram.com
RSS Feed for Wolfram Community showing any discussions in tag Mathematica sorted by activeThe shortest distance from a given point to a given line
http://community.wolfram.com/groups/-/m/t/1127928
Is there a function in Mathematica that gives me the length of the shortest distance from a given point to a given line?Laurens Wachters2017-06-25T06:42:04ZHow to Postprocess a ContourPlot to find minimum and maximum values?
http://community.wolfram.com/groups/-/m/t/1126384
I have a complicated function that is time consuming to evaluate, and has lots of local minima and maxima
cp = ContourPlot[x y Sin[30 x] Sin[20 y], {x, 0, 1}, {y, 0, 1}]
Is there a simple way to find the coordinates and function value of the maximum and minimum of the function, as found by Mathematica in the process of generating the contour plot?
I looked at the FullForm of cp, but it is a long and cryptic GraphicsComplex.James D Hanson2017-06-23T17:53:53ZBuilding a simple Mathematica code for Tensorial/Vectorial Calculus
http://community.wolfram.com/groups/-/m/t/1127218
There are a lot of Mathematica packages for vector/tensor calculus: [Tensorial][1], [Advanced Tensor Analysis][2], [Ricci][3], [TensoriaCalc][4], [grt][5], [xAct][6] are just a few mentions.
The main thing I don't like about these packages is that the majority of them (if not all) are not up to date with the current Mathematica version. Some of them haven't seen an update in decades. The other major drawback of these packages is the cumbersome notation, declaration and the fact that almost all are to be used with General Relativity in mind and use coordinate notation. The results are not really coordinate-free.
With this in mind, I have developed a "Package" that can deal with symbolic vectors/tensors in a coordinate-free form and the package is human-readable (some packages implementations in this matter are simply indecipherable).
In the first part, I'll present the code with simple explanations and the second part some simple examples.
These functions are needed to clear the OenOwnValues, DownValues, UpValues, SubValues. Mathematica has no built-in way of doing it.
(* Prevent evaluation *)
SetAttributes[{ClearOwnValues, ClearDownValues, ClearUpValues, ClearSubValues}, HoldFirst]
(* Always return true for commodity later *)
ClearOwnValues[var_Symbol] := (OwnValues@var = {}; True)
ClearDownValues[var_Symbol] := (DownValues@var = {}; True)
ClearUpValues[var_Symbol] := (UpValues@var = {}; True)
ClearSubValues[var_Symbol] := (SubValues@var = {}; True)
(* Delete Values of "f" if they match the input *)
ClearDownValues[expr:f_Symbol[___]] := (DownValues@f = DeleteCases[DownValues@f, _?(!FreeQ[First@#, HoldPattern@expr] &)]; True)
ClearUpValues[expr:f_Symbol[___]] := (UpValues@f = DeleteCases[UpValues@f, _?(!FreeQ[First@#, HoldPattern@expr] &)]; True)
ClearSubValues[expr:f_Symbol[___][___]] := (SubValues@f = DeleteCases[SubValues@f, _?(!FreeQ[First@#, HoldPattern@expr] &)]; True)
The Define-family functions are the main core of this package, they are used to define all kinds of relationship and can be easily expanded.
You can define a Symbol "var" or a function "fun[var, ___]" to be a certain "type". "var" cannot have OwnValues, otherwise it will be evaluated.
You can only define symbols. the function "fun" should be primarily used as script-like functions as Subscript, SuperHat, etc.
(* Prevent evaluation *)
SetAttributes[Define$Internal, HoldAll]
(*
Internal version of Define, all others versions are a call of this function.
The OwnValues and/or DownValues are cleared immediately, this step is to avoid rule-parsing.
The possible types are: Real, Imaginary, Tensor and Constant. For Tensor type additional parameters are needed: rank and dimension.
*)
Define$Internal[(var_Symbol /; ClearOwnValues@var) | (var:Except[Hold, fun_][head_Symbol /; ClearOwnValues@head, ___] /; ClearDownValues@var),
type:"Real" | "Imaginary" | "Tensor" | "Constant",
rank_Integer:2, dim_Integer:3] := Module[{tag},
(* All expressions are defined as UpValues, assign it to the corresponding tag *)
(* UpValues cannot be deeply nested, hence the need to assign it to the "tag" *)
tag = If[Head@var === Symbol, var, fun];
Which[
type === "Real", (* Typical properties needed for real quantities *)
Evaluate@tag /: Element[var, Reals] = True;
Evaluate@tag /: Re[v:var] := v;
Evaluate@tag /: Im[v:var] := 0;
Evaluate@tag /: Conjugate[v:var] := v;
Evaluate@tag /: Abs[v:var] := RealAbs@v;
,
type === "Imaginary", (* Typical properties needed for Imaginary quantities *)
Evaluate@tag /: Element[var, Reals] = False;
Evaluate@tag /: Re[v:var] = 0;
Evaluate@tag /: Im[v:var] := v/I;
Evaluate@tag /: Conjugate[v:var] := -v;
Evaluate@tag /: Abs[v:var] := RealAbs@Im@v;
,
type === "Tensor", (* For compativility with Mathematica current Tensor-functions *)
Evaluate@tag /: ArrayQ@var = rank != 0;
Evaluate@tag /: TensorQ@var = rank != 0;
Evaluate@tag /: MatrixQ@var = rank == 2;
Evaluate@tag /: VectorQ@var = rank == 1;
Evaluate@tag /: ListQ@var = rank != 0;
Evaluate@tag /: ScalarQ@var = rank == 0;
Evaluate@tag /: TensorRank@var = rank;
Evaluate@tag /: TensorDimensions@var = ConstantArray[dim, {rank}];
Evaluate@tag /: Element[var, Arrays@TensorDimensions@var] = True;
Evaluate@tag /: Element[var, Matrices@{dim, dim}] = rank == 2;
Evaluate@tag /: Element[var, Vectors@dim] = rank == 1;
,
type === "Constant", (* A constant has zero "derivative" *)
Evaluate@tag /: ConstantQ@var = True;
Evaluate@tag /: grad@var = 0;
Evaluate@tag /: div@var = 0;
Evaluate@tag /: curl@var = 0;
Evaluate@tag /: DotNabla[_, var] = 0;
Evaluate@tag /: D[var, __] = 0;
Evaluate@tag /: Dp[var, __] = 0;
Evaluate@tag /: Dt[var, ___] = 0;
Evaluate@tag /: Delta[var] = 0;
,
True, $Failed]
]
(* Assign more than one variable *)
Define$Internal[vars__ /; Length@{vars} > 1, type:"Real" | "Imaginary" | "Tensor" | "Constant", rank_Integer:2, dim_Integer:3] :=(
Define$Internal[#, type, rank, dim] & /@ Hold /@ Hold@vars // ReleaseHold;) (* Hacky-way of passing Hold down *)
Define$Internal[Hold@var_, type:"Real" | "Imaginary" | "Tensor" | "Constant", rank_Integer:2, dim_Integer:3] := Define$Internal[var, type, rank, dim]
(* Main Define functions *)
SetAttributes[{DefineReal, DefineImaginary, DefineTensor, DefineConstant}, HoldAll]
DefineReal[vars__] := Define$Internal[vars, "Real"]
DefineImaginary[vars__] := Define$Internal[vars, "Imaginary"]
DefineTensor[vars__, rank_Integer:2, dim_Integer:3] := Define$Internal[vars, "Tensor", rank, dim]
DefineConstant[vars__] := Define$Internal[vars, "Constant"]
(* Define multiple things at once *)
SetAttributes[{DefineRealTensor, DefineConstantTensor, DefineRealConstantTensor}, HoldAll]
DefineRealTensor[vars__, rank_Integer:2, dim_Integer:3] := (DefineReal@vars; DefineTensor[vars, rank, dim];)
DefineConstantTensor[vars__, rank_Integer:2, dim_Integer:3] := (DefineConstant@vars; DefineTensor[vars, rank, dim];)
DefineRealConstantTensor[vars__, rank_Integer:2, dim_Integer:3] := (DefineReal@vars; DefineConstant@vars; DefineTensor[vars, rank, dim];)
Now it is possible to define tensorial variables and make them behave as tensor with current Mathematica implementation.
Some built-in functions needed to be redefined to work with symbolic tensors. An example of this necessity is:
(* Define two tensors a and b *)
DefineTensor[a, b, 2]
TensorRank[2*a - 3*b] (* Return 2 *)
TensorQ[a] (* Return True *)
TensorQ[2*a] (* Return False *)
Mathematica function TensorQ don't know that a scalar times a tensor is a tensor. The following code is for refifinition:
Unprotect[TensorQ, VectorQ, TensorRank, Dot, Cross, TensorProduct]
(* Numbers are always scalar/constant. These functions are not built-in. *)
ScalarQ[a_?NumericQ] := True
ConstantQ[a_?NumericQ] := True
(* Complexes *)
TensorQ[(Re|Im|Conjugate)[a_]] := TensorQ@a
VectorQ[(Re|Im|Conjugate)[a_]] := VectorQ@a
ScalarQ[(Re|Im|Conjugate)[a_]] := ScalarQ@a
ConstantQ[(Re|Im|Conjugate)[a_]] := ConstantQ@a
TensorRank[(Re|Im|Conjugate)[a_]] := TensorRank@a
(* Plus *)
TensorQ[(a_?TensorQ) + (b_?TensorQ)] := TensorRank@a === TensorRank@b
VectorQ[(a_?VectorQ) + (b_?VectorQ)] := True
ScalarQ[(a_?ScalarQ) + (b_?ScalarQ)] := True
ConstantQ[(a_?ConstantQ) + (b_?ConstantQ)] := True
(* Times *)
TensorQ[(a__?ScalarQ) * (b_?TensorQ)] := True
VectorQ[(a__?ScalarQ) * (b_?VectorQ)] := True
ScalarQ[(a__?ScalarQ) * (b_?ScalarQ)] := True
ConstantQ[(a_?ConstantQ /; ScalarQ@a) * (b_?ConstantQ)] := True
(* Pass scalars out of Dot and Cross, as is done in TensorProduct *)
Dot[a___, Times[b_, s__?ScalarQ], c___] := Times[s, Dot[a, b, c]]
Cross[a_, Times[b_, s__?ScalarQ]] := Times[s, Cross[a, b]]
Cross[Times[a_, s__?ScalarQ], b_] := Times[s, Cross[a, b]]
(* Dot *)
TensorQ[(a_?TensorQ) . (b_?TensorQ)] /; TensorRank@a + TensorRank@b - 2 >= 1 := True
VectorQ[(a_?TensorQ) . (b_?TensorQ)] /; TensorRank@a + TensorRank@b - 2 == 1 := True
ScalarQ[(a_?VectorQ) . (b_?VectorQ)] := True
ConstantQ[(a_?ConstantQ /; TensorQ@a) . (b_?ConstantQ /; TensorQ@b)] := True
(* Automatically evaluate to zero, as TensorProduct *)
Dot[a___, 0, b___] := 0
(* Cross *)
TensorQ[(a_?VectorQ) \[Cross] (b_?VectorQ)] := True
VectorQ[(a_?VectorQ) \[Cross] (b_?VectorQ)] := True
ConstantQ[(a_?ConstantQ /; VectorQ@a) \[Cross] (b_?ConstantQ /; VectorQ@b)] := True
(* Perpendicular vectors automatically evalute to zero *)
Cross[a_?VectorQ, a_?VectorQ] := 0
(* Automatically evaluate to zero, as TensorProduct *)
Cross[a___, 0, b___] := 0
(* Return single argument as Dot, Times and TensorProduct *)
Cross[a_] := a
(* Tensor Product *)
TensorQ[(a_?TensorQ) \[TensorProduct] (b_?TensorQ)] := True
ConstantQ[(a_?ConstantQ /; TensorQ@a) \[TensorProduct] (b_?ConstantQ /; TensorQ@b)] := True
(* Power *)
ScalarQ@Power[a_?ScalarQ, b_?ScalarQ] := True
ScalarQ[1/a_?ScalarQ] := True
ConstantQ@Power[a_?ConstantQ /; ScalarQ@a, b_?ConstantQ /; ScalarQ@b] := True
ConstantQ[1/a_?ConstantQ /; ScalarQ@a] := True
(* grad *)
grad[_?ConstantQ] := 0
TensorQ@grad[a_?ScalarQ] := True
VectorQ@grad[a_?ScalarQ] := True
TensorQ@grad[a_?TensorQ] := True
TensorRank@grad[a_?ScalarQ] := 1
TensorRank@grad[a_?TensorQ] := TensorRank@a + 1
(* div *)
div[_?ConstantQ] := 0
TensorQ@div[a_?TensorQ /; TensorRank@a >= 2] := True
VectorQ@div[a_?TensorQ /; TensorRank@a == 2] := True
ScalarQ@div[a_?VectorQ] := True
TensorRank@div[a_?TensorQ] := TensorRank@a - 1
(* curl *)
curl[_?ConstantQ] := 0
TensorQ@curl[a_?VectorQ] := True
VectorQ@curl[a_?VectorQ] := True
TensorRank@curl[a_?VectorQ] := 1
(* DotNabla *)
DotNabla[_, _?ConstantQ] := 0
(* Dp *)
Dp[_?ConstantQ, args__] := 0
TensorQ@Dp[a_, args__] := TensorQ@a
VectorQ@Dp[a_, args__] := VectorQ@a
ScalarQ@Dp[a_, args__] := ScalarQ@a
TensorRank@Dp[a_, args__] := TensorRank@a
(* Delta *)
Delta[_?ConstantQ] := 0
TensorQ@Delta[a_] := TensorQ@a
VectorQ@Delta[a_] := VectorQ@a
ScalarQ@Delta[a_] := ScalarQ@a
TensorRank@Delta[a_] := TensorRank@a
(* List *)
Dp[a_List, args__] := Dp[#, args] & /@ a
(* Don't assume anything is a scalar/constant *)
ScalarQ[a_] := False
ConstantQ[a_] := False
Protect[TensorQ, VectorQ, TensorRank, Dot, Cross, TensorProduct]
Where we have defined the Tensor-functions: grad, div, curl which are self-explanatory; Dp is the partial derivative, Delta gives the variation of a quantity, somewhat related to Dp, and DotNabla is (for the lack of better name) the convective derivative.
For better print, we'll define the following notation:
(* Hacky-way to create parenthesis *)
MakeBoxes[Parenthesis[a_], _] := MakeBoxes[a.1][[1, 1]]
MakeBoxes[grad[a_], form:TraditionalForm] := TemplateBox[{MakeBoxes@Parenthesis@a}, "grad", Tooltip -> Automatic,
DisplayFunction :> (RowBox@{"\[Del]", #1} &)]
MakeBoxes[div[a_], form:TraditionalForm] := TemplateBox[{MakeBoxes@Parenthesis@a}, "div", Tooltip -> Automatic,
DisplayFunction :> (RowBox@{"\[Del]\[CenterDot]", #1} &)]
MakeBoxes[curl[a_], form:TraditionalForm] := TemplateBox[{MakeBoxes@Parenthesis@a}, "curl", Tooltip -> Automatic,
DisplayFunction :> (RowBox@{"\[Del]\[Cross]", #1} &)]
MakeBoxes[DotNabla[a_, b_], form:TraditionalForm] := TemplateBox[{MakeBoxes@Parenthesis@a, MakeBoxes@Parenthesis@b}, "DotNabla", Tooltip -> Automatic,
DisplayFunction :> (RowBox@{"(", #1, "\[CenterDot]\[Del]", ")", #2} &)]
MakeBoxes[Delta[a_], form:TraditionalForm] := TemplateBox[{MakeBoxes@Parenthesis@a}, "Delta", Tooltip -> Automatic,
DisplayFunction :> (RowBox@{"\[Delta]", #1} &)]
And now for the most important part of the code, the function ExpandDerivative, which as the name suggest, expand the derivative-like functions:
(* Expand Derivatives/Vectors/Tensors on expr and apply custom rules *)
ExpandDerivative[expr_, rules_:{}] := expr //. Flatten@{
(* Custom Rules *)
rules,
(* Linearity *)
(op:grad|div|curl|Delta|Inactive[grad]|Inactive[div]|Inactive[curl]|Inactive[Delta]|Re|Im|Conjugate)[a_ + b__] :> op@a + op[+b],
(op:Dp|Inactive[Dp]|Sum|Inactive[Sum])[a_ + b__, arg__] :> op[a, arg] + op[+b, arg],
(op:Times|Dot|TensorProduct|Cross|DotNabla|Inactive[DotNabla])[a___, b_ + c__, d___] :> op[a, b, d] + op[a, +c, d],
(op:grad|div|curl|Delta|Inactive[grad]|Inactive[div]|Inactive[curl]|Inactive[Delta]|Re|Im|Conjugate)[(op\[CapitalSigma]:Sum|Inactive[Sum])[a_, args__]] :> op\[CapitalSigma][op@a, args],
(* Sum *)
(op:Sum|Inactive[Sum])[s_*a_, v_Symbol] /; FreeQ[s, v] :> s*op[a, v],
(op:Sum|Inactive[Sum])[s_, v_Symbol] /; FreeQ[s, v] :> s*op[1, v],
(* Complexes *)
Conjugate@(op:Times|Dot|Cross|TensorProduct)[a_, b__] :> op[Conjugate@a, Conjugate@op@b], (* Pass Conjugate to child *)
(op:grad|div|curl|Delta|Inactive[grad]|Inactive[div]|Inactive[curl]|Inactive[Delta])[(opC:Re|Im|Conjugate)[a_]] :> opC@op@a, (* Pass Conjugate/Re/Im to parent *)
Dp[(op:Re|Im|Conjugate)[a_], v_] :> op@Dp[a, v], (* Pass Conjugate/Re/Im to parent *)
(* Triple Product *)
Cross[a_?VectorQ, Cross[b_?VectorQ, c_?VectorQ]] :> b*a.c - c*a.b,
Cross[Cross[a_?VectorQ, b_?VectorQ], c_?VectorQ] :> b*a.c - a*b.c,
(* Quadruple Product *)
Dot[Cross[a_?VectorQ, b_?VectorQ], Cross[c_?VectorQ, d_?VectorQ]] :> (a.c)*(b.d) - (a.d)*(b.c),
(* Second Derivatives *)
div@curl[_?VectorQ] :> 0,
curl@grad[_?ScalarQ | _?VectorQ] :> 0,
(* grad *)
grad[(s_?ScalarQ) * (b_)] :> s*grad@b + b\[TensorProduct]grad@s,
grad[(a_?VectorQ) . (b_?VectorQ)] :> a\[Cross]curl@b + b\[Cross]curl@a + DotNabla[a, b] + DotNabla[b, a], (* Use physics form *)
grad[(s_?ScalarQ) ^ (n_?ConstantQ)] :> n*s^(n-1)*grad@s,
grad[(n_?ConstantQ /; ScalarQ@n) ^ (s_?ScalarQ)] :> n^s*Log[n]*grad@s,
(* div *)
div[(s_?ScalarQ) * (b_?TensorQ)] :> s*div@b + b.grad@s,
div[(a_?VectorQ) \[TensorProduct] (b_?VectorQ)] :> DotNabla[b, a] + a*div@b,
div[(a_?VectorQ) \[Cross] (b_?VectorQ)] :> b.curl@a - a.curl@b,
(* curl *)
curl[(s_?ScalarQ) * (b_?VectorQ)] :> grad[s]\[Cross]b + s*curl@b,
curl[(a_?VectorQ) \[Cross] (b_?VectorQ)] :> div[a\[TensorProduct]b - b\[TensorProduct]a],
(* DotNabla *)
DotNabla[(s_?ScalarQ) * (b_?VectorQ), c_?VectorQ] :> s*DotNabla[b, c],
DotNabla[a_?VectorQ, (\[Beta]_?ScalarQ)*(c_?VectorQ)] :> c*a.grad@\[Beta] + \[Beta]*DotNabla[a, c],
(* Dp *)
Dp[(op:Times|Dot|Cross|TensorProduct)[a_, b__], v_Symbol] :> op[Dp[a, v], b] + op[a, Dp[op@b, v]],
Dp[Power[a_?ScalarQ, b_?ScalarQ], v_Symbol] :> Power[a, b-1]*b*Dp[a, v] + Power[a,b]*Log[a]*Dp[b, v],
(* Delta *)
Delta[(op:Times|Dot|Cross|TensorProduct)[a_, b__]] :> op[Delta@a, b] + op[a, Delta@op@b],
Delta@Power[a_?ScalarQ, b_?ScalarQ] :> Power[a, b-1]*b*Delta[a] + Power[a,b]*Log[a]*Delta[b]
}
Some examples. Calculating the divergent of Maxwell Stress Tensor in vaccum:
![MST][7]
Calculate the Einstein-Laub force density for linear dielectrics:
![EL][8]
Testing Poynting theorem in vaccum (no sources):
![P][9]
Where the first argument is the quantity being "tested".
Many other uses are possible and is fairly easy to extended some definitions.
[1]: http://library.wolfram.com/infocenter/Demos/434/
[2]: http://library.wolfram.com/infocenter/MathSource/8827/
[3]: https://sites.math.washington.edu/~lee/Ricci/
[4]: http://www.stargazing.net/yizen/Tensoria.html
[5]: http://www.vaudrevange.com/pascal/grt/
[6]: http://www.xact.es/
[7]: http://community.wolfram.com//c/portal/getImageAttachment?filename=Fig1.png&userId=845022
[8]: http://community.wolfram.com//c/portal/getImageAttachment?filename=Fig2.png&userId=845022
[9]: http://community.wolfram.com//c/portal/getImageAttachment?filename=Fig3.png&userId=845022Thales Fernandes2017-06-23T22:49:11ZMonad code generation and extension
http://community.wolfram.com/groups/-/m/t/1126923
## Introduction
This document aims to introduce monadic programming in Mathematica / Wolfram Language (WL) in a concise and code-direct manner. The core of the monad codes discussed is simple, derived from the fundamental principles of Mathematica / WL.
The usefulness of the monadic programming approach manifests in multiple ways. Here are a few we are interested in:
1) easy to construct, read, and modify sequences of commands (pipelines),
2) easy to program polymorphic behaviour,
3) easy to program context utilization.
Speaking informally,
- Monad programming provides an interface that allows interactive, dynamic creation and change of sequentially structured computations with polymorphic and context-aware behavior.
The theoretical background provided in this document is given in the Wikipedia article on Monadic programming. The code in this document is based on the primary monad definition given [Wk1,H3]. (Based on the ["Kleisli triple"](https://en.wikipedia.org/wiki/Kleisli_category) and used in Haskell.)
The general monad structure can be seen as:
1) a software design pattern;
2) a fundamental programming construct (similar to class in object-oriented programming);
3) an interface for software types to have implementations of.
In this document we treat the monad structure as a [design pattern](https://en.wikipedia.org/wiki/Software_design_pattern), [[Wk3](https://en.wikipedia.org/wiki/Software_design_pattern)]. (After reading [H3] point 2 becomes more obvious. A similar in spirit, minimalistic approach to [Object-oriented Design Patterns](https://en.wikipedia.org/wiki/Design_Patterns) is given in [[AA1](https://github.com/antononcube/MathematicaForPrediction/blob/master/MarkdownDocuments/Implementation-of-Object_Oriented-Programming-Design-Patterns-in-Mathematica.md)].)
We do not deal with types for monads explicitly, we generate code for monads instead. One reason for this is the "monad design pattern" perspective; another one is that in Mathematica / WL the notion of algebraic data type is not needed -- pattern matching comes from the core "book of replacement rules" principle.
The rest of the document is organized as follows.
**1.** *Fundamental sections*
The section "What is a monad?" gives the necessary definitions. The section "The basic Maybe monad" shows how to program a monad from scratch in Mathematica / WL. The section "Extensions with polymorphic behavior" shows how extensions of the basic monad functions can be made. (These three sections form a complete read on monadic programming, the rest of document can be skipped.)
**2.** *Monadic programming in practice*
The section "Monad code generation" describes packages for generating monad code. The section "Flow control in monads" describes additional, control flow functionalities. The section "General work-flow of monad code generation utilization" gives a general perspective on the use monad code generation. The section "Software design with monadic programming" discusses (small scale) software design with monadic programming.
**3.** *Case study sections*
The case study sections "Contextual monad classification" and "Tracing monad pipelines" hopefully have interesting and engaging examples of monad code generation, extension, and utilization.
## What is a monad?
### The monad definition
In this document a monad is any set of a symbol $m$ and two operators *unit* and *bind* that adhere to the monad laws. (See the next sub-section.) The definition is taken from [Wk1] and [H3] and phrased in Mathematica / WL terms in this section. In order to be brief, we deliberately do not consider the equivalent monad definition based on *unit*, *join*, and *map* (also given in [H3].)
Here are operators for a monad associated with a certain symbol `M`:
1. monad *unit* function ("return" in Haskell notation) is `Unit[x_] := M[x]`;
2. monad *bind* function (">>=" in Haskell notation) is a rule like `Bind[M[x_], f_] := f[x] with MatchQ[f[x],M[_]]` giving `True`.
Note that:
- the function `Bind` unwraps the content of `M[_]` and gives it to the function `f`;
- the functions $f_i$ are responsible to return results wrapped with the monad symbol `M`.
Here is an illustration formula showing a **monad pipeline**:
![Monad-formula-generic](http://imgur.com/JyNp2os.png)
From the definition and formula it should be clear that if for the result of `Bind[_M,f[x]]` the test `MatchQ[f[x],_M]` is `True` then the result is ready to be fed to the next binding operation in monad's pipeline. Also, it is clear that it is easy to program the pipeline functionality with `Fold`:
Fold[Bind, M[x], {f1, f2, f3}]
(* Bind[Bind[Bind[M[x], f1], f2], f3] *)
### The monad laws
The monad laws definitions are taken from
[[H1](https://wiki.haskell.org/Monad_laws)] and [H3]. In the monad laws given below the symbol "⟹" is for monad's binding operation and ↦ is for a function in anonymous form.
Here is a table with the laws:
![](http://imgur.com/E4VEucD.png)
**Remark:** The monad laws are satisfied for every symbol in Mathematica / WL with `List` being the unit operation and `Apply` being the binding operation.
![](http://imgur.com/FR6S2Fu.png)
### Expected monadic programming features
Looking at formula (1) -- and having certain programming experiences -- we can expect the following features when using monadic programming.
- Computations that can be expressed with monad pipelines are easy to construct and read.
- By programming the binding function we can tuck-in a variety of monad behaviours -- this is the so called "programmable semicolon" feature of monads.
- Monad pipelines can be constructed with `Fold`, but with suitable definitions of infix operators like `DoubleLongRightArrow` (⟹) we can produce code that resembles the pipeline in formula (1).
- A monad pipeline can have polymorphic behaviour by overloading the signatures of $f_i$ (and if we have to, `Bind`.)
These points are clarified below. For more complete discussions see [[Wk1](https://en.wikipedia.org/wiki/Monad_(functional_programming))] or [H3].
## The basic Maybe monad
It is fairly easy to program the basic monad Maybe discussed in [WK1].
The goal of the Maybe monad is to provide easy exception handling in a sequence of chained computational steps. If one of the computation steps fails then the whole pipeline returns a designated failure symbol, say `None` otherwise the result after the last step is wrapped in another designated symbol, say Maybe.
Here is the special version of the generic pipeline formula (1) for the Maybe monad:
!["Monad-formula-maybe"](http://imgur.com/DRNAhPG.png)
Here is the minimal code to get a functional Maybe monad (for a more detailed exposition of code and explanations see [AA7]):
MaybeUnitQ[x_] := MatchQ[x, None] || MatchQ[x, Maybe[___]];
MaybeUnit[None] := None;
MaybeUnit[x_] := Maybe[x];
MaybeBind[None, f_] := None;
MaybeBind[Maybe[x_], f_] :=
Block[{res = f[x]}, If[FreeQ[res, None], res, None]];
MaybeEcho[x_] := Maybe@Echo[x];
MaybeEchoFunction[f___][x_] := Maybe@EchoFunction[f][x];
MaybeOption[f_][xs_] :=
Block[{res = f[xs]}, If[FreeQ[res, None], res, Maybe@xs]];
In order to make the pipeline form of the code we write let us give definitions to a suitable infix operator (like "⟹") to use MaybeBind:
DoubleLongRightArrow[x_?MaybeUnitQ, f_] := MaybeBind[x, f];
DoubleLongRightArrow[x_, y_, z__] :=
DoubleLongRightArrow[DoubleLongRightArrow[x, y], z];
Here is an example of a Maybe monad pipeline using the definitions so far:
data = {0.61, 0.48, 0.92, 0.90, 0.32, 0.11};
MaybeUnit[data]⟹(* lift data into the monad *)
(Maybe@ Join[#, RandomInteger[8, 3]] &)⟹(* add more values *)
MaybeEcho⟹(* display current value *)
(Maybe @ Map[If[# < 0.4, None, #] &, #] &)(* map values that are too small to None *)
(* {0.61,0.48,0.92,0.9,0.32,0.11,4,4,0}
None *)
The result is `None` because:
1. the data has a number that is too small, and
2. the definition of `MaybeBind` stops the pipeline aggressively using a `FreeQ[_,None]` test.
### Monad laws verification
Let us convince ourselves that the current definition of `MaybeBind` gives a monad.
The verification is straightforward to program and shows that the implemented Maybe monad adheres to the monad laws.
!["Monad-laws-table-Maybe"](http://imgur.com/N2PV9sY.png)
## Extensions with polymorphic behavior
We can see from formulas (1) and (2) that the monad codes can be easily extended through overloading the pipeline functions.
For example the extension of the Maybe monad to handle of `Dataset` objects is fairly easy and straightforward.
Here is the formula of the Maybe monad pipeline extended with `Dataset` objects:
![](http://imgur.com/aWdtR6B.png)
Here is an example of a polymorphic function definition for the Maybe monad:
MaybeFilter[filterFunc_][xs_] := Maybe@Select[xs, filterFunc[#] &];
MaybeFilter[critFunc_][xs_Dataset] := Maybe@xs[Select[critFunc]];
See [AA7] for more detailed examples of polymorphism in monadic programming with Mathematica / WL.
A complete discussion can be found in [H3]. (The main message of [H3] is the poly-functional and polymorphic properties of monad implementations.)
### Polymorphic monads in R's dplyr
The R package [dplyr](http://dplyr.tidyverse.org), [[R1](https://github.com/tidyverse/dplyr)], has implementations centered around monadic polymorphic behavior. The command pipelines based on [dplyr](http://dplyr.tidyverse.org)can work on R data frames, SQL tables, and Spark data frames without changes.
Here is a diagram of a typical work-flow with dplyr:
[!["dplyr-pipeline"](http://i.imgur.com/kqch4eUl.jpg)](http://i.imgur.com/kqch4eU.jpg)
The diagram shows how a pipeline made with dplyr can be re-run (or reused) for data stored in different data structures.
## Monad code generation
We can see monad code definitions like the ones for Maybe as some sort of initial templates for monads that can be extended in specific ways depending on their applications. Mathematica / WL can easily provide code generation for such templates; (see [[WL1](https://mathematica.stackexchange.com/a/2352/34008)]). As it was mentioned in the introduction, we do not deal with types for monads explicitly, we generate code for monads instead.
In this section are given examples with packages that generate monad codes. The case study sections have examples of packages that utilize generated monad codes.
### Maybe monads code generation
The package [[AA2](https://github.com/antononcube/MathematicaForPrediction/blob/master/MonadicProgramming/MaybeMonadCodeGenerator.m)] provides a Maybe code generator that takes as an argument a prefix for the generated functions. (Monad code generation is discussed further in the section "General work-flow of monad code generation utilization".)
Here is an example:
Import["https://raw.githubusercontent.com/antononcube/MathematicaForPrediction/master/MonadicProgramming/MaybeMonadCodeGenerator.m"]
GenerateMaybeMonadCode["AnotherMaybe"]
data = {0.61, 0.48, 0.92, 0.90, 0.32, 0.11};
AnotherMaybeUnit[data]⟹(* lift data into the monad *)
(AnotherMaybe@Join[#, RandomInteger[8, 3]] &)⟹(* add more values *)
AnotherMaybeEcho⟹(* display current value *)
(AnotherMaybe @ Map[If[# < 0.4, None, #] &, #] &)(* map values that are too small to None *)
(* {0.61,0.48,0.92,0.9,0.32,0.11,8,7,6}
AnotherMaybeBind: Failure when applying: Function[AnotherMaybe[Map[Function[If[Less[Slot[1], 0.4], None, Slot[1]]], Slot[1]]]]
None *)
We see that we get the same result as above (`None`) and a message prompting failure.
### State monads code generation
The State monad is also basic and its programming in Mathematica / WL is not that difficult. (See [[AA3](https://github.com/antononcube/MathematicaForPrediction/blob/master/MonadicProgramming/StateMonadCodeGenerator.m)].)
Here is the special version of the generic pipeline formula (1) for the State monad:
!["Monad-formula-State"](http://imgur.com/rbXWydC.png)
Note that since the State monad pipeline caries both a value and a state, it is a good idea to have functions that manipulate them separately.
For example, we can have functions for context modification and context retrieval. (These are done in [[AA3](https://github.com/antononcube/MathematicaForPrediction/blob/master/MonadicProgramming/StateMonadCodeGenerator.m)].)
This loads the package [[AA3](https://github.com/antononcube/MathematicaForPrediction/blob/master/MonadicProgramming/StateMonadCodeGenerator.m)]:
Import["https://raw.githubusercontent.com/antononcube/MathematicaForPrediction/master/MonadicProgramming/StateMonadCodeGenerator.m"]
This generates the State monad for the prefix "StMon":
GenerateStateMonadCode["StMon"]
The following StMon pipeline code starts with a random matrix and then replaces numbers in the current pipeline value according to a threshold parameter kept in the context. Several times are invoked functions for context deposit and retrieval.
SeedRandom[34]
StMonUnit[RandomReal[{0, 1}, {3, 2}], <|"mark" -> "TooSmall", "threshold" -> 0.5|>]⟹
StMonEchoValue⟹
StMonEchoContext⟹
StMonAddToContext["data"]⟹
StMonEchoContext⟹
(StMon[#1 /. (x_ /; x < #2["threshold"] :> #2["mark"]), #2] &)⟹
StMonEchoValue⟹
StMonRetrieveFromContext["data"]⟹
StMonEchoValue⟹
StMonRetrieveFromContext["mark"]⟹
StMonEchoValue;
(* value: {{0.789884,0.831468},{0.421298,0.50537},{0.0375957,0.289442}}
context: <|mark->TooSmall,threshold->0.5|>
context: <|mark->TooSmall,threshold->0.5,data->{{0.789884,0.831468},{0.421298,0.50537},{0.0375957,0.289442}}|>
value: {{0.789884,0.831468},{TooSmall,0.50537},{TooSmall,TooSmall}}
value: {{0.789884,0.831468},{0.421298,0.50537},{0.0375957,0.289442}}
value: TooSmall *)
## Flow control in monads
We can implement dedicated functions for governing the pipeline flow in a monad.
Let us look at a breakdown of these kind of functions using the State monad StMon generated above.
### Optional acceptance of a function result
A basic and simple pipeline control function is for optional acceptance of result -- if failure is obtained applying $f$ then we ignore its result (and keep the current pipeline value.)
Here is an example with `StMonOption` :
SeedRandom[34]
StMonUnit[RandomReal[{0, 1}, 5]]⟹
StMonEchoValue⟹
StMonOption[If[# < 0.3, None] & /@ # &]⟹
StMonEchoValue
(* value: {0.789884,0.831468,0.421298,0.50537,0.0375957}
value: {0.789884,0.831468,0.421298,0.50537,0.0375957}
StMon[{0.789884, 0.831468, 0.421298, 0.50537, 0.0375957}, <||>] *)
Without `StMonOption` we get failure:
SeedRandom[34]
StMonUnit[RandomReal[{0, 1}, 5]]⟹
StMonEchoValue⟹
(If[# < 0.3, None] & /@ # &)⟹
StMonEchoValue
(* value: {0.789884,0.831468,0.421298,0.50537,0.0375957}
StMonBind: Failure when applying: Function[Map[Function[If[Less[Slot[1], 0.3], None]], Slot[1]]]
None *)
### Conditional execution of functions
It is natural to want to have the ability to chose a pipeline function application based on a condition.
This can be done with the functions `StMonIfElse` and `StMonWhen`.
SeedRandom[34]
StMonUnit[RandomReal[{0, 1}, 5]]⟹
StMonEchoValue⟹
StMonIfElse[
Or @@ (# < 0.4 & /@ #) &,
(Echo["A too small value is present.", "warning:"];
StMon[Style[#1, Red], #2]) &,
StMon[Style[#1, Blue], #2] &]⟹
StMonEchoValue
(* value: {0.789884,0.831468,0.421298,0.50537,0.0375957}
warning: A too small value is present.
value: {0.789884,0.831468,0.421298,0.50537,0.0375957}
StMon[{0.789884,0.831468,0.421298,0.50537,0.0375957},<||>] *)
**Remark:** Using flow control functions like `StMonIfElse` and `StMonWhen` with appropriate messages is a better way of handling computations that might fail. The silent failures handling of the basic Maybe monad is convenient only in a small number of use cases.
### Iterative functions
The last group of pipeline flow control functions we consider comprises iterative functions that provide the functionalities of `Nest`, `NestWhile`, `FoldList`, etc.
In [[AA3](https://github.com/antononcube/MathematicaForPrediction/blob/master/MonadicProgramming/StateMonadCodeGenerator.m)] these functionalities are provided through the function StMonIterate.
Here is a basic example using `Nest` that corresponds to `Nest[#+1&,1,3]`:
StMonUnit[1]⟹StMonIterate[Nest, (StMon[#1 + 1, #2]) &, 3]
(* StMon[4, <||>] *)
Consider this command that uses the full signature of `NestWhileList`:
NestWhileList[# + 1 &, 1, # < 10 &, 1, 4]
(* {1, 2, 3, 4, 5} *)
Here is the corresponding StMon iteration code:
StMonUnit[1]⟹StMonIterate[NestWhileList, (StMon[#1 + 1, #2]) &, (#[[1]] < 10) &, 1, 4]
(* StMon[{1, 2, 3, 4, 5}, <||>] *)
Here is another results accumulation example with `FixedPointList` :
StMonUnit[1.]⟹
StMonIterate[FixedPointList, (StMon[(#1 + 2/#1)/2, #2]) &]
(* StMon[{1., 1.5, 1.41667, 1.41422, 1.41421, 1.41421, 1.41421}, <||>] *)
When the functions `NestList`, `NestWhileList`, `FixedPointList` are used with `StMonIterate` their results can be stored in the context. Here is an example:
StMonUnit[1.]⟹
StMonIterate[FixedPointList, (StMon[(#1 + 2/#1)/2, #2]) &, "fpData"]
(* StMon[{1., 1.5, 1.41667, 1.41422, 1.41421, 1.41421, 1.41421}, <|"fpData" -> {StMon[1., <||>],
StMon[1.5, <||>], StMon[1.41667, <||>], StMon[1.41422, <||>], StMon[1.41421, <||>],
StMon[1.41421, <||>], StMon[1.41421, <||>]} |>] *)
More elaborate tests can be found in [[AA8](https://github.com/antononcube/MathematicaForPrediction/blob/master/UnitTests/GeneratedStateMonadTests.m)].
### Partial pipelines
Because of the associativity law we can design pipeline flows based on functions made of "sub-pipelines."
fEcho = Function[{x, ct}, StMonUnit[x, ct]⟹StMonEchoValue⟹StMonEchoContext];
fDIter = Function[{x, ct},
StMonUnit[y^x, ct]⟹StMonIterate[FixedPointList, StMonUnit@D[#, y] &, 20]];
StMonUnit[7]⟹fEcho⟹fDIter⟹fEcho;
(*
value: 7
context: <||>
value: {y^7,7 y^6,42 y^5,210 y^4,840 y^3,2520 y^2,5040 y,5040,0,0}
context: <||> *)
## General work-flow of monad code generation utilization
With the abilities to generate and utilize monad codes it is natural to consider the following work flow. (Also shown in the diagram below.)
1. Come up with an idea that can be expressed with monadic programming.
2. Look for suitable monad implementation.
3. If there is no such implementation, make one (or two, or five.)
4. Having a suitable monad implementation, generate the monad code.
5. Implement additional pipeline functions addressing envisioned use cases.
6. Start making pipelines for the problem domain of interest.
7. Are the pipelines are satisfactory? If not go to **5**. (Or **2.**)
[!["make-monads"](http://imgur.com/9iinzkzl.jpg)](http://imgur.com/9iinzkz.jpg)
### Monad templates
The template nature of the general monads can be exemplified with the group of functions in the package [StateMonadCodeGenerator.m](https://github.com/antononcube/MathematicaForPrediction/blob/master/MonadicProgramming/StateMonadCodeGenerator.m), [4].
They are in five groups:
1. base monad functions (unit testing, binding),
2. display of the value and context,
3. context manipulation (deposit, retrieval, modification),
4. flow governing (optional new value, conditional function application, iteration),
5. other convenience functions.
We can say that all monad implementations will have their own versions of these groups of functions. The more specialized monads will have functions specific to their intended use. Such special monads are discussed in the case study sections.
## Software design with monadic programming
The application of monadic programming to a particular problem domain is very similar to designing a software framework or designing and implementing a Domain Specific Language (DSL).
The answers of the question "When to use monadic programming?" can form a large list. This section provides only a couple of general, personal viewpoints on monadic programming in software design and architecture. The principles of monadic programming can be used to build systems from scratch (like Haskell and Scala.) Here we discuss making specialized software with or within already existing systems.
### Framework design
Software framework design is about architectural solutions that capture the commonality and variability in a problem domain in such a way that:
1) significant speed-up can be achieved when making new applications, and
2) a set of policies can be imposed on the new applications.
The rigidness of the framework provides and supports its flexibility -- the framework has a backbone of rigid parts and a set of "hot spots" where new functionalities are plugged-in.
Usually Object-Oriented Programming (OOP) frameworks provide inversion of control -- the general work-flow is already established, only parts of it are changed. (This is characterized with "leave the driving to us" and "don't call us we will call you.")
The point of utilizing monadic programming is to be able to easily create different new work-flows that share certain features. (The end user is the driver, on certain rail paths.)
In my opinion making a software framework of small to moderate size with monadic programming principles would produce a library of functions each with polymorphic behaviour that can be easily sequenced in monadic pipelines. This can be contrasted with OOP framework design in which we are more likely to end up with backbone structures that (i) are static and tree-like, and (ii) are extended or specialized by plugging-in relevant objects. (Those plugged-in objects themselves can be trees, but hopefully short ones.)
### DSL development
Given a problem domain the general monad structure can be used to shape and guide the development of DSLs for that problem domain.
Generally, in order to make a DSL we have to choose the language syntax and grammar. Using monadic programming the syntax and grammar commands are clear. (The monad pipelines are the commands.) What is left is "just" the choice of particular functions and their implementations.
Another way to develop such a DSL is through a grammar of natural language commands. Generally speaking, just designing the grammar -- without developing the corresponding interpreters -- would be very helpful in figuring out the components at play. Monadic programming meshes very well with this approach and applying the two approaches together can be very fruitful.
## Contextual monad classification *(case study)*
In this section we show an extension of the State monad into a monad aimed at machine learning classification work-flows.
### Motivation
We want to provide a DSL for doing machine learning classification tasks that allows us:
1) to do basic summarization and visualization of the data,
1) to control splitting of the data into training and testing sets;
2) to apply the built-in classifiers;
3) to apply classifier ensembles (see [[AA9](https://github.com/antononcube/MathematicaForPrediction/blob/master/ClassifierEnsembles.m)] and [[AA10](https://mathematicaforprediction.wordpress.com/2016/10/15/roc-for-classifier-ensembles-bootstrapping-damaging-and-interpolation/)]);
4) to evaluate classifier performances with standard measures and
5) ROC plots.
Also, we want the DSL design to provide clear directions how to add (hook-up or plug-in) new functionalities.
The package [[AA4](https://github.com/antononcube/MathematicaForPrediction/blob/master/MonadicProgramming/MonadicContextualClassification.m)] discussed below provides such a DSL through monadic programming.
### Package and data loading
This loads the package [[AA4](https://github.com/antononcube/MathematicaForPrediction/blob/master/MonadicProgramming/MonadicContextualClassification.m)]:
Import["https://raw.githubusercontent.com/antononcube/MathematicaForPrediction/master/MonadicProgramming/MonadicContextualClassification.m"]
This gets some test data (the Titanic dataset):
dataName = "Titanic";
ds = Dataset[Flatten@*List @@@ ExampleData[{"MachineLearning", dataName}, "Data"]];
varNames = Flatten[List @@ ExampleData[{"MachineLearning", dataName}, "VariableDescriptions"]];
varNames = StringReplace[varNames, "passenger" ~~ (WhitespaceCharacter ..) -> ""];
If[dataName == "FisherIris", varNames = Most[varNames]];
ds = ds[All, AssociationThread[varNames -> #] &];
### Monad design
The package [[AA4](https://github.com/antononcube/MathematicaForPrediction/blob/master/MonadicProgramming/MonadicContextualClassification.m)] provides functions for the monad **ClCon** -- the functions implemented in [[AA4](https://github.com/antononcube/MathematicaForPrediction/blob/master/MonadicProgramming/MonadicContextualClassification.m)] have the prefix "ClCon".
The classifier contexts are Association objects. The pipeline values can have the form:
ClCon[ val, context:(_String|_Association) ]
The ClCon specific monad functions deposit or retrieve values from the context with the keys: "trainData", "testData", "classifier". The general idea is that if the current value of the pipeline cannot provide all arguments for a ClCon function, then the needed arguments are taken from the context. If that fails, then an message is issued.
This is illustrated with the following pipeline with comments example.
[!["ClCon-basic-example"](http://imgur.com/98e60pN.png)](http://imgur.com/98e60pN.png)
The pipeline and results above demonstrate polymorphic behaviour over the classifier variable in the context:
different functions are used if that variable is a `ClassifierFunction` object or an association of
named `ClassifierFunction` objects.
Note the demonstrated granularity and sequentiality of the operations coming from using a monad structure.
With those kind of operations it would be easy to make interpreters for natural language DSLs.
### Another usage example
This monadic pipeline in this example goes through several stages: data summary, classifier training, evaluation, acceptance test, and if the results are rejected a new classifier is made with a different algorithm using the same data splitting. The context keeps track of the data and its splitting. That allows the conditional classifier switch to be concisely specified.
First let us define a function that takes a `Classify` method as an argument and makes a classifier and calculates performance measures.
ClSubPipe[method_String] :=
Function[{x, ct},
ClConUnit[x, ct]⟹
ClConMakeClassifier[method]⟹
ClConEchoFunctionContext["classifier:",
ClassifierInformation[#["classifier"], Method] &]⟹
ClConEchoFunctionContext["training time:", ClassifierInformation[#["classifier"], "TrainingTime"] &]⟹
ClConClassifierMeasurements[{"Accuracy", "Precision", "Recall"}]⟹
ClConEchoValue⟹
ClConEchoFunctionContext[
ClassifierMeasurements[#["classifier"],
ClConToNormalClassifierData[#["testData"]], "ROCCurve"] &]
];
Using the sub-pipeline function `ClSubPipe` we make the outlined pipeline.
SeedRandom[12]
res =
ClConUnit[ds]⟹
ClConSplitData[0.7]⟹
ClConEchoFunctionValue["summaries:", ColumnForm[Normal[RecordsSummary /@ #]] &]⟹
ClConEchoFunctionValue["xtabs:",
MatrixForm[CrossTensorate[Count == varNames[[1]] + varNames[[-1]], #]] & /@ # &]⟹
ClSubPipe["LogisticRegression"]⟹
(If[#1["Accuracy"] > 0.8,
Echo["Good accuracy!", "Success:"]; ClConFail,
Echo["Make a new classifier", "Inaccurate:"];
ClConUnit[#1, #2]] &)⟹
ClSubPipe["RandomForest"];
[!["ClCon-pipeline-2-output"](http://imgur.com/ffXPNMvl.png)](http://imgur.com/ffXPNMv.png)
## Tracing monad pipelines *(case study)*
The monadic implementations in the package [MonadicTracing.m](https://github.com/antononcube/MathematicaForPrediction/blob/master/MonadicProgramming/MonadicTracing.m), [[AA5](https://github.com/antononcube/MathematicaForPrediction/blob/master/MonadicProgramming/MonadicTracing.m)] allow tracking of the pipeline execution of functions within other monads.
The primary reason for developing the package was the desire to have the ability to print a tabulated trace of code and comments using the usual monad pipeline notation. (I.e. without conversion to strings etc.)
It turned out that by programming [MonadicTracing.m](https://github.com/antononcube/MathematicaForPrediction/blob/master/MonadicProgramming/MonadicTracing.m) I came up with a [monad transformer](https://en.wikipedia.org/wiki/Monad_transformer); see [[Wk2](https://en.wikipedia.org/wiki/Monad_transformer)], [H2].
### Package loading
This loads the package [[AA5](https://github.com/antononcube/MathematicaForPrediction/blob/master/MonadicProgramming/MonadicTracing.m)]:
Import["https://raw.githubusercontent.com/antononcube/MathematicaForPrediction/master/MonadicProgramming/MonadicTracing.m"]
### Usage example
This generates a Maybe monad to be used in the example (for the prefix "Perhaps"):
GenerateMaybeMonadCode["Perhaps"]
GenerateMaybeMonadSpecialCode["Perhaps"]
In following example we can see that pipeline functions of the Perhaps monad are interleaved with comment strings.
Producing the grid of functions and comments happens "naturally" with the monad function `TraceMonadEchoGrid`.
data = RandomInteger[10, 15];
TraceMonadUnit[PerhapsUnit[data]]⟹"lift to monad"⟹
TraceMonadEchoContext⟹
PerhapsFilter[# > 3 &]⟹"filter current value"⟹
PerhapsEcho⟹"display current value"⟹
PerhapsWhen[#[[3]] > 3 &,
PerhapsEchoFunction[Style[#, Red] &]]⟹
(Perhaps[#/4] &)⟹
PerhapsEcho⟹"display current value again"⟹
TraceMonadEchoGrid[Grid[#, Alignment -> Left] &];
Note that :
1. the tracing is initiated by just using `TraceMonadUnit`;
2. pipeline functions (actual code) and comments are interleaved;
3. putting a comment string after a pipeline function is optional.
Another example is the ClCon pipeline in the sub-section "Monad design" in the previous section.
## Summary
This document presents a style of using monadic programming in Wolfram Language (Mathematica). The style has some shortcomings, but it definitely provides convenient features for day-to-day programming and in coming up with architectural designs.
The style is based on WL's basic language features. As a consequence it is fairly concise and produces light overhead.
Ideally, the packages for the code generation of the basic Maybe and State monads would serve as starting points for other more general or more specialized monadic programs.
## References
### Monadic programming
[Wk1] Wikipedia entry: [Monad (functional programming)](https://en.wikipedia.org/wiki/Monad_(functional_programming)), URL: [https://en.wikipedia.org/wiki/Monad_(functional_programming)](https://en.wikipedia.org/wiki/Monad_(functional_programming)) .
[Wk2] Wikipedia entry: [Monad transformer](https://en.wikipedia.org/wiki/Monad_transformer), URL: [https://en.wikipedia.org/wiki/Monad_transformer](https://en.wikipedia.org/wiki/Monad_transformer) .
[Wk3] Wikipedia entry: [Software Design Pattern](https://en.wikipedia.org/wiki/Software_design_pattern), URL: [https://en.wikipedia.org/wiki/Software_design_pattern](https://en.wikipedia.org/wiki/Software_design_pattern) .
[H1] Haskell.org article: [Monad laws,](https://wiki.haskell.org/Monad_laws) URL: [https://wiki.haskell.org/Monad_laws](https://wiki.haskell.org/Monad_laws).
[H2] Sheng Liang, Paul Hudak, Mark Jones, ["Monad transformers and modular interpreters",](http://haskell.cs.yale.edu/wp-content/uploads/2011/02/POPL96-Modular-interpreters.pdf) (1995), Proceedings of the 22nd ACM SIGPLAN-SIGACT symposium on Principles of programming languages. New York, NY: ACM. pp. 333\[Dash]343. doi:10.1145/199448.199528.
[H3] Philip Wadler, ["The essence of functional programming"](https://page.mi.fu-berlin.de/scravy/realworldhaskell/materialien/the-essence-of-functional-programming.pdf), (1992), 19'th Annual Symposium on Principles of Programming Languages, Albuquerque, New Mexico, January 1992.
### R
[R1] Hadley Wickham et al., [dplyr: A Grammar of Data Manipulation](https://github.com/tidyverse/dplyr), (2014), [tidyverse at GitHub](https://github.com/tidyverse), URL: [https://github.com/tidyverse/dplyr](https://github.com/tidyverse/dplyr) .
(See also, [http://dplyr.tidyverse.org](http://dplyr.tidyverse.org) .)
### Mathematica / Wolfram Language
[WL1] Leonid Shifrin, "Metaprogramming in Wolfram Language", (2012), [Mathematica StackExchange](https://mathematica.stackexchange.com). (Also posted at [Wolfram Community](http://community.wolfram.com) in 2017.)
URL of [the Mathematica StackExchange answer](https://mathematica.stackexchange.com/a/2352/34008): [https://mathematica.stackexchange.com/a/2352/34008](https://mathematica.stackexchange.com/a/2352/34008) .
URL of t[he Wolfram Community post](http://community.wolfram.com/groups/-/m/t/1121273): [http://community.wolfram.com/groups/-/m/t/1121273](http://community.wolfram.com/groups/-/m/t/1121273) .
### MathematicaForPrediction
[AA1] Anton Antonov, ["Implementation of Object-Oriented Programming Design Patterns in Mathematica"](https://github.com/antononcube/MathematicaForPrediction/blob/master/MarkdownDocuments/Implementation-of-Object_Oriented-Programming-Design-Patterns-in-Mathematica.md), (2016) [MathematicaForPrediction at GitHub](https://github.com/antononcube/MathematicaForPrediction)*, *[https://github.com/antononcube/MathematicaForPrediction](https://github.com/antononcube/MathematicaForPrediction).
[AA2] Anton Antonov, [Maybe monad code generator Mathematica package](https://github.com/antononcube/MathematicaForPrediction/blob/master/MonadicProgramming/MaybeMonadCodeGenerator.m), (2017), [MathematicaForPrediction at GitHub](https://github.com/antononcube/MathematicaForPrediction)*.
*URL: [https://github.com/antononcube/MathematicaForPrediction/blob/master/MonadicProgramming/MaybeMonadCodeGenerator.m](https://github.com/antononcube/MathematicaForPrediction/blob/master/MonadicProgramming/MaybeMonadCodeGenerator.m) .
[AA3] Anton Antonov, [State monad code generator Mathematica package](https://github.com/antononcube/MathematicaForPrediction/blob/master/MonadicProgramming/StateMonadCodeGenerator.m), (2017), [MathematicaForPrediction at GitHub](https://github.com/antononcube/MathematicaForPrediction)*.
*URL: [https://github.com/antononcube/MathematicaForPrediction/blob/master/MonadicProgramming/StateMonadCodeGenerator.m](https://github.com/antononcube/MathematicaForPrediction/blob/master/MonadicProgramming/StateMonadCodeGenerator.m) .
[AA4] Anton Antonov, [Monadic contextual classification Mathematica package](https://github.com/antononcube/MathematicaForPrediction/blob/master/MonadicProgramming/MonadicContextualClassification.m), (2017), [MathematicaForPrediction at GitHub](https://github.com/antononcube/MathematicaForPrediction)*.*
URL: [https://github.com/antononcube/MathematicaForPrediction/blob/master/MonadicProgramming/MonadicContextualClassification.m](https://github.com/antononcube/MathematicaForPrediction/blob/master/MonadicProgramming/MonadicContextualClassification.m) .
[AA5] Anton Antonov, [Monadic tracing Mathematica package](https://github.com/antononcube/MathematicaForPrediction/blob/master/MonadicProgramming/MonadicTracing.m), (2017), [MathematicaForPrediction at GitHub](https://github.com/antononcube/MathematicaForPrediction)*.
*URL: [https://github.com/antononcube/MathematicaForPrediction/blob/master/MonadicProgramming/MonadicTracing.m](https://github.com/antononcube/MathematicaForPrediction/blob/master/MonadicProgramming/MonadicTracing.m) .
[AA6] Anton Antonov, [MathematicaForPrediction utilities](https://github.com/antononcube/MathematicaForPrediction/blob/master/MathematicaForPredictionUtilities.m), (2014), [MathematicaForPrediction at GitHub](https://github.com/antononcube/MathematicaForPrediction).
URL: [https://github.com/antononcube/MathematicaForPrediction/blob/master/MathematicaForPredictionUtilities.m](https://github.com/antononcube/MathematicaForPrediction/blob/master/MathematicaForPredictionUtilities.m) .
[AA7] Anton Antonov, ["Simple monadic programming"](https://github.com/antononcube/MathematicaForPrediction/blob/master/Documentation/Simple-monadic-programming.pdf), (2017), [MathematicaForPrediction at GitHub](https://github.com/antononcube/MathematicaForPrediction).
(*Preliminary version, 40% done.)*
URL: [https://github.com/antononcube/MathematicaForPrediction/blob/master/Documentation/Simple-monadic-programming.pdf](https://github.com/antononcube/MathematicaForPrediction/blob/master/Documentation/Simple-monadic-programming.pdf) .
[AA8] Anton Antonov, [Generated State Monad Mathematica unit tests](https://github.com/antononcube/MathematicaForPrediction/blob/master/UnitTests/GeneratedStateMonadTests.m), (2017), [MathematicaForPrediction at GitHub](https://github.com/antononcube/MathematicaForPrediction).
URL: [https://github.com/antononcube/MathematicaForPrediction/blob/master/UnitTests/GeneratedStateMonadTests.m](https://github.com/antononcube/MathematicaForPrediction/blob/master/UnitTests/GeneratedStateMonadTests.m) .
[AA9] Anton Antonov, [Classifier ensembles functions Mathematica package](https://github.com/antononcube/MathematicaForPrediction/blob/master/ClassifierEnsembles.m), (2016), [MathematicaForPrediction at GitHub](https://github.com/antononcube/MathematicaForPrediction).
URL: [https://github.com/antononcube/MathematicaForPrediction/blob/master/ClassifierEnsembles.m](https://github.com/antononcube/MathematicaForPrediction/blob/master/ClassifierEnsembles.m) .
[AA10] Anton Antonov, ["ROC for classifier ensembles, bootstrapping, damaging, and interpolation"](https://mathematicaforprediction.wordpress.com/2016/10/15/roc-for-classifier-ensembles-bootstrapping-damaging-and-interpolation/), (2016), [MathematicaForPrediction at WordPress](https://mathematicaforprediction.wordpress.com).
URL: [https://mathematicaforprediction.wordpress.com/2016/10/15/roc-for-classifier-ensembles-bootstrapping-damaging-and-interpolation/](https://mathematicaforprediction.wordpress.com/2016/10/15/roc-for-classifier-ensembles-bootstrapping-damaging-and-interpolation/) .Anton Antonov2017-06-23T11:09:46ZTunnel a remote Wolfram Language kernel connection through ssh?
http://community.wolfram.com/groups/-/m/t/1126278
I am unable to connect to a remote kernel using the default procedure as documented in "[How to | Connect to a Remote Kernel][1]." It seem that the an ssh connection is made to the remote server, however the remote server then attempts to make a direct connection back to the client using an IP address and multiple dynamically assigned ports. This fails for a number of reasons. The client has a firewall. In addition, the client is connected over a VPN and routed via NAT which make it difficult to route back by IP address.
Creating a manual connection using the procedure as documented in "[Quick Answer How do I manually create a remote Wolfram Language kernel connection?][2]" also fails. It that case, I manually made an ssh connect to the remote system with -R flags for the remote ports. Unfortunately, the remote wolfram instance still fails to connect to those localhost ports.
Please let me know if you have any suggestions for connecting a Mathematica client and remote kernel through ssh.
Test System: Wolfram Mathematica 11.1.1 Kernel for Linux x86 (64-bit)
[1]: http://reference.wolfram.com/language/howto/ConnectToARemoteKernel.html
[2]: http://support.wolfram.com/kb/12495Donald Pellegrino2017-06-22T13:18:43ZVisual Interface to the Wolfram Language
http://community.wolfram.com/groups/-/m/t/1124967
Hi everyone.
I'm working on a visual interface to the Wolfram Language called [visX][1], and I'd like to ask what you all think of it.
Wolfram Language code can often be thought of as a set of blocks, each of which takes some inputs, does something, and produces an output. VisX lets you write WL code exactly this way - you draw a digram, connecting blocks with links. For example, say you want to count how many times each digit (0 to 9) occurs in the first 30 digits of Pi. With text-based WL code, you'd write
digits = RealDigits[N[Pi, 30]][[1]]
Count[digits, #] & /@ Range[0, 9]
In visX, you'd draw this:
![digits of Pi][2]
I guess it's pretty self-explanatory. In addition to using built-in WL blocks, you can write your own, like the CountInList block. Normally, blocks just transform inputs to output, but the CountInList block is mapped over its input which is indicated by the little brackets on the outside of its connection ports. (That's basically visual syntactic sugar for "/@" or Map.) The 4 in the upper-right corner indicates that results inside this block are showing results from the 4-th time through the map. The block with "digits" in it sets a variable, which is then referenced in the CountInList block.
You define blocks (which are basically functions) by just making an empty rectangle and dragging contents in then wiring them together, then you can use copies of the block wherever you want. A change in any copy of the block will be reflected in all other copies. There's no real difference between a defining a block and using it. Recursion can be specified by just including a copy of the block inside itself. Blocks can call other blocks in the same manner.
Just like regular WL code, visX blocks can be nested deeply, but with the visual interface, it's easy to zoom in and out. At any point, the UI will show you the right amount of detail for each block - sometimes no detail at all, sometimes its name and labels on its inputs, sometimes its actual contents (which can then be edited or further zoomed...).
visX is stand-alone software that runs locally on your machine, evaluates the diagram using your local Mathematica kernel, and receives the results and puts them back in the diagram. You can load data files using Import as usual.
One of the problems that I've seen with visual languages in the past is that while simple things are easy to do, the code quickly gets too complex to manage and the visual interface starts to get in the way. With the Wolfram Language in theory everything is an expression, and this can lead you to write functional-style programs which are easily thought of as a diagram, but that's not always the most natural way to express a computation. Sometimes you just need a little for loop. Consider calculating Fibonacci numbers. Start the sequence with 1, 1, ... then each element of the sequence is the sum of the previous two. Yes, you can write a recursive algorithm to do this, but most people just want to write a little for loop. In visX, you can do this (calculates the 6th Fibonacci number):
![embedded code][4]
I've tried to let you use blocks-and-links when that's the most natural thing (which is usually), and text-based code when that's better. Of course, you can mix them together however you want.
A second problem I've found with visual programming languages is that it can actually be much slower to use then writing out text, because you have to laboriously drag and drop every single block. Even simple algebraic expressions like
Sin[x]^2 + Cos[x]^2
2x^2 + 4x*y + 8y^2
would involve a lot of blocks because of all the Plus, Times, and Power blocks, as well as all the constants and symbols. With visX, you can enter Wolfram Language code snippets like those, and it will parse them and transform them into blocks which you can then insert into your diagram all at once and edit at will. This makes it much faster to get your idea onto the screen so that you can start evaluating it and developing it. I'm also working on the ability to take a visX block and give you back the Wolfram Language code that it represents.
The examples given here are simple, but of course you can use this interface for putting together a complex piece of code as well. I find it especially handy when building up a calculation with lots of intermediate results along the way, or to rapidly prototype an algorithm where I want to be able to easily switch the data flows around.
Does this project seem useful to anyone? I'd like to get some feedback - what do you think of it? Would you use it? For what?
If there's interest, I could do a small-scale alpha test in about a month from now.
More info at [visx.io][5].
-Nicholas Hoff
*edited to clarify block definitions and recursion*
[1]: http://visx.io
[2]: http://community.wolfram.com//c/portal/getImageAttachment?filename=pi_digits_without_chrome.png&userId=1124239
[3]: http://community.wolfram.com//c/portal/getImageAttachment?filename=pi_digits.png&userId=1124239
[4]: http://community.wolfram.com//c/portal/getImageAttachment?filename=embeded_wl.png&userId=1124239
[5]: http://visx.ioNicholas Hoff2017-06-20T10:02:52ZGet FinancialData price history?
http://community.wolfram.com/groups/-/m/t/1097940
Since 16 may 2017, why am I not able to get the price history of US stock market tickers? Please see output below! Has anything changed?
In[9]:= FinancialData["GE", {{2017, 1, 3}, {2017, 5, 15}}]
Out[9]= Missing["NotAvailable"]
In[10]:= FinancialData["IBM", {{2017, 1, 3}, {2017, 5, 15}}]
Out[10]= Missing["NotAvailable"]sridev ramaswamy2017-05-18T12:09:48Z[Mathematica-vs-R] Text analysis of Trump tweets
http://community.wolfram.com/groups/-/m/t/967299
## Introduction
This post is to proclaim the [MathematicaVsR at GitHub](https://github.com/antononcube/MathematicaVsR) project ["Text analysis of Trump tweets"](https://github.com/antononcube/MathematicaVsR/tree/master/Projects/TextAnalysisOfTrumpTweets) in which we compare Mathematica and R over text analyses of Twitter messages made by Donald Trump (and his staff) before the USA president elections in 2016.
This project follows and extends the exposition and analysis of the R-based blog post ["Text analysis of Trump's tweets confirms he writes only the (angrier) Android half"](http://varianceexplained.org/r/trump-tweets/) by David Robinson at [VarianceExplained.org](http://varianceexplained.org); see [1].
The blog post \[[1](http://varianceexplained.org/r/trump-tweets/)\] links to several sources that claim that during the election campaign Donald Trump tweeted from his Android phone and his campaign staff tweeted from an iPhone. The blog post [1] examines this hypothesis in a quantitative way (using various R packages.)
The hypothesis in question is well summarized with the tweet:
> Every non-hyperbolic tweet is from iPhone (his staff).
> Every hyperbolic tweet is from Android (from him). [pic.twitter.com/GWr6D8h5ed](pic.twitter.com/GWr6D8h5ed)
> -- Todd Vaziri (@tvaziri) August 6, 2016
This conjecture is fairly well supported by the following [mosaic plots](https://mathematicaforprediction.wordpress.com/2014/03/17/mosaic-plots-for-data-visualization/), \[[2](https://mathematicaforprediction.wordpress.com/2014/03/17/mosaic-plots-for-data-visualization/)\]:
[![TextAnalysisOfTrumpTweets-iPhone-MosaicPlot-Sentiment-Device](http://i.imgur.com/eKjxlTvm.png)](http://i.imgur.com/eKjxlTv.png) [![TextAnalysisOfTrumpTweets-iPhone-MosaicPlot-Device-Weekday-Sentiment](http://i.imgur.com/RMfuNNtm.png)](http://i.imgur.com/RMfuNNt.png)
We can see the that Twitter messages from iPhone are much more likely to be neutral, and the ones from Android are much more polarized. As
Christian Rudder (one of the founders of [OkCupid](https://www.okcupid.com), a dating website) explains in the chapter "Death by a Thousand Mehs" of the book ["Dataclysm"](http://dataclysm.org), \[[3](http://dataclysm.org)\], having a polarizing image (online persona) is as a very good strategy to engage online audience:
> [...] And the effect isn't small-being highly polarizing will in fact get you about 70 percent more messages. That means variance allows you to effectively jump several "leagues" up in the dating pecking order - [...]
(The mosaic plots above were made for the Mathematica-part of this project. Mosaic plots and weekday tags are not used in [1].)
### Links
- The Mathematica part: [PDF file](https://github.com/antononcube/MathematicaVsR/blob/master/Projects/TextAnalysisOfTrumpTweets/Mathematica/Text-analysis-of-Trump-tweets.pdf), [Markdown file](https://github.com/antononcube/MathematicaVsR/blob/master/Projects/TextAnalysisOfTrumpTweets/Mathematica/Text-analysis-of-Trump-tweets.md).
- The R part consists of :
- the blog post \[[1](http://varianceexplained.org/r/trump-tweets/)\], and
- the R-notebook given as [Markdown](https://github.com/antononcube/MathematicaVsR/blob/master/Projects/TextAnalysisOfTrumpTweets/R/TextAnalysisOfTrumpTweets.Rmd) and [HTML](https://cdn.rawgit.com/antononcube/MathematicaVsR/master/Projects/TextAnalysisOfTrumpTweets/R/TextAnalysisOfTrumpTweets.nb.html).
## Concrete steps
The Mathematica-part of this project does not follow closely the blog post [1]. After the ingestion of the data provided in [1], the Mathematica-part applies alternative algorithms to support and extend the analysis in [1].
The sections in the [R-part notebook](https://github.com/antononcube/MathematicaVsR/blob/master/Projects/TextAnalysisOfTrumpTweets/R/TextAnalysisOfTrumpTweets.Rmd) correspond to some -- not all -- of the sections in the Mathematica-part.
The following list of steps is for the Mathematica-part.
1. **Data ingestion**
- The blog post [1] shows how to do in R the ingestion of Twitter data of Donald Trump messages.
- That can be done in Mathematica too using the built-in function `ServiceConnect`,
but that is not necessary since [1] provides a link to the ingested data used [1]:
load(url("http://varianceexplained.org/files/trump_tweets_df.rda"))
- Which leads to the ingesting of an R data frame in the Mathematica-part using RLink.
2. **Adding tags**
- We have to extract device tags for the messages -- each message is associated with one of the tags "Android", "iPad", or "iPhone".
- Using the message time-stamps each message is associated with time tags corresponding to the creation time month, hour, weekday, etc.
- Here is summary of the data at this stage:
![enter image description here][1]
3. **Time series and time related distributions**
- We can make several types of time series plots for general insight and to support the main conjecture.
- Here is a Mathematica made plot for the same statistic computed in [1] that shows differences in tweet posting behavior:
![enter image description here][2]
- Here are distributions plots of tweets per weekday:
![enter image description here][3]
4. **Classification into sentiments and Facebook topics**
- Using the built-in classifiers of Mathematica each tweet message is associated with a sentiment tag and a Facebook topic tag.
- In [1] the results of this step are derived in several stages.
- Here is a mosaic plot for conditional probabilities of devices, topics, and sentiments:
![enter image description here][4]
5. **Device-word association rules**
- Using [Association rule learning](https://en.wikipedia.org/wiki/Association_rule_learning) device tags are associated with words in the tweets.
- In the Mathematica-part these associations rules are not needed for the sentiment analysis (because of the built-in classifiers.)
- The association rule mining is done mostly to support and extend the text analysis in [1] and, of course, for comparison purposes.
- Here is an example of derived association rules together with their most important measures:
![enter image description here][5]
In [1] the sentiments are derived from computed device-word associations, so in [1] the order of steps is 1-2-3-5-4. In Mathematica we do not need the steps 3 and 5 in order to get the sentiments in the 4th step.
## Comparison
Using Mathematica for sentiment analysis is much more direct because of the built-in classifiers.
The R-based blog post [1] uses heavily the "pipeline" operator `%>%` which is kind of a recent addition to R (and it is both fashionable and convenient to use it.) In Mathematica the related operators are `Postfix` (`//`), `Prefix` (`@`), `Infix` (`~~`), `Composition` (`@*`), and `RightComposition` (`/*`).
Making the time series plots with the R package "ggplot2" requires making special data frames. I am inclined to think that the Mathematica plotting of time series is more direct, but for this task the data wrangling codes in Mathematica and R are fairly comparable.
Generally speaking, the R package ["arules"](https://cran.r-project.org/web/packages/arules/index.html) -- used in this project for Associations rule learning -- is somewhat awkward to use:
- it is data frame centric, does not work directly with lists of lists, and
- requires the use of factors.
The Apriori implementation in ["arules"](https://cran.r-project.org/web/packages/arules/index.html) is much faster than the one in ["AprioriAlgorithm.m"](https://github.com/antononcube/MathematicaForPrediction/blob/master/AprioriAlgorithm.m) -- "arules" uses a more efficient algorithm [implemented in C](http://www.borgelt.net/fpm.html).
## References
\[1\] David Robinson, ["Text analysis of Trump's tweets confirms he writes only the (angrier) Android half"](http://varianceexplained.org/r/trump-tweets/), (2016), [VarianceExplained.org](http://varianceexplained.org).
\[2\] Anton Antonov, ["Mosaic plots for data visualization"](https://mathematicaforprediction.wordpress.com/2014/03/17/mosaic-plots-for-data-visualization/), (2014), [MathematicaForPrediction at WordPress](https://mathematicaforprediction.wordpress.com).
\[3\] Christian Rudder, [Dataclysm](http://dataclysm.org), Crown, 2014. ASIN: B00J1IQUX8 .
[1]: http://community.wolfram.com//c/portal/getImageAttachment?filename=yMtdphT.png&userId=143837
[2]: http://community.wolfram.com//c/portal/getImageAttachment?filename=oDv5Cm0.png&userId=143837
[3]: http://community.wolfram.com//c/portal/getImageAttachment?filename=UGMy4EW.png&userId=143837
[4]: http://community.wolfram.com//c/portal/getImageAttachment?filename=dMxSpHa.png&userId=143837
[5]: http://community.wolfram.com//c/portal/getImageAttachment?filename=dSSb4KD.png&userId=143837Anton Antonov2016-11-21T10:51:46ZSimulating the Universe (an alternative approach)
http://community.wolfram.com/groups/-/m/t/982238
This is the Community Post presenting the 2016 Wolfram Summer School Project of József Konczer, a Hungarian PhD student of theoretical physics, assisted by Todd Rowland.
The project notebook with the code is attached to this post.
## Fundamental theories in Physics ##
The Holy Grail of theoretical physics would be a theory, which could describe all known phenomena in every situation. This would be called the Theory of Everything ([ToE][1]). In present times, all approaches to a ToE candidate—like String/M theory, Loop quantum gravity ([and many others][2])—incorporate quantum mechanics from the beginning. This approach served surely very useful effective theories like the Standard Model itself, however this approach does not help to find an underlying deterministic theory from which quantum effects would emerge, like Einstein dreamed.
The standard argument for the unavoidability of quantum mechanics and uncertainty is the result of the [Bohr–Einstein debates][3] which was "won" by Bohr because the [EPR paradox][4] was tested by [measurements][5] (there is an ongoing test as well called the [Big Bell test][6]) and the result excludes local hidden variables. (The arguments can be found in details [here][7] and [here][8]). It has to be emphasized that in these arguments locality is a key assumption.
----------
One can ask, what kind of nonlocal theory can be constructed, which still have predictive power, and is not based of conspiracy of Nature? There are only a few researchers who post this question openly, one is them is Gerard ’t Hooft who published recently a [book][9] based on collected [papers][10]. His approach is conservative (from main stream point of view), and mainly suggests, that if one quantize time, then in same basis the unitary time evolution considered in quantum mechanics become a permutation operator between special basis elements or "beable states". However not every time evolution has this property and typically the interacting theories fail to fulfill the requirements. A more bold, however much less understood theory (or framework) is what Stephen Wolfram described in [NKS][11]. The brief summary of his ideas can be found in this [blog post][12]. The main idea here, is to find a simple data structure, for instance a sparse graph, a simple discrete dynamics governed by a replacement rule, an interpretation for this cellular automaton (CA) and then investigate if we can observe similar phenomena what we see in our Universe.
## Hints pointing toward the CA description ##
This is a speculative and highly subjective argumentation, however I think this blog post is an appropriate place to articulate my motives and do not stick to the objective style of research papers.
So first of all without going too deep into metaphysics I don't want to state things about Nature it self, I only talk about our description of it.
The first successful and highly useful description of Nature was Newtons description, which heavily used the idea of continuity of space and time. This idea proved to be useful in description of solids, liquids and gases as well. However some ideas became so useful and popular, that we forgot that all of them is only our description and not Nature its self. Quantum effects and effects related to relativity reminded us, that under non standard circumstances old descriptions can fail. As I see, quantum effects have two message for us. The first is that quantities could be and should be described by discrete variables, and secondly that under a certain level systems can not be observed without disturbance. If we take into account that space and time even as we observe them are influenced by these quantized quantities, it is straightforward to deduce, that space and time should be quantized as well.
Before these findings in physics probability theory was developed. First it was used to analyze gambling situations where one don't know every information about the system. From this point of view it is clearly a strategy to manage our ignorance toward some details in a deterministic situation. However after some point physicists started to use probabilities as they were part of the phenomena, and not only our clever way to make inference from systems where we do not know every detail. Many physicists—including myself—where educated in the spirit of frequentist [interpretation of probability theory][13], which is useful in some cases but as I think, prevents some questions to ask. I think this promotion of probability to an objective property contributed to the interpretation of quantum mechanics as well. As Jayns wrote in his [book][14]:
> In current quantum theory, probabilities express our own ignorance due to our failure
to search for the real causes of physical phenomena; and, worse, our failure even to think
seriously about the problem. This ignorance may be unavoidable in practice, but in our
present state of knowledge we do not know whether it is unavoidable in principle; the
‘central dogma’ simply asserts this, and draws the conclusion that belief in causes, and
searching for them, is philosophically naive. If everybody accepted this and abided by it,
no further advances in understanding of physical law would ever be made; indeed, no such
advance has been made since the 1927 Solvay Congress in which this mentality became
solidified into physics. But it seems to us that this attitude places a premium on stupidity;
to lack the ingenuity to think of a rational physical explanation is to support the supernatural
view.
However even if one thinks, that theories incorporating quantum mechanics are "only" effective theories, probably we can get intuitions from them. There is a [recent result][15] from [AdS/CFT][16] correspondence as an example for the [EPR=ER][17] conjecture. And a connecting [paper][18] of Leonard Susskind, concluding that:
> What all of this suggests to me, and what I want to suggest to you, is that quantum mechanics and gravity are far more tightly related than we (or at least I) had ever imagined. The essential nonlocalities of quantum mechanics (the need for instantaneous communication in order to classically simulate entanglement) parallels the nonlocal potentialities of general relativity: ER=EPR.
The cited papers state, that spacetime structure can be understood as a net of entanglements, however maybe the statement can be reversed, and say that the phenomena of entanglement can be described by a nonlocal spacetime structure.
Among the mentioned hints, the existing theoretical constructions can help to find an appropriate interpretations as well. For example it can happen, that to describe our seemingly 3 dimensional space one has to describe space with higher effective dimensionality and interpret the entangled parts not just as connected regions, but as global structures in the extra dimensions.
After taking hints from existing theoretical constructions one can investigate, what kind of phenomena can appear in simple CA-s which mimic some parts of Nature.
Perhaps the most well known CA is Conway's [Game of Life][19] this is a 2D Cellular automaton where localized objects (called [spaceships][20] or gliders) can propagate, and can interact with each other. This behavior can remind us to particles, however the built in rectangular structure is reflected on the properties of spaceships, and there are no nonlocal connections between these object because of the locality of the rule.
Both problems can be solved, if one tries to construct a CA without built in topology. (This construction will be described in detail.)
Another nice feature of special CA-s called substitution systems, is that for a structure living in the automaton, can not observe the absolute number of steps, or other structures beside him, only the causal net of implemented changes can be recognized from inside. This feature unites the relative space and time for observers or structures inside the system. It can remind us to causal network description of General Relativity.
A third hint from CA point of view is the typical appearance of complex behavior, which can lead to an effective probabilistic description of the system with a higher symmetry what the framework originally allowed. (For example CA description of flows) From disorder new effective order can emerge possible with higher symmetry.
The conjectured computational irreducibility of CA would replace the promised "free will" possibility of quantum mechanics with a different but in some sense similar concept. In this framework the faith of the Universe would be determined, but even an observer outside the system—God if one wishes—could not know the consequences only by letting the simulation run up to the desired point.
Furthermore a multiway CA dynamics is compatible with the many world interpretation of quantum mechanics, with the advantage, that the splitting points of histories are not observer dependent. In this framework the overall dynamics is deterministic, however structures living always on one branch of the evolution will witness an unavoidable true random behavior from the inside point of view.
## Nature and our understanding of it ##
Of course it would be an arrogant attitude to force Nature to fulfill our philosophical expectations, however one can imagine how our description of it can change during time.
There are several situations what one can imagine:
- There is a deterministic description which is valid in any situation (which can appear in our Universe)
- This can be totally discreet
- Or it can be continuous partially or in whole
- It can be, that after some point a truly random (or at least appearing to us) mechanism will appear, which can not be unfolded
- Or it can happen that construction of laws to describe Nature will never come to an end, and our understanding of reality will based on infinite set of possibly deterministic rules.
- And of course it can happen that something unexpected will turn out.
Without favoring any of the listed cases above, my main point is that the very first situation, namely that our universe can be described as a deterministic discrete system is not totally excluded. And the most natural way to understand it can be a CA description.
## CA description candidate for our Universe ##
To have a CA description, one has to choose a data structure, a dynamics and an interpretation. (It has to be pointed out, that any CA can be simulated on another Turing complete CA with different interpretation of the states. Because of that any CA description is highly non unique. However one can try to choose a description which has the "simplest" interpretation.)
For a fundamental CA description one can choose simple graphs as data structure. This seems as a natural choice, because of its simplicity and because of its non fixed topology.
To have a chance to describe deterministic dynamics on this data structure, we even restrict the degree of nodes on the graph. One can try to find the threshold of complexity of the CA, and it seams that cubic graphs can already produce complicated enough structures. So one can set the data structure to a simple cubic graph.
The next step is to define an appropriate dynamics on this data structure. A natural approach is to introduce subgraph replacement rules, which means the following: If one finds a given subgraph pattern $H_1$ in the present graph $G$, then replace it with a compatible new graph $H_2$. It sounds simple, however there are many details which have to be fulfilled to get a dynamics with desired properties.
I mention here two properties of the patterns, which seems to be essential to get a substitution systems which generates a complex behavior and appears completely deterministic from the inside without the specification of the order of replacements in the system.
The first one is a **non overlapping property** of the pattern graph(s) $ H_1 $. This means, that $H_1$ has a special structure such as there is no cubic graph $G$, where two subgraphs can be found which are isomorphic to $H_1$ and have nonzero intersection. The following rule does not fulfill this requirement because there is a cubic graph, where two intersecting copies of $H_1$ can be found
![overlapping rule][21]
![Interesting patterns][22]
The second requirement is **non triviality**, which gives a constraint for $H_2$. In this case we wish to have $H_2$, that there exists a cubic graph $G$, which contains a subgraph $H_1$, where after the replacement $H_1 \rightarrow H_2$ there can be found a new pattern $H_1$, which intersects with $H_2$ but has parts outside $H_2$ as well. (Without this property only self similar or frozen (where there is no more pattern which can be changed) graphs can be generated from finite initial graphs.) The pictures show visually the requirement:
![enter image description here][23]
![enter image description here][24]
After setting some rule, which fulfill these requirements, we have to find an initial graph, apply the rule many times and find an interpretation for the result. It has to be pointed out, that the actual graph structure at a given step can not be observed from an inside point of view. What an inner observer, or a structure can explore is the causal structure which is generated by the replacements. (For details see [NKS chapter 9, section 13][25] )
This is similar to the [causal set program][26].
So a natural way of interpretation of the emerging causal net is that it is a discretization of some kind of spacetime. And local propagating disturbances relative to the overall average structure are particle like excitations, which can have nonlocal connections relative to the average large scale structure. However from AdS/CFT insights it can happen, that we have to interpret particles for example as global structures in a higher dimensional bulk spacetime, which have ends on a boundary-like smaller dimensional surface.
## My contribution to the project ##
During the 3 weeks of 2016 Wolfram summer school I set a framework where the steps of a substitution are precisely defined, and in which the substitutions can be effectively performed even for relatively big graphs. Furthermore I tested a numerical approach to measure the effective dimensionality of the emergent graph structure after sufficiently many steps.
Unfortunately I could not test this framework with rules which could give complex, deterministic behavior, so I could benchmark this machinery on a simple, point to triangle rule, which gives a fractal-like structure. If we interpret this graph as space, then this simple dynamics results a $D=\log(3)/\log(2)=1.58$ dimensional fractal space.
Here is a graph of the generated fractal Universe after 100 steps, started from a tetrahedron:
![Generated fractal Universe after 100 steps, started from a tetrahedron][27]
And the neighborhood structure of this space:
![Local structure in the fractal Universe][28]
## Further directions ##
This project to find deterministic CA description for our Universe is in its infant stage. The framework is more or less set, but it needs tremendous work to investigate possible dynamics and analyze the results of simulations.
An outline of a huge project would be the following:
- List the possible rules, which fulfill the non overlapping and non trivial conditions
- Investigate their long term behavior starting from simple initial graphs
- Find quantities and a method of their measurement which can be determined from generated causal graphs
- Find fixed points of the dynamics which preserve long scale dimensionality and possibly other quantities
- List and investigate local disturbances near these fixed points
- After setting an interpretation analyze the particle-like structures (gliders of this dynamics)
- Develop an effective field theory which can describe an effective behavior of the system near to the fixed points
- Match these field theories with the Standard Model of particle physics
- Find out new predictions of the derived effective field theories, which can be tested by measurements
## Conclusion ##
In my project I could set a framework and show a trivial example for a deterministic graph evolution model.
During the summer school I was not fortunate enough to find dynamics which produce complex behavior, however to find an appropriate rule seems reachable in the near future. Hopefully a dynamics producing complex topology would be interesting enough to inspire much more people and after some point a serious investigation of the field could be started.
I think personally, that proving or even disproving that this framework to describe Nature can be worked out is an extremely interesting challenge and deserves further theoretical research.
In the end I would like to thank my mentor Todd Rowland, and the whole Wolfram summer school team for the organization and I really hope that there will be a continuation of this project.
Last but not least I thank for all the summer school participants for great discussions and a lifelong experience!
![enter image description here][29]
----------
## Further comments ##
I try to collect here some useful comments of my friends and collegues, who kindly read my post, and responded in person:
There is a concept named [Digital physics][30] which has a much longer history what I suggested, and probably the earliest pioneer of the field was Konrad Zuse. Fortunatelly his thesis—[Calculating Space][31] or “Rechnender Raum”—is now translated into English and has a modern, LaTeX typesetting.
Beside NKS there is another relevant book, which can serve as an extended list of references and valuable material in its own, written by Andrew Ilachinski with the title [Cellular Automata A Discrete Universe][32].
There is an ongoing "mini revolution" in the description of AdS/CFT based on [Tensor Networks][33]. The original paper on the topic can be found [here][34].
[1]: https://en.wikipedia.org/wiki/Theory_of_everything
[2]: https://www.quantamagazine.org/20150803-physics-theories-map/
[3]: https://en.wikipedia.org/wiki/Bohr%E2%80%93Einstein_debates
[4]: https://en.wikipedia.org/wiki/EPR_paradox
[5]: https://arxiv.org/abs/1508.05949
[6]: http://thebigbelltest.org/#/science?l=EN
[7]: http://www.cambridge.org/catalogue/catalogue.asp?isbn=9780521818629
[8]: http://www.springer.com/in/book/9783662137352
[9]: http://www.springer.com/us/book/9783319412849
[10]: https://arxiv.org/abs/1405.1548
[11]: http://www.wolframscience.com/
[12]: http://blog.stephenwolfram.com/2015/12/what-is-spacetime-really/
[13]: https://plato.stanford.edu/entries/probability-interpret/
[14]: http://www.cambridge.org/catalogue/catalogue.asp?isbn=0521592712
[15]: http://www.nature.com/news/the-quantum-source-of-space-time-1.18797
[16]: https://en.wikipedia.org/wiki/AdS/CFT_correspondence
[17]: https://en.wikipedia.org/wiki/ER=EPR
[18]: https://arxiv.org/abs/1604.02589
[19]: https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life
[20]: http://conwaylife.com/wiki/Category:Spaceships
[21]: http://community.wolfram.com//c/portal/getImageAttachment?filename=H1H2.png&userId=981213
[22]: http://community.wolfram.com//c/portal/getImageAttachment?filename=GH1H1.png&userId=981213
[23]: http://community.wolfram.com//c/portal/getImageAttachment?filename=H1H2_2.png&userId=981213
[24]: http://community.wolfram.com//c/portal/getImageAttachment?filename=GG.png&userId=981213
[25]: http://www.wolframscience.com/nksonline/section-9.13
[26]: https://en.wikipedia.org/wiki/Causal_sets
[27]: http://community.wolfram.com//c/portal/getImageAttachment?filename=PresentationTemplate_KJ_2.png&userId=981213
[28]: http://community.wolfram.com//c/portal/getImageAttachment?filename=PresentationTemplate_KJ_3.png&userId=981213
[29]: http://community.wolfram.com//c/portal/getImageAttachment?filename=vladstudio_higgs_boson_fluo_800x600_signed.jpg&userId=981213
[30]: https://en.wikipedia.org/wiki/Digital_physics
[31]: http://www.mathrix.org/zenil/ZuseCalculatingSpace-GermanZenil.pdf
[32]: http://www.worldscientific.com/worldscibooks/10.1142/4702
[33]: https://arxiv.org/abs/1306.2164
[34]: https://arxiv.org/abs/0905.1317Jozsef Konczer2016-12-16T07:22:29ZPage breaks with writing assistant
http://community.wolfram.com/groups/-/m/t/1121632
I have a problem that I hope someone has a solution for. I'm using Writing Assistant to write a book in Mathematica. After selecting the page break button on Writing Assistant, I find that Mathematica inserts two page breaks at the start and end of the (hidden) cell. So when I print the document, it throws a whole blank page.
How do I ensure that only one page break is inserted?Jonathan Kinlay2017-06-16T17:26:25ZBiokmod 5.4 A toolbox for biokinetic modeling to free download
http://community.wolfram.com/groups/-/m/t/615970
Biokmod 5.4: A Mathematica toolbox for solving system of differential equations, fitting coefficients, convolution, and more, with application for modeling Linear and Nonlinear Biokinetic Systems. It included the current ICRP biokinetic models. It can be applied in pharmacokinetic, internal dosimetry, bioassay evaluations, nuclear medicine and more. This toolbox consist in Mathematica packages and tutorials .
It can be downloaded here:
http://diarium.usal.es/guillermo/biokmod/ .
Additional information:
http://diarium.usal.es/guillermo/files/2015/11/SummaryBiokmod54.pdf
BIOKMODWEB is a web application developped with webMathematica and Biokmod available at:
http://www3.enusa.es/webMathematica/Public/biokmod.html
GuillermoGuillermo Sanchez2015-11-17T21:11:37ZUsing Mathematica in Teaching Differential Equations
http://community.wolfram.com/groups/-/m/t/1124581
I am Brian Winkel, Professor Emeritus (civilian) United States Military Academy, West Point NY USA. I wish to contact colleagues who teach differential equations using or considering using modeling and technology.
I am currently the Director of SIMIODE-Systemic Initiative for Modeling Investigations and Opportunities with Differential Equations, an organization of teachers and students interested in teaching and learning differential equations by using modeling and technology throughout the process. Visit us at www.simiode.org.
We have designed a Student Competition Using Differential Equation Modeling – SCUDEM for April 2018 (see www.simiode.org/scudem for complete details) and invite schools to host SCUDEM (we have some 60 teams in the US already)and to consider sponsoring a team.
SIMIODE is a 501(c)3 organization and all its resources are freely available under the most generous Creative Commons license. Visit us at www.simiode.org and join. All is FREE at SIMIODE.
![SCUDEM 2018 local site locations in the United States as of 16 June 2017][1]
[1]: http://community.wolfram.com//c/portal/getImageAttachment?filename=SCUDEMSitesMap.jpg&userId=1124219Brian Winkel2017-06-19T23:12:18ZCDF templates for creating textbook notes?
http://community.wolfram.com/groups/-/m/t/1109137
I purchased the license for Mathematica 11 and got it installed this week, So far I'm having a great learning experience with it. One of the most highlights of purchasing Mathematica was CDF documentation . I would like to create notes on textbooks by using the exact same feature as shown on this website: [Link][1].
Is this a template created by Mathematica, how do I go about creating a CDF such as this for a textbook I am learning.
[1]: http://www.wolfram.com/cdf/uses-examples/textbooks.htmlAbhilash Sukumari2017-05-26T19:18:21ZGive input in Robotica Mathematica for dynamic inputs?
http://community.wolfram.com/groups/-/m/t/1120688
How to give input in robotica mathematica for dynamic inputs. like mass inertia?Purnendu Tripathi2017-06-15T11:36:18ZWhere is the world map in MMA from? How to cite it?
http://community.wolfram.com/groups/-/m/t/1121780
I use GeoListPlot[] function to generate a beautiful map and write a paper. I cite the link http://www.wolfram.com/mathematica/ in my paper, but the editor ask me to cite in the figure legend properly, according to Wolfram Mathematica's tems of use (http://www.wolfram.com/legal/agreements/wolfram-mathematica.html). Please where is the world map from? Thank you.tueqier tueqier2017-06-17T07:51:28Z