03/27/17

# Using pipes while running external programs in Julia

Recently I was using Julia to run ffprobe to get the length of a video file. The trouble was the ffprobe was dumping its output to stderr and I wanted to take that output and run it through grep. From a bash shell one would typically run:

ffprobe somefile.mkv 2>&1 |grep Duration

This would result in an output like

 Duration: 00:04:44.94, start: 0.000000, bitrate: 128 kb/s

This works because we used 2>&1 to redirect stderr to stdout which would in be piped to grep.

If you were try to run this in Julia

julia> run(ffprobe somefile.mkv 2>&1 |grep Duration)

you will get errors. Julia does not like pipes | inside the backticks command (for very sensible reasons). Instead you should be using Julia’s pipeline command. Also the redirection 2>&1 will not work. So instead, the best thing to use is and instance of Pipe. This was not in the manual. I stumbled upon it in an issue discussion on GitHub. So a good why to do what I am after is to run.

julia> p=Pipe()
Pipe(uninit => uninit, 0 bytes waiting)

julia> run(pipeline(ffprobe -i  somefile.mkv,stderr=p))

This would create a pipe object p that is then used to capture stderr after the execution of the command. Next we need to close the input end of the pipe.

julia> close(p.in)

Finally we can use the pipe with grep to filter the output.

julia> readstring(pipeline(p,grep Duration))
"  Duration: 00:04:44.94, start: 0.000000, bitrate: 128 kb/s\n"

We can then do a little regex magic to get the duration we are after.

julia> matchall(r"(\d{2}:\d{2}:\d{2}.\d{2})",ans)[1]
"00:04:44.94"
01/13/17

# Kaperkar’s Constant

I was recently introduced to Kaperkar’s Constant.
It is quite magical. You take any four digit number $A$, sort the digits from highest to lowest to create a new number $A^{\text{high}}$, sort the digits from lowest to highest to get $A^{\text{low}}$, and calculate and new number $A= A^{\text{high}}- A^{\text{low}}$. You repeat this procedure enough times and you end up with $A=6174$.

I made a nifty implementation of that in Julia below.

09/25/16

# Julia calling C: A minimal example

This blog is a “Hello World” example of Julia calling C.

We start of by at bit of C code we want to call from Julia. We write the following in calc_mean.c

double mean(double a, double b) {
return (a+b) / 2;
}

To build the library, we need to create a Makefile

CC=gcc

CFLAGS=-c -Wall -fPIC

SOURCES=calc_mean.c
OBJECTS=$(SOURCES:.c=.o) .c.o:$(CC) $(CFLAGS)$< -o $@ lib:$(OBJECTS)
$(CC) -shared -fPIC -o libmean.so$(OBJECTS)

clean:
rm *.o *.so

The option fPIC and -shared are essential for Julia to be able to resolve the function in our library. Now we are almost ready to build our library. From the bash terminal we invoke:

make lib

This will generate a libmean.so file.

In Julia we call the function in our c library by

x=ccall((:mean,"libmean"),Float64,(Float64,Float64),2.0,5.0)
println(x)
3.5

For this to work,

• Julia must be running either on the same path where libmean.so resides,
• the path to libmean.so is in LD_LIBRARY_PATH, or
• the path to the library is pushed to Libdl.DL_LOAD_PATH via

push!(Libdl.DL_LOAD_PATH,"path_to_libmean.so")

P.S. Thanks to Christopher Rackauckas for tips on Julia highlighting.

05/2/15

# Monkeys and Coconuts

Here is my attempt to solve the monkeys and coconuts problem. The story goes
that five sailors were stranded on an island and had decided to gather some
coconuts for their provisions. They put all the coconuts in one large pile and
went to sleep. One sailor got up and fearing that there could be problems when the
time to came to divide the pile, he divided the pile five ways and noticing that
he has an extra coconut, he gave it to a monkey, and then hid his stash. The other
four sailor repeated the same procedure. When they woke up they noticed that
they had a smaller pile and proceeded to divide into five equal piles, this time
around there were no extra coconut left for the monkey. So the question is: what was
the size of the original pile?

We will denote by $x_i$ as the size of the
pile after the $i$th sailor has carried out his procedure. In this system $x_0$
is original size of the pile. So following this procedure we then proceed as
follows:

$x_1=\frac{4(x_0-1)}{5}$
$\cdots$
$x_i=\frac{4(x_{i-1}-1)}{5}$
$\cdots$
$x_5=\frac{4(x_4-1)}{5}$

It is important to note that $x_i \in \mathbb{N}_0$ for $i=0\ldots5$, also $\frac{x_5}{5}\in \mathbb{N}_0$. Alternatively, we think of the reverse procedure and express the above as
$x_4=\frac{5x_5}{4}+1$
$\cdots$
$x_i=\frac{5x_{i+1}}{4}+1$
$\cdots$
$x_0=\frac{5x_1}{4}+1$

Observing that $x_5$ has to be divisible by 4 and 5 (last equation in the first system and first equation in the second), one can brute force the solution(s) by the following Julia code:

Which results in

[2496,1996,1596,1276,1020]
m=51, x₀= 3121
[14996,11996,9596,7676,6140]
m=307, x₀= 18746
[27496,21996,17596,14076,11260]
m=563, x₀= 34371
[39996,31996,25596,20476,16380]
m=819, x₀= 49996


This corresponds nicely to the answers that were obtained by rigorous derivation in the video, however it shows how programming can easily find such solutions by brute force.

If one would like to avoid the negative or blue concocts suggested in the video and also preserve the monkey. Below is an alternative derivation. Working through the first system, one gets:

$x_{5}={{4\,\left({{4\,\left({{4\,\left({{4\,\left({{4\,\left(x_{0}- 1\right)}\over{5}}-1\right)}\over{5}}-1\right)}\over{5}}-1\right) }\over{5}}-1\right)}\over{5}}=\frac{4^5x_0}{5^5}-\frac{8404}{5^5}$

Hence,
$5^5x_5=4^5x_0-8404$.
Realizing the $x_5$ has to be necessarily divisible by 5, we denote the final share that each sailor gets in the last division by $s$. So our Diophantine equation becomes
$5^6s=4^5x_0-8404$.
It will have solutions at $x_0=3121, 18746, 34371 \ldots= 3121+n5^6 \text{ for } n \in \mathbb{N}_0$.