Processing math: 100%

Content tagged math

#38: Spirals

posted on 2018-03-20

Recall that the symbol Z2 denotes the set of all coordinate pairs with integer components.

A spiral of length n1 is a counter-clockwise spiral starting at (0,0) extending to the right to (n,0), wrapping around so as to cover Z2 completely.

Consider the following spiral of length 2.

*--*--*--*--*--*
|              |
*  *--*--*--*  *
|  | (0,0)  |  |
*  *  x--*--x  *
|  |     (2,0) |
*  *--*--*--*--*
|
*--*--*--*--*--* ...

The points of the spiral have a natural ordering. The above spiral can be written as a sequence s where s0=(0,0),s3=(2,1),s1=(1,0),s4=(1,1),s2=(2,0),s5=(0,1), and so on.

There are three challenges. Given an n1, do the following.

  1. Given a k0, find sk.

  2. Given a point (α,β)Z2, find the k such that sk=(α,β).

  3. Print the spiral of length 2 to the terminal by printing the values of k in their correct positions. (And make it look nice by ensuring the numbers are aligned and reasonably spaced out.)

#37: Staircase Paths

posted on 2018-02-10

Prerequisites

The symbol Zn denotes the set of all coordinate n-tuples with integer components. These are one kind of n-dimensional lattice.

Given two points s=(s1,,sn) and e=(e1,,en), their taxicab distance is se1:=ni=1|siei|.

Given a complex number z, we denote its normalization as N(z):={0if z=0,z/|z|otherwise.

Challenge

A staircase path between two points, informally, is any path by going only right or up.

Formally, given the starting point sZn and ending point eZn with se (i.e., each component of s is less than or equal to each corresponding component of e), a staircase path is a sequence of :=1+se1 coordinates σ0,,σ with the following facts:

  1. σ0=s,

  2. sσke (in the sense described above), and

  3. exactly one component of σk+1σk is 1, with the other n1 components equal to 0.

Denote the set of all staircase paths from s to e as {se}.

Part 1: Verify that facts 1–3 imply σ=e.

Next, consider the set Tm×n defined as n×m matrices whose entries are complex numbers of unit modulus. For consistency with the above, let's suppose the bottom-left element of each matrix is identified by (1,1) and top-right element is (n,m).

Part 2: Implement the function f:Tm×nT{0} defined by f(M):=N(Σ{(1,1)(m,n)}(x,y)ΣMx,y).

Extra Credit: Generalize f to work on a tensor of any order.

#30: Combinator Practice

posted on 2018-01-30

In Challenge #15, we wrote a few simple combinators. Here we write a few more.

In addition to combinator practice, this exercise will be written so as to practice math notation comprehension.

For each of these, implement the combinator and write the most general type for that combinator.

  1. Combinator C1 which "flips" the arguments of binary function. It takes f:X×YZ to C1f=f:Y×XZ such that for all xX and yY, f(x,y)=f(y,x). This combinator is an involution because C21 is the identity.

  2. Combinator C2 which duplicates an argument to a binary function. It takes f:X×XZ to C2g=f:XZ such that for all xX, f(x,x)=f(x).

  3. Combinator C3 which takes two functions with the same domain X, and produces a function taking xX and returning (f(x),g(x)).

  4. Combinator C4 which takes a function, and produces a curried function of two arguments, such that the second argument supplied is effectively ignored.

Fun Fact: In Haskell, the combinators above are called flip, join, liftM2 (,), and (const .) respectively.

This blog covers math, challenge

View content from 2018-01, 2018-02, 2018-03


Unless otherwise credited all material copyright © 2010–2018 by Robert Smith