Skip to content

Instantly share code, notes, and snippets.

View jakobrs's full-sized avatar

Jakob Rødal Skaar jakobrs

View GitHub Profile
#include <cstdlib>
#include <iostream>
#include <chrono>
#include <random>
template <typename Fn>
void measure(Fn &&fn) {
auto before = std::chrono::system_clock::now();
auto res = std::forward<Fn>(fn)();
auto after = std::chrono::system_clock::now();
structure Z (modulus : Nat) : Type where
val : Fin modulus
deriving Repr
instance : Add (Z modulus) where
add x y := { val := x.val + y.val }
instance : Mul (Z modulus) where
mul x y := { val := x.val * y.val }
[package]
name = "idk"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
cranelift = "0.105.1"
cranelift-module = "0.105.1"
@jakobrs
jakobrs / mdspan.hpp
Last active December 12, 2023 16:37
template <typename T, size_t N>
struct mdspan {
T *data;
std::array<size_t, N> extents;
template <typename... OtherIndexTypes>
requires(std::is_convertible_v<OtherIndexTypes, size_t> && ...) &&
(sizeof...(OtherIndexTypes) == N)
constexpr explicit mdspan(T *data, OtherIndexTypes... _extents)
: data(data), extents{} {
#let template(
title: none,
font: "New Computer Modern",
authors: (),
date: none,
show-outline: false,
body
) = {
show link: text.with(blue)
{-# LANGUAGE NumericUnderscores #-}
import Data.Int
modulus = 1_000_000_007
-- M = Z / 1_000_000_007 Z
newtype M = M Int64 deriving (Eq, Ord)
-- Ext = M[x]/(x^2 - 5)
@jakobrs
jakobrs / montgomery.rs
Last active July 15, 2023 13:50
Montgomery arithmetic in Rust
use std::ops::{Add, Mul, Sub};
/// Performs the extended euclidean algorithm
pub const fn xgcd(a: i64, b: i64) -> (i64, i64, i64) {
let (mut old_r, mut r) = (a, b);
let (mut old_s, mut s) = (1, 0);
let (mut old_t, mut t) = (0, 1);
while r != 0 {
let quotient = old_r / r;
// Complex numbers are represented as pairs
#let re(x) = x.at(0)
#let im(x) = x.at(1)
#let cmul(x, y) = (re(x)*re(y) - im(x)*im(y), re(x)*im(y) + im(x)*re(y))
#let cadd(x, y) = (re(x) + re(y), im(x) + im(y))
#let csub(x, y) = (re(x) - re(y), im(x) - im(y))
#let cscale(k, x) = (k * re(x), k * im(x))
#let cis(phi) = (calc.cos(phi), calc.sin(phi))
#let cshow(x) = [#re(x) + i #im(x)]
#![feature(generators, generator_trait)]
use std::{
cell::Cell,
future::Future,
ops::Generator,
pin::Pin,
rc::Rc,
task::{Context, Poll, RawWaker, RawWakerVTable, Waker},
};
#lang racket
(define (split x)
(match x
['() '(() . ())]
[(list-rest x y zs)
(match-let ([(cons xs ys) (split zs)])
(cons
(cons x xs)
(cons y ys)))]))