Skip to content

Instantly share code, notes, and snippets.

View ivan-pi's full-sized avatar

Ivan Pribec ivan-pi

View GitHub Profile
@ivan-pi
ivan-pi / linspace.f90
Created December 19, 2019 18:54
Linspace in Fortran
module linspace_mod
use iso_fortran_env, only: dp => real64
implicit none
contains
!>
! Return evenly spaced numbers over a specified interval.
!
! Returns `num` evenly spaced samples, calculated over the interval `[start, stop]`.
subroutine C_F_STRPOINTER (STRARRAY, FSTRPTR, MAXLEN)
use, intrinsic :: ISO_C_BINDING
implicit none
character, dimension(*), target, intent(in) :: STRARRAY
character(:), pointer, intent(out) :: FSTRPTR
integer, intent(in), optional :: MAXLEN
integer :: curlen
curlen = 0
@ivan-pi
ivan-pi / continued_fractions.py
Created June 26, 2020 12:23
Basic functions for working with continued fractions. These are handy for approximating irrational numbers as ratios of integer. A Fortran solution is available at https://fortran-lang.discourse.group/t/implmentation-of-contnued-fraction/124/2
import math
from fractions import Fraction
def continued_fraction(r,steps=10):
cf = []
for step in range(steps):
i = math.floor(r)
cf.append(i)
@ivan-pi
ivan-pi / cbrt_1.f90
Created June 28, 2020 12:07
Cube root code example 1
module cbrt_mod1
implicit none
private
public :: cbrt, sp, r
interface cbrt
module procedure cbrt_sp_sp
module procedure cbrt_csp_csp
@ivan-pi
ivan-pi / cbrt_2.f90
Created June 28, 2020 12:09
Cube root code example 2
module cbrt_mod2
implicit none
private
public :: cbrt, sp
interface cbrt
module procedure cbrt_sp_sp
module procedure cbrt_sp_csp
@ivan-pi
ivan-pi / ensemble_kf.f90
Created July 9, 2020 16:01
Ensemble Kalman filter from Evensen, G. (2003). The ensemble Kalman filter: Theoretical formulation and practical implementation. Ocean dynamics, 53(4), 343-367.
module m_multa
implicit none
private
public :: multa
contains
subroutine multa(A,X,ndims,nrens,iblkmax)
@ivan-pi
ivan-pi / cbrt_libm_example.f90
Created July 10, 2020 08:46
Port of the libm cbrt function (https://svnweb.freebsd.org/base/head/lib/msun/src/s_cbrt.c?revision=342563&view=markup) from C to Fortran. This code has not been verified. Use at your own risk!
! @(#)s_cbrt.c 5.1 93/09/24
!
! ====================================================
! Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
!
! Developed at SunPro, a Sun Microsystems, Inc. business.
! Permission to use, copy, modify, and distribute this
! software is freely granted, provided that this notice
! is preserved.
! ====================================================
@ivan-pi
ivan-pi / spline_test_driver.f90
Created November 13, 2020 01:28
Example for scicomp.stackexchange thread "Factorization of cubic spline interpolation matrix"
! For an explanation see:
! https://scicomp.stackexchange.com/questions/36261/factorization-of-cubic-spline-interpolation-matrix
!
! compile with:
! gfortran -Wall spline_test_driver.f90 -o spline_test_driver -llapack
!
module spline_test
implicit none
@ivan-pi
ivan-pi / polynomial_companion_matrix.f90
Created November 18, 2020 20:59
Solve the roots of a cubic polynomial using the polynomial companion matrix (depends on LAPACK routine SGEEV)
! Compile with:
! $ gfortran -Wall polynomial_companion_matrix.f90 -o polynomial_companion_matrix -llapack
!
! To run the code:
! $ ./polynomial_companion_matrix
!
program main
implicit none
integer, parameter :: sp = kind(1.0)
integer, parameter :: n = 3
@ivan-pi
ivan-pi / polyfit.f90
Created November 19, 2020 01:19
Polyfit function following the implementation in numpy: https://github.com/numpy/numpy/blob/v1.19.0/numpy/lib/polynomial.py#L429-L658
module polyfit_mod
implicit none
contains
function vander(x,N,increasing) result(v)
real, intent(in) :: x(:)
integer, intent(in) :: N
logical, intent(in), optional :: increasing