Examples from Mathematica Stack Exchange

This notebook contains examples from Mathematica Stack Exchange applied to Symata.

Disclaimer to avoid any possible confusion.

Neither Symata nor the Symata language are affiliated in any way with Mathematica and/or the Wolfram language. Symata is an open source project. Mathematica and Wolfram language are software products developed and licensed by WRI.

In [1]:
using Symata
In [2]:
FloatFormat(Short);

Determining all possible traversals of a tree

The following example example is from L. Shifrin.

C is a tree:

In [3]:
C = [a,[[a1,[a12,b12,c12]],[b2,[a22,b22,c22]],[c3,[a32,b32,c32,d32]]]];
In [4]:
trav(tree_List) := Flatten(trav([], tree), 1)
trav(accum_List, [x_, y_List]) := Map(yy -> trav([accum, x], yy), y)
trav(x_,y_) := Flatten([x,y])
In [5]:
trav(C)
Out[5]:
$$ \left[ \left[ a,a1,a12 \right] , \left[ a,a1,b12 \right] , \left[ a,a1,c12 \right] , \left[ a,b2,a22 \right] , \left[ a,b2,b22 \right] , \left[ a,b2,c22 \right] , \left[ a,c3,a32 \right] , \left[ a,c3,b32 \right] , \left[ a,c3,c32 \right] , \left[ a,c3,d32 \right] \right] $$

What are the use cases for different scoping constructs?

In this example, Module creates a closure. We want to use big integers, so we use big"1" for one of the values.

This example is also by L. Shifrin

In [6]:
Module([prev, prevprev, this],
begin 
    reset() := (prev = big"1"; prevprev = 1); 
    reset(); 
    nextFib() := (this = prev + prevprev; prevprev = prev; prev = this)
end
);
In [7]:
reset()

a = Table(nextFib(),[1000]);

a[-1]
Out[7]:
$$ 113796925398360272257523782552224175572745930353730513145086634176691092536145985470146129334641866902783673042322088625863396052888690096969577173696370562180400527049497109023054114771394568040040412172632376 $$

Return all free symbols in an expression

Below is another example from L. Shifrin. allsyms returns all free symbols in expr.

In [8]:
ClearAll(a,b)

allsyms(expr_) := Cases(expr , s_Symbol => HoldComplete(s),[0,Infinity])
In [9]:
allsyms(a + b * (1 - x))
Out[9]:
$$ \left[ \text{HoldComplete} \! \left( a \right) ,\text{HoldComplete} \! \left( b \right) ,\text{HoldComplete} \! \left( x \right) \right] $$

The Power function returns the principal root, not necessarily a real root.

In [10]:
[(-8)^(1/3), (-8.0)^(1/3)]
Out[10]:
$$ \left[ 2 \ \left( \left( -1 \right) ^{\frac{1}{3}} \right) ,1. + 1.73205\mathbb{i} \right] $$

CubeRoot and Surd give real roots

In [11]:
[CubeRoot(-8), Surd(-32,5)]
Out[11]:
$$ \left[ -2,-2 \right] $$

Surd returns unevaluated if the root is even.

In [12]:
Surd(-8,4)
┌ Warning: Surd::noneg: Surd is not defined for even roots of negative values.
└ @ Symata /home/lapeyre/.julia/dev/Symata/src/wrapout.jl:29
Out[12]:
$$ \text{Surd} \! \left( \left( -8 \right) ,4 \right) $$

or complex

In [13]:
Surd(I,3)
┌ Warning: Surd::preal: The parameter I should be real valued
└ @ Symata /home/lapeyre/.julia/dev/Symata/src/wrapout.jl:29
Out[13]:
$$ \text{Surd} \! \left( \mathbb{i},3 \right) $$

Version and date

In [14]:
VersionInfo()
Symata version     0.4.6
Julia version      1.6.0-DEV.58
Python version     3.8.3
SymPy version      1.5.1
In [15]:
InputForm(Now())
Out[15]:
2020-05-29T22:44:02.976