Skip to content

Commit

Permalink
doc for github
Browse files Browse the repository at this point in the history
  • Loading branch information
vmchale committed Aug 4, 2024
1 parent 27ca067 commit 4f03e67
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 8 deletions.
91 changes: 85 additions & 6 deletions doc/apple-by-example.html
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,10 @@
margin: 0 0.8em 0.2em -1.6em;
vertical-align: middle;
}
.display.math{display: block; text-align: center; margin: 0.5rem auto;}
</style>
<script
src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml-full.js"
type="text/javascript"></script>
</head>
<body>
<header id="title-block-header">
Expand All @@ -194,6 +196,8 @@ <h1 class="title">Apple by Example</h1>
<li><a href="#array-literals" id="toc-array-literals">Array
Literals</a></li>
<li><a href="#reverse" id="toc-reverse">Reverse</a></li>
<li><a href="#outer-product" id="toc-outer-product">Outer
Product</a></li>
</ul></li>
<li><a href="#examples" id="toc-examples">Examples</a>
<ul>
Expand All @@ -213,6 +217,17 @@ <h1 class="title">Apple by Example</h1>
<ul>
<li><a href="#filter" id="toc-filter">Filter</a></li>
</ul></li>
<li><a href="#numerics" id="toc-numerics">Numerics</a>
<ul>
<li><a href="#arithmetic-geometric-mean"
id="toc-arithmetic-geometric-mean">Arithmetic-Geometric Mean</a></li>
<li><a href="#hypergeometric-function"
id="toc-hypergeometric-function">Hypergeometric Function</a></li>
</ul></li>
<li><a href="#geography" id="toc-geography">Geography</a>
<ul>
<li><a href="#mercator" id="toc-mercator">Mercator</a></li>
</ul></li>
<li><a href="#number-theory" id="toc-number-theory">Number Theory</a>
<ul>
<li><a href="#primality-check" id="toc-primality-check">Primality
Expand All @@ -229,7 +244,7 @@ <h2 id="installation">Installation</h2>
<p>The REPL and typechecker are available on the <a
href="https://github.com/vmchale/apple/releases">release page</a>.</p>
<h3 id="libraries">Libraries</h3>
<p>To install the libraries, you will can use <a
<p>To install the libraries, you can use <a
href="https://www.haskell.org/ghcup/">ghcup</a> to install cabal and
GHC.</p>
<p>Then:</p>
Expand Down Expand Up @@ -303,6 +318,29 @@ <h2 id="reverse">Reverse</h2>
[ [2, 4]
, [1, 0]
, [0, 1] ]</code></pre>
<h2 id="outer-product">Outer Product</h2>
<p>The outer product <code></code> creates a table by applying some
function.</p>
<p>It has type</p>
<pre><code> &gt; :ty \f.\x.\y. x f⊗ y
(a → b → c) → Arr sh0 a → Arr sh1 b → Arr (sh0 ⧺ sh1) c</code></pre>
<pre><code> &gt; (frange 0 9 10) (*)⊗ (frange 0 9 10)
[ [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
, [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]
, [0.0, 2.0, 4.0, 6.0, 8.0, 10.0, 12.0, 14.0, 16.0, 18.0]
, [0.0, 3.0, 6.0, 9.0, 12.0, 15.0, 18.0, 21.0, 24.0, 27.0]
, [0.0, 4.0, 8.0, 12.0, 16.0, 20.0, 24.0, 28.0, 32.0, 36.0]
, [0.0, 5.0, 10.0, 15.0, 20.0, 25.0, 30.0, 35.0, 40.0, 45.0]
, [0.0, 6.0, 12.0, 18.0, 24.0, 30.0, 36.0, 42.0, 48.0, 54.0]
, [0.0, 7.0, 14.0, 21.0, 28.0, 35.0, 42.0, 49.0, 56.0, 63.0]
, [0.0, 8.0, 16.0, 24.0, 32.0, 40.0, 48.0, 56.0, 64.0, 72.0]
, [0.0, 9.0, 18.0, 27.0, 36.0, 45.0, 54.0, 63.0, 72.0, 81.0] ]</code></pre>
<pre><code> &gt; (frange 0 4 5) [(x,y)]⊗ (frange 0 4 5)
[ [(0.0*0.0), (0.0*1.0), (0.0*2.0), (0.0*3.0), (0.0*4.0)]
, [(1.0*0.0), (1.0*1.0), (1.0*2.0), (1.0*3.0), (1.0*4.0)]
, [(2.0*0.0), (2.0*1.0), (2.0*2.0), (2.0*3.0), (2.0*4.0)]
, [(3.0*0.0), (3.0*1.0), (3.0*2.0), (3.0*3.0), (3.0*4.0)]
, [(4.0*0.0), (4.0*1.0), (4.0*2.0), (4.0*3.0), (4.0*4.0)] ]</code></pre>
<h1 id="examples">Examples</h1>
<h2 id="dot-product">Dot Product</h2>
<pre><code>[(+)/ ((*)`((x::Vec n float)) y)]</code></pre>
Expand Down Expand Up @@ -333,8 +371,8 @@ <h2 id="rising-factorial">Rising Factorial</h2>
<p>The <a
href="https://mathworld.wolfram.com/RisingFactorial.html">rising
factorial</a> is defined as:</p>
<p><span
class="math display"><em>x</em><sup>(<em>n</em>)</sup> = <em>x</em>(<em>x</em> + 1)⋯(<em>x</em> + <em>n</em> − 1)</span></p>
<p><span class="math display">\[ x^{(n)} = x(x+1)\cdots
(x+n-1)\]</span></p>
<p>In apple this is</p>
<pre><code>[(*)/ₒ 1 (⍳ x (x+y-1) 1)]</code></pre>
<p><code>/ₒ</code> is a ternary operator, fold with seed.</p>
Expand All @@ -355,13 +393,54 @@ <h2 id="array">Array</h2>
<pre><code>[}.\`7 x]</code></pre>
<h3 id="filter">Filter</h3>
<pre><code>\p.\xs. (xs˙)&#39;p⩪xs</code></pre>
<h2 id="numerics">Numerics</h2>
<h3 id="arithmetic-geometric-mean">Arithmetic-Geometric Mean</h3>
<pre><code>λx.λy.([{a⟜x-&gt;1;g⟜x-&gt;2;((a+g)%2,√(a*g))}]^:6 (x,y))-&gt;1</code></pre>
<p>Thence the <a
href="https://en.wikipedia.org/wiki/Elliptic_integral#Complete_elliptic_integral_of_the_first_kind">complete
elliptic integral of the first kind</a>:</p>
<pre><code>λk.
{
agm ← λx.λy.([{a⟜x-&gt;1;g⟜x-&gt;2;((a+g)%2,√(a*g))}]^:6 (x,y))-&gt;1;
𝜋%(2*agm 1 (√(1-k^2)))
}</code></pre>
<p>Gauss’ approximation of the logarithm:</p>
<pre><code>λm.λx.
{
amgm ← λx.λy.([{a⟜x-&gt;1;g⟜x-&gt;2;((a+g)%2,√(a*g))}]^:15 (x,y))-&gt;1;
-- m&gt;2
𝜋%(2*amgm 1 (0.5^(m-2)%x))-ℝ m*0.6931471805599453
}</code></pre>
<h3 id="hypergeometric-function">Hypergeometric Function</h3>
<p><span class="math display">\[
{}_pF_q(a_1,\ldots,a_p;b_1,\ldots,b_q;x) = \sum_{n=0}^\infty
\frac{(a_1)_n\ldots (a_p)_n}{(b_1)_n\ldots
(b_q)_n}\frac{x^n}{n!}\]</span></p>
<p>where <span class="math inline">\((x)_n\)</span> is the <a
href="#rising-factorial">rising factorial</a> above.</p>
<pre><code>λa.λb.λz.
{
rf ← [(*)/ₒ 1 (𝒻 x (x+y-1) (⌊y))]; fact ← rf 1;
Σ ← λN.λa. (+)/ₒ 0 (a&#39;(⍳ 0 N 1)); Π ← [(*)/x];
Σ 30 (λn. {nn⟜ℝ n; (Π ((λa.rf a nn)&#39;a)%Π((λb. rf b nn)&#39;b))*(z^n%fact nn)})
}</code></pre>
<p>We can use the REPL to inspect the type:</p>
<pre><code> &gt; :yank H math/hypergeometric.🍏
&gt; :ty H
Vec (i + 1) float → Vec (i + 1) float → float → float</code></pre>
<h2 id="geography">Geography</h2>
<h3 id="mercator">Mercator</h3>
<p><span class="math display">\[ x \mapsto \lambda - \lambda_0 \]</span>
<span class="math display">\[ y \mapsto \frac{1}{2}
\log{\left(\frac{1+\sin \phi}{1-\sin \phi}\right)} \]</span></p>
<pre><code>\𝜆₀.\𝜑.\𝜆.{a⟜sin.𝜑;(𝜆-𝜆₀,(_.((1+a)%(1-a)))%2)}</code></pre>
<h2 id="number-theory">Number Theory</h2>
<h3 id="primality-check">Primality Check</h3>
<pre><code>λn.¬((∨)/ₒ #f ([(n|x)=0]&#39;(⍳ 2 (⌊(√(ℝn))) 1)))</code></pre>
<h3 id="radical">Radical</h3>
<p>Compute the radical of an integer <span
class="math inline"><em>n</em></span>, <span
class="math inline"><sub><em>p</em>|<em>n</em></sub><em>p</em></span></p>
class="math inline">\(n\)</span>, <span
class="math inline">\(\displaystyle \prod_{p|n} p\)</span></p>
<pre><code>λn.
{ ni ⟜ ⌊(√(ℝn))
; isPrime ← λn.¬((∨)/ₒ #f ([(n|x)=0]&#39;(⍳ 2 (⌊(√(ℝn))) 1)))
Expand Down
101 changes: 99 additions & 2 deletions doc/apple-by-example.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ The REPL and typechecker are available on the [release page](https://github.com/

### Libraries

To install the libraries, you will can use [ghcup](https://www.haskell.org/ghcup/) to install cabal and GHC.
To install the libraries, you can use [ghcup](https://www.haskell.org/ghcup/) to install cabal and GHC.

Then:

Expand Down Expand Up @@ -141,6 +141,40 @@ Reverse applied to a higher-dimensional array reverses elements (sub-arrays) alo
, [0, 1] ]
```

## Outer Product

The outer product `` creates a table by applying some function.

It has type

```
> :ty \f.\x.\y. x f⊗ y
(a → b → c) → Arr sh0 a → Arr sh1 b → Arr (sh0 ⧺ sh1) c
```

```
> (frange 0 9 10) (*)⊗ (frange 0 9 10)
[ [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
, [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]
, [0.0, 2.0, 4.0, 6.0, 8.0, 10.0, 12.0, 14.0, 16.0, 18.0]
, [0.0, 3.0, 6.0, 9.0, 12.0, 15.0, 18.0, 21.0, 24.0, 27.0]
, [0.0, 4.0, 8.0, 12.0, 16.0, 20.0, 24.0, 28.0, 32.0, 36.0]
, [0.0, 5.0, 10.0, 15.0, 20.0, 25.0, 30.0, 35.0, 40.0, 45.0]
, [0.0, 6.0, 12.0, 18.0, 24.0, 30.0, 36.0, 42.0, 48.0, 54.0]
, [0.0, 7.0, 14.0, 21.0, 28.0, 35.0, 42.0, 49.0, 56.0, 63.0]
, [0.0, 8.0, 16.0, 24.0, 32.0, 40.0, 48.0, 56.0, 64.0, 72.0]
, [0.0, 9.0, 18.0, 27.0, 36.0, 45.0, 54.0, 63.0, 72.0, 81.0] ]
```

```
> (frange 0 4 5) [(x,y)]⊗ (frange 0 4 5)
[ [(0.0*0.0), (0.0*1.0), (0.0*2.0), (0.0*3.0), (0.0*4.0)]
, [(1.0*0.0), (1.0*1.0), (1.0*2.0), (1.0*3.0), (1.0*4.0)]
, [(2.0*0.0), (2.0*1.0), (2.0*2.0), (2.0*3.0), (2.0*4.0)]
, [(3.0*0.0), (3.0*1.0), (3.0*2.0), (3.0*3.0), (3.0*4.0)]
, [(4.0*0.0), (4.0*1.0), (4.0*2.0), (4.0*3.0), (4.0*4.0)] ]
```

# Examples

## Dot Product
Expand Down Expand Up @@ -233,6 +267,69 @@ To drop the first 6 elements:
\p.\xs. (xs˙)'p⩪xs
```

## Numerics

### Arithmetic-Geometric Mean

```
λx.λy.([{a⟜x->1;g⟜x->2;((a+g)%2,√(a*g))}]^:6 (x,y))->1
```

Thence the [complete elliptic integral of the first kind](https://en.wikipedia.org/wiki/Elliptic_integral#Complete_elliptic_integral_of_the_first_kind):

```
λk.
{
agm ← λx.λy.([{a⟜x->1;g⟜x->2;((a+g)%2,√(a*g))}]^:6 (x,y))->1;
𝜋%(2*agm 1 (√(1-k^2)))
}
```

Gauss' approximation of the logarithm:

```
λm.λx.
{
amgm ← λx.λy.([{a⟜x->1;g⟜x->2;((a+g)%2,√(a*g))}]^:15 (x,y))->1;
-- m>2
𝜋%(2*amgm 1 (0.5^(m-2)%x))-ℝ m*0.6931471805599453
}
```

### Hypergeometric Function

$$ {}_pF_q(a_1,\ldots,a_p;b_1,\ldots,b_q;x) = \sum_{n=0}^\infty \frac{(a_1)_n\ldots (a_p)_n}{(b_1)_n\ldots (b_q)_n}\frac{x^n}{n!}$$

where $(x)_n$ is the [rising factorial](#rising-factorial) above.

```
λa.λb.λz.
{
rf ← [(*)/ₒ 1 (𝒻 x (x+y-1) (⌊y))]; fact ← rf 1;
Σ ← λN.λa. (+)/ₒ 0 (a'(⍳ 0 N 1)); Π ← [(*)/x];
Σ 30 (λn. {nn⟜ℝ n; (Π ((λa.rf a nn)'a)%Π((λb. rf b nn)'b))*(z^n%fact nn)})
}
```

We can use the REPL to inspect the type:

```
> :yank H math/hypergeometric.🍏
> :ty H
Vec (i + 1) float → Vec (i + 1) float → float → float
```

## Geography

### Mercator

$$ x \mapsto \lambda - \lambda_0 $$
$$ y \mapsto \frac{1}{2} \log{\left(\frac{1+\sin \phi}{1-\sin \phi}\right)} $$

```
\𝜆₀.\𝜑.\𝜆.{a⟜sin.𝜑;(𝜆-𝜆₀,(_.((1+a)%(1-a)))%2)}
```

## Number Theory

### Primality Check
Expand All @@ -243,7 +340,7 @@ To drop the first 6 elements:

### Radical

Compute the radical of an integer $n$, $\prod_{p|n} p$
Compute the radical of an integer $n$, $\displaystyle \prod_{p|n} p$

```
λn.
Expand Down
1 change: 1 addition & 0 deletions docs/index.html

0 comments on commit 4f03e67

Please sign in to comment.