Skip to content

Instantly share code, notes, and snippets.

View haampie's full-sized avatar

Harmen Stoppels haampie

View GitHub Profile
@haampie
haampie / json-base64.php
Created August 19, 2016 16:12
File contents and memory
<?php
$app->get('/', function () {
// Write some data to a temp file
$stream = fopen('php://temp', 'w+');
for ($i = 0; $i != 100000; $i++) {
fwrite($stream, str_repeat(strval($i), 100) . PHP_EOL);
}
@haampie
haampie / test
Created November 8, 2016 21:49
test
0.5258035496409385338996633621320518075577262895034576824436935036190909359484821735511244086357105703763089095001084909503562784106432631780885381833487161441115426324089735535648577478022198173062961599662707079574920200839277886235481421364962201726366096411855900154689889962293083318039904967542464945043705977339467548513433912472348985230682047335936507184327257718362160494751446144883847282407215005669970705901314196397855777633081493954608446354...
@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
@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 / 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 / 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 / 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 / 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)
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 / 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