Skip to content

Instantly share code, notes, and snippets.

View Jutho's full-sized avatar

Jutho Jutho

  • Ghent, Belgium
  • 18:58 (UTC +02:00)
View GitHub Profile
@Jutho
Jutho / type inference issue
Last active December 29, 2015 14:29
Code to illustrate type inference issue
import Base: one, eltype
typealias AbstractNumericalMatrix{T<:Number} AbstractMatrix{T}
specialsum{T<:Number}(a1::Matrix{T},a2::Matrix{T})=a1+a2
type MatrixList{T<:Number,A<:AbstractNumericalMatrix} # T should correspond to the eltype of A
list::Vector{A}
args
@Jutho
Jutho / gemmtest
Created February 11, 2014 12:54
Matrix Multiplication with Julia
{
"metadata": {
"language": "Julia",
"name": "MatrixMultiplication"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"metadata": {
"language": "Julia",
"name": "TensorOperations"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
@Jutho
Jutho / gist:10713261
Created April 15, 2014 08:20
permutedims benchmark
{
"metadata": {
"language": "Julia",
"name": "permutebench"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
@Jutho
Jutho / permutedims
Created April 28, 2014 16:43
recursive permutedims benchmark
{
"metadata": {
"language": "Julia",
"name": "permutebench"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
@Jutho
Jutho / stagedpermutedims.jl
Created November 4, 2014 22:24
A staged function approach to permutedims!
using Base.Cartesian
function permutedimsnew!{T1,T2,N}(P::StridedArray{T1,N},B::StridedArray{T2,N},perm)
length(perm) == N || error("expected permutation of size $N, but length(perm)=$(length(perm))")
isperm(perm) || error("input is not a permutation")
dims = size(P)
for i = 1:N
dims[i] == size(B,perm[i]) || throw(DimensionMismatch("destination tensor of incorrect size"))
end
stridesB = strides(B)[perm]
stridesP = strides(P)
@Jutho
Jutho / gist:832f3f4aee84cf927a53
Last active August 29, 2015 14:09
Cartesian indexing and iteration
import Base: start, done, next, getindex, setindex!
import Base: @nref, @ncall, @nif, @nexprs
export eachelement, eachindex, linearindexing, LinearFast
# Traits for linear indexing
abstract LinearIndexing
immutable LinearFast <: LinearIndexing end
immutable LinearSlow <: LinearIndexing end
@Jutho
Jutho / cartesianiteration
Created November 19, 2014 17:53
Cartesian Iteration with correction for N=0
### Multidimensional iterators
module IteratorsMD
import Base: start, _start, done, next, getindex, setindex!, linearindexing
import Base: @nref, @ncall, @nif, @nexprs, LinearFast, LinearSlow
export eachindex
# Traits for linear indexing
linearindexing(::BitArray) = LinearFast()
function analyzeconvert()
methodlist = methods(convert,Tuple{Type,Number})
dict = Dict{DataType,Vector{Any}}()
for m in methodlist
to = m.sig.parameters[1].parameters[1]
from = m.sig.parameters[2]
TT = isa(to,Union) ? to.types : Base.svec(to)
TF = isa(from,Union) ? from.types : Base.svec(from)

Preliminary notes:

  • Abstract is left out of the type names for conciseness
  • We need a Covector (or alternative name) type for theoretical reasons, i.e. to make dispatch able to distinguish between all of the following operations and to have methods with a different return type for these different operations.
  • We need a LazyTranspose type (and Covector) for computational efficiency, i.e. to link with BLAS, to replace the current Ac_mul_Bt etc family.
  • Alternatives with ? ... ? are not very likely, just to point out the possibility.

Given vectors v and w, linear maps with matrix representation A and B, and a covector z

abstract operation julia ascii julia method possible alternative or unicode equivalent