Skip to content

Instantly share code, notes, and snippets.

View smrfeld's full-sized avatar
🙂

Oliver K. Ernst smrfeld

🙂
View GitHub Profile
@smrfeld
smrfeld / cpp_extension.mm
Last active December 15, 2023 22:53
C++/Obj-C extension calling Metal shader
#include <torch/extension.h>
#include <Metal/Metal.h>
#include <Foundation/Foundation.h>
#include <iostream>
// Define a function to add tensors using Metal
torch::Tensor add_tensors_metal(torch::Tensor a, torch::Tensor b, const std::string& shaderFilePath) {
// Check that device is MPS
if (a.device().type() != torch::kMPS || b.device().type() != torch::kMPS) {
@smrfeld
smrfeld / setup.py
Created December 15, 2023 21:38
Setup for Python package including PyTorch and Metal shader
import os
from setuptools import find_packages, setup
import torch
from torch.utils.cpp_extension import CppExtension, BuildExtension
def get_extensions():
# prevent ninja from using too many resources
try:
import psutil
@smrfeld
smrfeld / PythTriple.cs
Created June 26, 2022 19:30
Pythagorean triple with not optimal bound
var soln = new List<(int a, int b, int c)>();
// Bound for a
// NOTE: this is a first guess, we'll do better later
// a + b + c = N => If all are equal, upper bound is N/3
double aMax = n / 3.0;
for (int a=1; a<aMax; a++)
{
@smrfeld
smrfeld / PythTriple.cs
Last active June 26, 2022 19:28
Skeleton for simplified Pythagorean triplet
var soln = new List<(int a, int b, int c)>();
// Bound for a
// NOTE: this is a first guess, we'll do better later
// a + b + c = N => If all are equal, upper bound is N/3
double aMax = n / 3.0;
for (int a=1; a<aMax; a++)
{
@smrfeld
smrfeld / PythTriple.cs
Last active June 26, 2022 19:24
Simplified Pythagorean triple
// b = (N^2 - 2Na) / (2N - 2a)
int num = n * n - 2 * n * a;
int denom = 2 * n - 2 * a;
if (num % denom != 0)
{
continue;
}
int b = num / denom;
@smrfeld
smrfeld / PythTriple.cs
Last active June 26, 2022 19:08
Simplified Pythagorean triple
// Given int n
var soln = new List<(int a, int b, int c)>();
// Bound for a
double aMax = (1.0 - (1.0 / Math.Sqrt(2.0))) * n;
for (int a=1; a<aMax; a++)
{
@smrfeld
smrfeld / ContentView.swift
Created April 12, 2022 23:09
CircleAnimation modifier
struct CircleAnimation: AnimatableModifier {
var angle: Float
var radius: Float
var animatableData: Float {
get { angle }
set { angle = newValue }
}
func body(content: Content) -> some View {
@smrfeld
smrfeld / ContentView.swift
Created April 12, 2022 21:54
Circle animation general
struct CircleAnimation: AnimatableModifier {
var angle: Float
var radius: Float
var animatableData: Float {
get { angle }
set { angle = newValue }
}
func body(content: Content) -> some View {
@smrfeld
smrfeld / ContentView.swift
Created April 12, 2022 21:38
Circle animation fixed
struct CircleWithAnimatableAngle: Shape {
var angle: Float
var radius: Float
var animatableData: Float {
get { angle }
set { angle = newValue }
}
func path(in rect: CGRect) -> Path {
@smrfeld
smrfeld / ContentView.swift
Created April 12, 2022 21:33
Circle animation not working
struct ContentView: View {
@State var angle: Float = Float.pi / 2.0
let radius: Float = 200
var body: some View {
ZStack {
Circle()
.strokeBorder(.black, lineWidth: 2)
.foregroundColor(.clear)