Skip to content

Instantly share code, notes, and snippets.

@rayfix
rayfix / ShrödingerEquationSolver.swift
Last active November 26, 2015 02:41
Schrödinger Equation Solver: Harmonic Oscillator
//: # Harmonic Oscillator Schrödinger Equation
//:
//: Numerically Solve for Shrödinger's Equation using the
//: Numerov algorithm. This example inspired by Quantum
//: Physics for Dummies, Steven Holzner, John Wiley & Sons, Inc. 2013.
//:
//: Had to increase the delta (step size) and reduce the maximum allowed
//: error to make it run in a reasonable amount of time in the playground.
//: (This obviously could be solved by switching to [more] compiled code.)
//:
@rayfix
rayfix / ShrödingerEquationSolver2.swift
Created November 29, 2015 04:14
ShrödingerEquationSolver2
//: # Harmonic Oscillator Schrödinger Equation
//: ## Version 2
//:
//: Numerically Solve for Shrödinger's Equation using the
//: [Numerov method](https://en.wikipedia.org/wiki/Numerov%27s_method).
//: This example inspired by Quantum
//: Physics for Dummies, Steven Holzner, Wiley 2013.
//:
//: This version is thousands of times faster and more
//: accurate than my more direct port of the original. It starts with
@rayfix
rayfix / loop.swift
Created December 4, 2015 21:45
a simple loop
var i = 0
while (i < 10) {
print(i)
i += 1
}
@rayfix
rayfix / merge_sort.cpp
Last active December 19, 2015 08:59
Simple merge sort implementation in C++. A little more polish. Thanks to feedback from @matt_dz
#include <iostream>
#include <vector>
#include <functional>
namespace rf {
template <typename T, typename Compare = std::less<T>>
std::vector<T> merge_sort(const std::vector<T>& input,
const Compare& compare = Compare())
{
@rayfix
rayfix / merge_sort.rb
Last active December 19, 2015 09:09
Naive merge sort in ruby.
class Array
def merge_sort
c = self.count
return self if c <= 1
mid = c/2
left = self[0,mid].merge_sort
right = self[mid, mid+1].merge_sort
output = []
l = 0;
@rayfix
rayfix / RFViewController.m
Created August 17, 2013 23:08
Our animations were not working with our dispatch queue. I boiled it down to a simple test case. However, the test case works perfectly as expected so there is another problem that is happening. This code makes a view oscillate back and forth.
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self move];
}
- (void)move
{
static BOOL flipFlop;
struct MultiArray<Element> {
private var data: [Element] // the actual data
private var counts: [Int] // the size of each dimension
private var offsets: [Int] // multipliers for accessing each dimension quickly
private func computeIndex(indices: [Int]) -> Int {
precondition(indices.count == counts.count)
for (index, count) in zip(indices, counts) {
precondition(index >= 0 && index < count)
@rayfix
rayfix / frequencies.swift
Created January 10, 2016 02:23
Generic frequencies method for SequenceTypes
//: # Generic frequencies for SequenceType
//:
//: Based on a tweet from Airspeed Velocity @airspeedswift
//: describing a custom subscript operation that takes
//: an initial value if the key is not found.
import Foundation
extension Dictionary {
subscript(key: Key, or initial: Value) -> Value {
@rayfix
rayfix / gist:1fb467981e386ad5797e
Created August 31, 2014 14:56
Swift Ring Buffer
//
// Ring.swift
//
// Copyright (c) 2014 Pelfunc, Inc. All rights reserved.
//
/// Allow iteration of Ring<T> in LIFO order
public struct RingGenerator<T> : GeneratorType {
@rayfix
rayfix / CrashyViewController.swift
Created January 27, 2016 15:55
Trying to make a func visible to ObjC via protocol extension
import UIKit
protocol Tapper: class {
func install()
func foo()
}
extension Tapper where Self: UIViewController {
func install() {
view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "foo"))