From 39ab972fa7bbf9bde0657a1480b2348a4b083cf1 Mon Sep 17 00:00:00 2001 From: NunoSempere Date: Tue, 1 Aug 2023 13:05:02 +0200 Subject: [PATCH] add burning fat example --- examples/09_burn_10kg_fat/example | Bin 0 -> 22408 bytes examples/09_burn_10kg_fat/example.c | 48 +++++++++++++++++++++++++ examples/09_burn_10kg_fat/makefile | 53 ++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+) create mode 100755 examples/09_burn_10kg_fat/example create mode 100644 examples/09_burn_10kg_fat/example.c create mode 100644 examples/09_burn_10kg_fat/makefile diff --git a/examples/09_burn_10kg_fat/example b/examples/09_burn_10kg_fat/example new file mode 100755 index 0000000000000000000000000000000000000000..7e74eae7c8f90da67e66ed08a118478beab26559 GIT binary patch literal 22408 zcmeHPdw5e-wm)eaTA<{Fm(~YzaKcP|r7a>Bl}iFGoNB=W#c81sLz}c6+oq-o6m_uL z7Km6c^WFKb-aF1WjGr@lA9FwEala3p8BNRE*A$(Hj^m{wYQjTn5M>m|{jL2-Ptr!c zGjqTD$7O$Q*4}Hcwe~t|t-ba>`y}W7vWi7nCX*tIS@}DKT016lj8k+RxLs5LoJxr@ z0iV;AE0i&4XEPk{cXEJUUS7{ig}DN61|+_Gs*D0J=V(PjsUbn)%T#ubsPen=Rs}_9euvE}XNMlf0@oupGFBz{=@a-(-a?;^hj#ea; z<1GUp+2yMi>E`89&kTMzAnYh1`I|~%bJMyxvkRN+uWN2<4Ru`CQ8MScIkO$@e#b1< zPkPZ96jO_rt|Yfa2q(bDMm^y$k*e{3&rd+O3(jE@|9M9QEsiG~XC zOBYj{>LiC~`1L=48pjbl%9SQLsNUu8>m{cu-V`0t@5mE_9||7LyN!F-5&G=f^Ih_9hK1A z(WW%}8wZ)7SAP8h#hsW(}YV|ZX-9uWEc%fyB2gCUSUazvaqI|(Z z_bkUO#|^{v8;7fNlhxVD+VzgvFisYmPiA~(W2Rj?-cmlO5QK=vi-VPrzuy+Ttt+E2K+eA%08n8Jk5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*!{$<}wwdCv%W|uAhsC_ZWPRFFX5DFN z2E^EwtTp4T1t4m{bjp~*k-Xp`ynd$Qb$S=Y>F;^mqc3%Dvg3)m_fl=AuIUe4=cJ*P~x|W ziqtzI=y2@IiX5NrZ>UH*d>@cqABJP7^P-(An2C7zgQVg%Q_^grh{SO)FPcb}%s z89yC@eo$;A*qIXuTp8$>XTaYr}Ba0ELSLiINSKOhd~q<;?!yi?dgp4%N`cp=mP z&I$Sd8=RY;7PqqmeUXsQ=eeRR5%{66@d4Qx z6{-&iedPOq3WYwa1-&ipx_kmJov=~R-wizJlOE6C7WA69ICf?Vo(CCTFdG1!F_FLY zVvdyi!yjaz-H56F=xvHk#Q$Al?6-i|40_1wry1mrMIcf9REqeKew-}u zczF^lWkS$&fgk!hN3b(HR0{=uyT}jhtQq(Q;I9#h&Av}^>6q2&f;Foo3cw8rgTwA{6% z%w4v$lwM+zN?WhEOKX<8mXt3{?P2dc0Sa%}ahK^rf?m2@aWAg8eSxdOefy$CE6S?e zRjvgUWh9logQTuK#I#1+_}&tyQ@)IZUNAy$CmF`j>q+<{D|dsZrN#4wFJz&2rKGa5 zH=?AXxU>L0V1n&K^dc8Bws-=0VJu{tl%BVcOW_ z3(mo1(;ID4busvw!cM%ZCY45b6qhM37>@hkJZ{kANfr1Amd&Q00@E)Dvp((s7%juOn!#D~sdeKfQ#_h(Ne#Ddz%ECWB z`1A{2^Fzi+6(jsIY|?dJ#Fa57$ev;V@sQY!hs1E=MmIed#*^jTJrs!>GZNK;YZ+A4gB)l#5oU06iLTh!)=K_hYpHeIleq6lkWkQhN1aKMe<4i z{~K^*za;siI@CFC}Sr{go@M2TamFGP6MEOSgDW9Kh!OC=ak}8+;BaW Z7C&-aNlVjxH2$ZSa*9jS7}A=`e*^3??EC-# literal 0 HcmV?d00001 diff --git a/examples/09_burn_10kg_fat/example.c b/examples/09_burn_10kg_fat/example.c new file mode 100644 index 0000000..62b3ab3 --- /dev/null +++ b/examples/09_burn_10kg_fat/example.c @@ -0,0 +1,48 @@ +#include "../../squiggle.h" +#include +#include +#include +#include + +double sample_minutes_per_day_jumping_rope_needed_to_burn_10kg(uint64_t* seed) +{ + double kcal_jumping_rope_minute = sample_to(15, 20, seed); + double kcal_jumping_rope_hour = kcal_jumping_rope_minute * 60; + + double kcal_in_kg_of_fat = 7700; + double num_kg_of_fat_to_lose = 10; + + double hours_jumping_rope_needed = kcal_in_kg_of_fat * num_kg_of_fat_to_lose / kcal_jumping_rope_hour; + + double days_until_end_of_year = 152; // as of 2023-08-01 + double hours_per_day = hours_jumping_rope_needed / days_until_end_of_year; + double minutes_per_day = hours_per_day * 60; + return minutes_per_day; +} + +int main() +{ + // set randomness seed + uint64_t* seed = malloc(sizeof(uint64_t)); + *seed = 1000; // xorshift can't start with 0 + + int n = 1000 * 1000; + + double* result = malloc(sizeof(double) * n); + for (int i = 0; i < n; i++) { + result[i] = sample_minutes_per_day_jumping_rope_needed_to_burn_10kg(seed); + } + + printf("How many minutes per day do I have to jump rope to lose 10kg of fat by the end of the year?\n"); + printf("Mean: %f\n", array_mean(result, n)); + printf("A few samples: [ "); + for (int i = 0; i < 9; i++) { + printf("%.6f, ", result[i]); + } + printf("... ]\n"); + + struct c_i c_i_90 = get_90_confidence_interval(sample_minutes_per_day_jumping_rope_needed_to_burn_10kg, seed); + printf("90%% confidence interval: [%f, %f]\n", c_i_90.low, c_i_90.high); + + free(seed); +} diff --git a/examples/09_burn_10kg_fat/makefile b/examples/09_burn_10kg_fat/makefile new file mode 100644 index 0000000..ef385f7 --- /dev/null +++ b/examples/09_burn_10kg_fat/makefile @@ -0,0 +1,53 @@ +# Interface: +# make +# make build +# make format +# make run + +# Compiler +CC=gcc +# CC=tcc # <= faster compilation + +# Main file +SRC=example.c ../../squiggle.c +OUTPUT=example + +## Dependencies +MATH=-lm + +## Flags +DEBUG= #'-g' +STANDARD=-std=c99 +WARNINGS=-Wall +OPTIMIZED=-O3 #-Ofast +# OPENMP=-fopenmp + +## Formatter +STYLE_BLUEPRINT=webkit +FORMATTER=clang-format -i -style=$(STYLE_BLUEPRINT) + +## make build +build: $(SRC) + $(CC) $(OPTIMIZED) $(DEBUG) $(SRC) $(MATH) -o $(OUTPUT) + +format: $(SRC) + $(FORMATTER) $(SRC) + +run: $(SRC) $(OUTPUT) + OMP_NUM_THREADS=1 ./$(OUTPUT) && echo + +time-linux: + @echo "Requires /bin/time, found on GNU/Linux systems" && echo + + @echo "Running 100x and taking avg time $(OUTPUT)" + @t=$$(/usr/bin/time -f "%e" -p bash -c 'for i in {1..100}; do $(OUTPUT); done' 2>&1 >/dev/null | grep real | awk '{print $$2}' ); echo "scale=2; 1000 * $$t / 100" | bc | sed "s|^|Time using 1 thread: |" | sed 's|$$|ms|' && echo + +## Profiling + +profile-linux: + echo "Requires perf, which depends on the kernel version, and might be in linux-tools package or similar" + echo "Must be run as sudo" + $(CC) $(SRC) $(MATH) -o $(OUTPUT) + sudo perf record ./$(OUTPUT) + sudo perf report + rm perf.data