Skip to content

Instantly share code, notes, and snippets.

Implementing APL rules for broadcasting into Numpy

This is an attempt for extending broadcasting rules in Numpy without losing the benefits of vectorization by converting new rules to preexisting ones.

The broadcasting policy is taken from APL standards according to the following ideas:

  • a subset of nested arrays from APL is implemented here;
  • an axis parameter is implemented here.

Enclosing parts of an array

@baruchel
baruchel / Maxima-Vim.md
Last active October 6, 2021 13:49
Complete integration of Maxima into Vim with Slimv

I previously wrote a plugin called vim-notebook for using Maxima in a Vim buffer. But since Maxima is written in Lisp and intended to be usable from Lisp code, I finally figured out a very nice way to achieve a similar result by using the existing Slimv.

In this post, I explain how to:

  • install a Lisp implementation (Clozure CL);
  • install the Quicklisp library manager;
  • install the Swank server;
  • fetch the latest version of Maxima and compile the interpreter only;
  • integrate Maxima to Vim with Slimv.
@baruchel
baruchel / compile_heirloom_doctools.md
Last active February 14, 2024 13:15
Quickly compiling Heirloom Doctools

Each time I re-install a fresh Linux distribution, one of the first things I need to do is to compile the Heirloom Documentation Tools. However some adjustments have to be performed; I write them here in order to recall them later:

  • download the sources from http://heirloom.sourceforge.net/doctools.html

  • type: sudo apt-get install g++ libc-dev bison flex

  • uncompress the tarball

  • enter the heirloom-doctools directory

  • edit mk.config and change three lines:

    • replace INSTALL=/usr/ucb/install with INSTALL=/usr/bin/install
  • replace PREFIX=/usr with PREFIX=/opt/heirloom
@baruchel
baruchel / dd.py
Last active December 11, 2015 13:03
Fast vectorized arithmetic with ~32 significant digits under Numpy
# Adapted from:
# "A Floating-Point Technique for Extending the vailable Precision"
# by T.J. Dekker in Numer. Math. 18, 224-242, 1971.
# (adapted to Numpy from the Algol 60 code in the paper)
import numpy as np
import decimal
_npDecimal = np.vectorize(decimal.Decimal, otypes=[object])
@baruchel
baruchel / library_sort.py
Last active November 21, 2015 04:04
Library sort in Python
# -*- coding: utf8 -*-
def library_sort(l):
# Initialization
d = len(l)
k = [None]*(d<<1)
m = d.bit_length() # floor(log2(n) + 1)
for i in range(d): k[2*i+1] = l[i]
# main loop
@baruchel
baruchel / gist:75c4134a362cd0ebd1f5
Created June 10, 2015 07:25
Quick version of k-means with numpy written in a functional style
# -*- coding: utf-8 -*-
import numpy as np
import scipy.ndimage.measurements
import random
def kMeans( data, k, centers=None, iter=64 ):
if len(data.shape) == 1:
data = np.vstack(data)

Hacking Dyalog APL # 1

Dyalog APL is a great tool but the interface for working in an interactive session is rather poor. Editing previous lines by moving the cursor to some other location is rather painful. Of course Dyalog APL isn't allowed to use the great readline library. But it is not too difficult to replace the officiel ncurses interface by some more modern readline-based one as long as you do it with an external tool.

What you will get is:

  • a modern readline-based input;
  • a great history system (persistent accross sessions if you wish);
  • the ability to launch your favorite editor on the fly for editing a more complex line of input (or defining a traditional function) whenever you want;
  • leaving Dyalog APL with the Ctrl-D signal;
" quickmove.vim : move the cursor according to the more/less game
" Author: Thomas Baruchel
" Version: 1.0
" Date: Mar 14, 2015
"
let g:quickmove_version = "1.0"
if &compatible
finish
endif
@baruchel
baruchel / notebook.md
Last active August 29, 2015 14:09
Thomas Baruchel's mathematical notebook

Thomas Baruchel's mathematical notebook

(https://gist.github.com/2b870728323456f71402)

This is my mathematical notebook; I put here the identities I find from time to time; this notebook is written in the markdown format with the vim editor. I use my own vim-notebook plugin for embedding Maxima code in it; thus I can evaluate each cell of code.

-- Reproduire la table `CountryLanguage` à la différence près que la colonne Percentage doit maintenant contenir le pourcentage de locuteurs d'une langue dans un pays par rapport au nombre total de locuteurs de cette langue dans le monde.
SELECT CountryCode, Language, IsOfficial,
(A.Percentage * A.Population / B.nbr) AS Percentage
FROM (
SELECT CountryLanguage.*, Country.Population
FROM CountryLanguage
LEFT JOIN Country
ON CountryLanguage.CountryCode = Country.Code
) AS A