Skip to content

Instantly share code, notes, and snippets.

View jiayihu's full-sized avatar
:octocat:
Nyaning in O(1)

Jiayi Hu jiayihu

:octocat:
Nyaning in O(1)
View GitHub Profile
#![allow(non_snake_case)]
fn init_array<const M: usize, const N: usize>(
A: &mut [[f32; N]; M],
r: &mut [f32; M],
p: &mut [f32; N],
) {
for i in 0..N {
p[i] = (i % N) as f32 / N as f32;
}
@jiayihu
jiayihu / machine.js
Created November 15, 2020 16:39
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
@jiayihu
jiayihu / ML.md
Created June 16, 2020 09:07
Welcome file

Machine Learning

Supervised learning

  • Inductive bias
  • Spazio delle ipotesi $H$ con ipotesi $h \in H$
  • version space, spazio delle ipotesi tra gli estremi di quelle consistenti
  • Ad esempio spazio delle curve polinomiali, con obiettivo minimizzare: $min_{w} 1/n \sum_{i=1}^n (y_i - wx_i)^2$
  • Training/empirical error vs ideal error
@jiayihu
jiayihu / machine.js
Created October 23, 2019 09:07
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
@jiayihu
jiayihu / utils.ts
Last active September 24, 2020 09:22
Cross-project utilities
export type Omit<T, K> = Pick<T, Exclude<keyof T, K>>;
export type ValueOf<T> = T[keyof T];
export function assertNever(_: never): never {
throw new Error('Unreachable code');
}
export function debounce<F extends (...params: Array<any>) => void>(fn: F, delay: number): F {
let timer: NodeJS.Timeout;
@jiayihu
jiayihu / intersect.ts
Created September 23, 2018 20:31
Determine the intersection of a segment, having ad edge in the center of circle, with the circle itself
/**
* Determine the intersection of a segment, having ad edge in the center of circle,
* with the circle itself
*/
function intersect(
xc: number,
yc: number,
radius: number,
x1: number,
y1: number
@jiayihu
jiayihu / segment-intersection.js
Created September 23, 2018 20:30
Determine the intersection point of two line segments, but considering also colinear segments as intersection
/**
* Determine the intersection point of two line segments, but considering also
* colinear segments as intersection
* @see {@link http://paulbourke.net/geometry/pointlineplane/}
*/
function intersect(
x1, y1, x2, y2,
x3, y3, x4, y4
) {
@jiayihu
jiayihu / getNearestPointInPerimeter.js
Last active September 23, 2018 21:10
Find the nearest point in the perimeter of a rectangle to a given point. JavaScript/TypeScript porting
/**
* TS porting of https://stackoverflow.com/questions/20453545/how-to-find-the-nearest-point-in-the-perimeter-of-a-rectangle-to-a-given-point
*/
function clamp(x: number, lower: number, upper: number): number {
return Math.max(lower, Math.min(upper, x));
}
export function getNearestPointInPerimeter(
l: number,
@jiayihu
jiayihu / streams.hs
Last active June 22, 2023 11:48
Infinite stream in Haskell
module Stream where
import Stream.Internal
import Debug.Trace
import Control.Arrow
import Control.Applicative
-- | Get the first element of a stream.
headS :: Stream a -> a
function copyStyles(sourceDoc, targetDoc) {
Array.from(sourceDoc.styleSheets).forEach(styleSheet => {
if (styleSheet.cssRules) { // for <style> elements
const newStyleEl = sourceDoc.createElement('style');
Array.from(styleSheet.cssRules).forEach(cssRule => {
// write the text of each rule into the body of the style element
newStyleEl.appendChild(sourceDoc.createTextNode(cssRule.cssText));
});