Skip to content

Instantly share code, notes, and snippets.

View haampie's full-sized avatar

Harmen Stoppels haampie

View GitHub Profile
@haampie
haampie / hessenberg.jl
Last active February 28, 2018 22:49
hessenberg optimization
using BenchmarkTools
import Base.BLAS: BlasInt, @blasfunc, liblapack
# subroutine dhseqr (
# character JOB,
# character COMPZ,
# integer N,
# integer ILO,
# integer IHI,
@haampie
haampie / jacobi.jl
Created August 9, 2017 10:19
Comparison row / column
import Base: start, next, done
using BenchmarkTools
mutable struct RowJacobi{matT,vecT}
A::matT
x::vecT
next::vecT
b::vecT
maxiter::Int
@haampie
haampie / unsafe.jl
Created July 6, 2017 16:08
unsafe.jl
struct UnsafeView{T, N} <: DenseArray{T, N}
dim::NTuple{N, Int}
ptr::Ptr{T}
end
const UnsafeVectorView{T} = UnsafeView{T,1}
const UnsafeMatrixView{T} = UnsafeView{T,2}
@inline Base.size(v::UnsafeView) = v.dim
@inline Base.size(v::UnsafeVectorView, idx::Int) = idx == 1 ? v.dim[idx] : 1
function literature_example()
# Problem: Δu + 1000uₓ = f
# u = 0 on the boundaries
# f(x, y, z) = exp(xyz) sin(πx) sin(πy) sin(πz)
# 2nd order central differences (shows serious wiggles)
# Unknowns per dimension
N = 50
# Total number of unknowns
@haampie
haampie / reortho.jl
Created June 15, 2017 15:31
reorthogonalization.jl
import Base.LinAlg.BlasFloat
import Base.LinAlg.BLAS: gemv!, gemv, axpy!
using BenchmarkTools
function classical_gram_schmidt!{T<:BlasFloat}(V::StridedMatrix{T}, w::StridedVector{T})
# orthogonalize
h = gemv('T', one(T), V, w)
gemv!('N', -one(T), V, h, one(T), w)
@haampie
haampie / gmres.jl
Last active June 10, 2017 22:30
More efficient GMRES
using BenchmarkTools
using IterativeSolvers
module GMRES
using Base.LinAlg.axpy!
function gmres(A, b; outer::Int = 5, restart::Int = 20, tol = sqrt(eps(real(eltype(b)))))
T = eltype(b)
@haampie
haampie / better_collections.php
Last active April 16, 2017 13:10
better_collections.php
<?php
function take(Iterator $it, int $n) {
for ($i = 0; $i < $n && $it->valid(); $i++, $it->next()) {
yield $it->current();
}
}
function filter(Iterator $it, callable $f) {
foreach ($it as $item) {
@haampie
haampie / collections.php
Last active September 4, 2023 14:30
Collections
<?php
final class User
{
private $name;
public function __construct(string $name)
{
$this->name = $name;
}
@haampie
haampie / 94.cc
Created December 3, 2016 13:04
Problem 94
#include <iostream>
#include <tuple>
using pell = std::pair<size_t, size_t>;
inline pell next(pell const &solution)
{
return {
2 * solution.first + 3 * solution.second,
2 * solution.second + solution.first
@haampie
haampie / pell.txt
Last active November 14, 2016 20:12
Pell's equation
3^2 - 2 * 2^2 = 1
9^2 - 5 * 4^2 = 1