{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Examples from Mathematica Stack Exchange\n", "\n", "This notebook contains examples from [Mathematica Stack Exchange](http://mathematica.stackexchange.com/) applied to Symata.\n", "\n", "Disclaimer to avoid any possible confusion.\n", "\n", "Neither Symata nor the Symata language are affiliated in any way with [Mathematica and/or the Wolfram language](http://wolfram.com). Symata is an open source project. Mathematica and Wolfram language are software products developed and licensed by [WRI](http://wolfram.com)." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "using Symata" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "FloatFormat(Short);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### [Determining all possible traversals of a tree](http://mathematica.stackexchange.com/questions/5753/determining-all-possible-traversals-of-a-tree)\n", "\n", "The [following example](http://mathematica.stackexchange.com/questions/5753/determining-all-possible-traversals-of-a-tree/5756#5756) example is from L. Shifrin.\n", "\n", "`C` is a tree:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "C = [a,[[a1,[a12,b12,c12]],[b2,[a22,b22,c22]],[c3,[a32,b32,c32,d32]]]];" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "trav(tree_List) := Flatten(trav([], tree), 1)\n", "trav(accum_List, [x_, y_List]) := Map(yy -> trav([accum, x], yy), y)\n", "trav(x_,y_) := Flatten([x,y])" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/latex": [ "$$ \\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] $$" ], "text/plain": [ "L\"$$ \\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] $$\"" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "trav(C)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### [What are the use cases for different scoping constructs?](http://mathematica.stackexchange.com/questions/559/what-are-the-use-cases-for-different-scoping-constructs/)\n", "\n", " In this example, `Module` creates a *closure*. We want to use big integers, so we use `big\"1\"` for one of the values.\n", "\n", "[This example](http://mathematica.stackexchange.com/questions/559/what-are-the-use-cases-for-different-scoping-constructs/569#569) is also by L. Shifrin\n" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "Module([prev, prevprev, this],\n", "begin \n", " reset() := (prev = big\"1\"; prevprev = 1); \n", " reset(); \n", " nextFib() := (this = prev + prevprev; prevprev = prev; prev = this)\n", "end\n", ");" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ 113796925398360272257523782552224175572745930353730513145086634176691092536145985470146129334641866902783673042322088625863396052888690096969577173696370562180400527049497109023054114771394568040040412172632376 $$" ], "text/plain": [ "L\"$$ 113796925398360272257523782552224175572745930353730513145086634176691092536145985470146129334641866902783673042322088625863396052888690096969577173696370562180400527049497109023054114771394568040040412172632376 $$\"" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "reset()\n", "\n", "a = Table(nextFib(),[1000]);\n", "\n", "a[-1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Return all free symbols in an expression\n", "\n", "Below is another example from L. Shifrin. `allsyms` returns all free symbols in `expr`." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "ClearAll(a,b)\n", "\n", "allsyms(expr_) := Cases(expr , s_Symbol => HoldComplete(s),[0,Infinity])" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/latex": [ "$$ \\left[ \\text{HoldComplete} \\! \\left( a \\right) ,\\text{HoldComplete} \\! \\left( b \\right) ,\\text{HoldComplete} \\! \\left( x \\right) \\right] $$" ], "text/plain": [ "L\"$$ \\left[ \\text{HoldComplete} \\! \\left( a \\right) ,\\text{HoldComplete} \\! \\left( b \\right) ,\\text{HoldComplete} \\! \\left( x \\right) \\right] $$\"" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "allsyms(a + b * (1 - x))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### [Finding real roots of negative numbers (for example $\\sqrt[3]{-8}$)](http://mathematica.stackexchange.com/questions/3886/finding-real-roots-of-negative-numbers-for-example-sqrt3-8)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `Power` function returns the principal root, not necessarily a real root." ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "scrolled": false }, "outputs": [ { "data": { "text/latex": [ "$$ \\left[ 2 \\ \\left( \\left( -1 \\right) ^{\\frac{1}{3}} \\right) ,1. + 1.73205\\mathbb{i} \\right] $$" ], "text/plain": [ "L\"$$ \\left[ 2 \\ \\left( \\left( -1 \\right) ^{\\frac{1}{3}} \\right) ,1. + 1.73205\\mathbb{i} \\right] $$\"" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[(-8)^(1/3), (-8.0)^(1/3)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`CubeRoot` and `Surd` give real roots" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$ \\left[ -2,-2 \\right] $$" ], "text/plain": [ "L\"$$ \\left[ -2,-2 \\right] $$\"" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[CubeRoot(-8), Surd(-32,5)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`Surd` returns unevaluated if the root is even." ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "scrolled": true }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "┌ Warning: Surd::noneg: Surd is not defined for even roots of negative values.\n", "└ @ Symata /home/lapeyre/.julia/dev/Symata/src/wrapout.jl:29\n" ] }, { "data": { "text/latex": [ "$$ \\text{Surd} \\! \\left( \\left( -8 \\right) ,4 \\right) $$" ], "text/plain": [ "L\"$$ \\text{Surd} \\! \\left( \\left( -8 \\right) ,4 \\right) $$\"" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Surd(-8,4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "or complex" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "┌ Warning: Surd::preal: The parameter I should be real valued\n", "└ @ Symata /home/lapeyre/.julia/dev/Symata/src/wrapout.jl:29\n" ] }, { "data": { "text/latex": [ "$$ \\text{Surd} \\! \\left( \\mathbb{i},3 \\right) $$" ], "text/plain": [ "L\"$$ \\text{Surd} \\! \\left( \\mathbb{i},3 \\right) $$\"" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Surd(I,3)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "### Version and date" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Symata version 0.4.6\n", "Julia version 1.6.0-DEV.58\n", "Python version 3.8.3\n", "SymPy version 1.5.1\n" ] } ], "source": [ "VersionInfo()" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2020-05-29T22:44:02.976" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "InputForm(Now())" ] } ], "metadata": { "kernelspec": { "display_name": "Julia 1.6.0-DEV", "language": "julia", "name": "julia-1.6" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.6.0" } }, "nbformat": 4, "nbformat_minor": 1 }