Skip to content

Instantly share code, notes, and snippets.

View SigurdJanson's full-sized avatar

Jan SigurdJanson

View GitHub Profile
@SigurdJanson
SigurdJanson / reversiontest.R
Last active January 14, 2020 16:34
ReversionTest
#' ReversionTest
#' Tests the precision of two reciprocal functions. It evaluates the differences between
#' a computed value \eqn{x'} and an expected value \eqn{x} after \eqn{x' = finv(f(x))}.
#' @param f,finv The functions to be tested for precision with finv the inverse function for f.
#' @param ToIterate a list of vectors, on for each variable.
#' @param KeyVar The variable in ToIterate for which the test is for. Either an list index
#' or a name as string (partial strings allowed).
#' @param ... additional arguments to be passed to f and finv.
#' @param DiffFunc A function to quantify the difference between expected
#' value and computed value. If Null, ReversionTest will use the absolute ratio.
#' SaturationThold
#' Where does an asymptotic function run into saturation? 'SaturationThold'
#' determines the practical range of definition of a function.
#' Though a function may theoretically approach to a limit asymptotically,
#' an implemented function ususally does not work along the full range of
#' possible input values. The limitations in the precision of floating point
#' numbers will most likely cause a function to run into saturation (long)
#' before the input values reach the maximum possible values.
#' @param f A function. It's first argument will be used to find the
#' saturation threshold.
@SigurdJanson
SigurdJanson / polyPDF.R
Last active March 13, 2020 18:54
Approximate a probability distribution function (PDF) from the moments
#' polyPDF
#' This function constructs a PD function from the moments. The output of this function is the PDF.
#' @param moments data frame, containing the moment order ("n") in one column
#' and their values ("moments") in the other
#' @param xmin,xmax Lower and upper limit of the PDF.
#' @param scale Scale factor can be used to transform data avoiding singularity (default = 1).
#' @param disp If TRUE, the PDF will be plotted and the polynomial coefficients are shown.
#' @author Hugo Hernandez; small refactoring by Jan Seifert
#' @reference Hernandez, H. (2018). Comparison of Methods for the Reconstruction
#' of Probability Density Functions from Data Samples. Technical Report
@SigurdJanson
SigurdJanson / DoubleFactorial.R
Created March 13, 2020 18:52
A clean version of the double factorial function including unit tests.
#' Double factorial function
#' The double factorial is the product of all the integers from 1 up to x
#' that have the same parity (odd or even) as x (see https://t1p.de/byye).
#'
#' @param x Numeric vector. Will be coerced to integer.
#'
#' @note dfactorial is defined for x in [0, 300]
#' @references Gould, H. & Quaintance, J. (2012). Double Fun with Double
#' Factorials. Mathematics Magazine, 85(3), 177-192. doi:10.4169/math.mag.85.3.177
dfactorial <- function(x) {
@SigurdJanson
SigurdJanson / nmkb_debug.R
Created May 4, 2020 19:18
Testing Results of dfoptim::nmkb (version 2018.2-1)
library(dfoptim)
fopt <- "dfoptim"
# Choose objective function ----
rosbkext <- function(x) {
# Extended Rosenbrock function
n <- length(x)
sum (100*(x[1:(n-1)]^2 - x[2:n])^2 + (x[1:(n-1)] - 1)^2)
}
# moved rastrigin by 1 so that the global optimum is at rep(1, 6)
@SigurdJanson
SigurdJanson / ActivityIndicatorButton.R
Last active May 11, 2021 17:38
How to display an activity indicator on a button to tell users that the app is busy with handling request (shiny, R)
# This sample shows how to display an activity indicator on a button to tell users that
# the app is busy with handling their recent request.
#
# The activity indicator works with CSS only.
#
# Based on the template project "Shiny Web Application" of RStudio.
# The difference is that the chart is not updated as soon as a user changes the bins slider but
# rather waits until an update is explicitely request through a button press.
# The CSS animation has been taken from https://maxbeier.github.io/text-spinners/
library(shiny)
@SigurdJanson
SigurdJanson / RoundedIntDiv.R
Last active January 23, 2021 17:56
Specialized integer division `x1 / x2` that correctly rounds to the nearest value (in R)
#' `%./%`
#' Specialized integer division `x1 / x2` that correctly rounds to the
#' nearest value.
#' @param x1,x2 numerator and denominator (integer)
#' @return integer
#' @source https://stackoverflow.com/questions/36377244/make-int-round-off-to-nearest-value/36377365
`%./%` <- function(x1, x2) {
return( x1 %/% x2 + (x1 %% x2) %/% (x2 %/% 2L + x2 %% 2L) )
}
@SigurdJanson
SigurdJanson / JSON_Deserialise_IReadOnlyList.cs
Last active September 29, 2021 16:22
De-serialise IReadOnlyList property in C# that cannot be set outside the class.
/*
How to de-serialise a property that returns an IReadOnlyList and shall not be set
from outside?
*/
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
public class PersonClass
@SigurdJanson
SigurdJanson / InkscapeBatchConversion.bat
Last active May 18, 2022 18:04
Inkscape Batch Conversion
:: Convert vector file formats in the current folder and their subfolders
:: Adaptation of Johannes Demls great batch script to convert vector graphic files with InkScape in batch mode
:: Original taken from https://gist.github.com/JohannesDeml/779b29128cdd7f216ab5000466404f11
:: Please refer to the original site for instructions
:: This script can be found at: https://gist.github.com/SigurdJanson/ddccf14d41ecbd490fd9070dd1ec13d6
@Echo off
setlocal ENABLEDELAYEDEXPANSION
:: Possible paths to check for the installation
@SigurdJanson
SigurdJanson / Type1_Assigment.razor
Created June 6, 2022 15:51
Blazor RenderFragments
// TYPE 1
protected RenderFragment RenderThis = @<span title = "I was a render fragment, once" class="fragged">
You can use me many times without per-component overhead
</span>;