Fun One Liners

It really just depends on the number of partials since complex step will repeat the primal.

I’m (100-ε )% shure that this Animation can be done better, but have to got to bad now:

j=0;i=0; d=2;  while true ; sleep(0.05);global i=i +0.02;global d; global j = j+1; if floor(i)%d==1 print("\e[4D\e[K")end; if floor(i)%d==0 print("\e[K") end ; print(Char((128512+j%128))); if  i%10==0 d= rand(2:5) end ; end

tomorrow i will write ForwardDiff as an one liner . . . not

3 Likes

My favorute:

julia> fib(n) = (BigInt.([1 1; 1 0])^(n-1))[1]
fib (generic function with 1 method)

julia> fib.([1:10;])
10-element Array{BigInt,1}:
  1
  1
  2
  3
  5
  8
 13
 21
 34
 55

julia> fib(100)
354224848179261915075
16 Likes

How about

rbinomial( p, size... ) = rand( size... ) .< p

?

1 Like

You mean like this one liner?

my_exp(x; n=1e10) = (1 + 1/n)^(n*x)
7 Likes

One of the first packages I made is Fatou.jl which is intended to provide a one-liner interface for fractals

but I haven’t seen anyone post their own version Fatou one-liner fractals anywhere yet

10 Likes

Precisely! This is a nice example, as it exhibits all the problems I mentioned above:

  1. it is absolutely unnecessary, as we have exp,

  2. it is about 1000x slower: this is incorrect, thanks @DNF

    julia> x = 12
    12
    
    julia> @btime exp($x);
      0.027 ns (0 allocations: 0 bytes)
    
    julia> @btime my_exp($x);
      19.922 ns (0 allocations: 0 bytes)
    
  3. it is of course inaccurate:

    julia> my_exp(x) / exp(x)
    1.0000009922849442
    
3 Likes

Actually, I’m not sure about that. I don’t know if I’m using this correctly, but I believe you may have to force the computation to happen during runtime, instead of letting the compiler figure out the answer:

julia> @btime exp(Ref($x)[]);
  28.093 ns (0 allocations: 0 bytes)

julia> @btime my_exp(Ref($x)[]);
  23.459 ns (0 allocations: 0 bytes)

I don’t think I did that quite right, but if you look at the broadcasted version:

julia> x = 100 * rand(1000);

julia> @btime exp.($x);
  9.457 μs (1 allocation: 7.94 KiB)

julia> @btime my_exp.($x);
  22.152 μs (1 allocation: 7.94 KiB)

it’s a lot closer than 1000x.

Is the benchmarking Ref trick documented anywhere? I couldn’t find it.

2 Likes

Good point. Sub-ns timings should have been a warning. Cf
https://github.com/JuliaCI/BenchmarkTools.jl/issues/130

2 Likes

Yeah, that’s a good idea. But I frequently see this Ref($x)[] (or something similar) trick. Could that be automated? Or maybe it should just be explained in the manual? I cannot find it there.

2 Likes

I

I am glad that you liked my example! Hopefully I wont derail the conversation again, but I believe that we have stumbled onto something simultaneously profound and silly at the same time.

A lot of people can be seduced by elegance. Here we see an example where one line of code can be used to approximate one of the most important/beautiful functions in mathematics. The approximation directly uses the definition

e=\lim_{n\to\infty}\left(1+\frac{1}{n}\right)^n,

so the approximation is highly elegant. Furthermore, the approximation can get you 6 digits of accuracy in mere nanoseconds! (As long as you are using a fast language like Julia.)

Although, as you pointed out the elegance conceals just how impractical the one liner is. I would not want to see anybody using this in production code.

Also, as a side note I have realized that:

\exp(x)=\lim_{n\to\infty}\left(1+\frac{x}{n}\right)^n

is a slightly better approximation and would also work as a one liner.

5 Likes

AFAIK Pade approximants (with some judicious scaling and squaring, if necessary) dominate this, but I may not remember correctly. As far as I am aware, actual implementations are table-driven, eg

Tang, Ping-Tak Peter. “Table-driven implementation of the exponential function in IEEE floating-point arithmetic.” ACM Transactions on Mathematical Software (TOMS) 15.2 (1989): 144-157.

@chakravala that is awesome!

Bound an angle to be between 0 and 2pi (or tau, hey can we get tau as a constant?)

Wrap(x) = ( (x % 2pi) + 2pi ) % 2pi

Hamilton Product

HamiltonProduct(a, b) = [ a[1]*b[1] - a[2]*b[2] - a[3]*b[3] - a[4]*b[4],
                          a[1]*b[2] + a[2]*b[1] + a[3]*b[4] - a[4]*b[3],
                          a[1]*b[3] - a[2]*b[4] + a[3]*b[1] + a[4]*b[2],
                          a[1]*b[4] + a[2]*b[3] - a[3]*b[2] + a[4]*b[1]
                        ]

In fact, I have a PR that does just that, but I don’t think it’ll be accepted. Document the practice of referencing and dereferencing variables to stop constant prop by MasonProtter · Pull Request #140 · JuliaCI/BenchmarkTools.jl · GitHub

For those curious, the correct thing to do is generally $(Ref(x))[] instead of Ref($x)[], since you don’t want to accidentally measure the time it takes to apply Ref to x.

1 Like

there is https://github.com/JuliaMath/Tau.jl
and mod2pi comes with Julia.

5 Likes

I think I’ll just continue to define Tau myself rather then import this package. Maybe there’s some elaborate argument why it should be “tau ~ 2pi” but for my uses, it’s just 2pi.

tau = 2pi

Cool to know about mod2pi. Didn’t know it was in the base library, because I had no idea there was a name for this function, the code for it is well, interesting: https://github.com/JuliaLang/julia/blob/c6da87ff4bc7a855e217856757ad3413cf6d1f79/base/math.jl#L801-L987
Guess this is why I’d never call myself a soft-ware engineer!

Here’s another reason why I love Julia:

"""Convenience function for performing Redheffer Star Products."""
⋆( A, B ) = RedhefferStarProduct( A, B )

The above function won’t run on your machine - but, you can define unicode operators for shorthand! So if I wanna Redheffer star some stuff

F=A⋆B⋆C⋆D⋆E

Here is a useful (maybe not so fun) oneliner that I use everywhere. Just put the following line after module MyPackage so that ?MyPackage shows README

@doc read(joinpath(dirname(@__DIR__), "README.md"), String) MyPackage

(It’s based on code I saw somewhere. Maybe it was @mbauman?)

There is also a multi-line version if you want to run doctest on README:

@doc let path = joinpath(dirname(@__DIR__), "README.md")
    include_dependency(path)
    replace(read(path, String), "```julia" => "```jldoctest")
end MyPackage
26 Likes

Simulate the frog problem shown recently on standupmaths with one line and

frog(n,cur_lily,steps) = (cur_lily == n) ? steps : frog(n,rand(cur_lily+1:n),steps+1)

estimate the mean number of steps needed to jump a 10 step wide river with another

mean(frog(10,0,0) for _ in 1:1_000_000)
4 Likes
a=[1:3;] # equal to collect(1:3)
4 Likes

single line grep:

grep(file, pattern) = collect(Iterators.filter(x -> occursin(pattern, x), eachline(file)))

9 Likes