utl

package
v0.0.0-...-1b5bcbe Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 1, 2016 License: BSD-3-Clause Imports: 16 Imported by: 0

Documentation

Overview

package utl implements functions for simplifying calculations and allocation of structures such as slices and slices of slices. It also contains functions for sorting quantities.

Index

Constants

View Source
const (
	KBSIZE = 1024.0
	MBSIZE = 1048576.0
	GBSIZE = 1073741824.0
)
View Source
const PRINTDEEPZERO = 1e-3

Variables

This section is empty.

Functions

func BestSquare

func BestSquare(size int) (nrow, ncol int)

BestSquare finds the best square for given size=Nrows * Ncolumns

func BoolAllFalse

func BoolAllFalse(values []bool) bool

BoolAllFalse returns true if all values are false

func BoolAllTrue

func BoolAllTrue(values []bool) bool

BoolAllTrue returns true if all values are true

func Cross3d

func Cross3d(w, u, v []float64)

Cross3d computes the cross product of two 3D vectors u and w

w = u cross v
Note: w must be pre-allocated

func CumSum

func CumSum(cs, p []float64)

CumSum returns the cumulative sum of the elements in p

Input:
 p -- values
Output:
 cs -- cumulated sum; pre-allocated with len(cs) == len(p)

func Dbl2Str

func Dbl2Str(v []float64, format string) (s []string)

Dbl2Str converts a slice of doubles (float64) to a slice of strings

func DblArgMinMax

func DblArgMinMax(v []float64) (imin, imax int)

DblArgMinMax finds the indices of min and max arguments

func DblArrayToMat

func DblArrayToMat(v []float64, m, n int) (a [][]float64)

DblArrayToMat converts a column-major array to a matrix

func DblCopy

func DblCopy(in []float64) (out []float64)

DblCopy gets a copy of slice of doubles

func DblDsort3

func DblDsort3(a, b, c *float64)

DblDsort3 sorts 3 values in descending order

func DblGetSorted

func DblGetSorted(A []float64) (Asorted []float64)

DblGetSorted returns a sorted (increasing) copy of 'A'

func DblMatToArray

func DblMatToArray(a [][]float64) (v []float64)

DblMatToArray converts a matrix into a column-major array

func DblMinMax

func DblMinMax(v []float64) (mi, ma float64)

DblMinMax returns the maximum and minimum elements in v

NOTE: this is not efficient and should be used for small slices only

func DblOnes

func DblOnes(n int) (res []float64)

DblOnes generates a slice of double precision '1s'

func DblSort3

func DblSort3(a, b, c *float64)

DblSort3 sorts 3 values in ascending order

func DblSort4

func DblSort4(a, b, c, d *float64)

DblSort4 sort four values in ascending order

func DblSplit

func DblSplit(s string) (r []float64)

DblSplit splits a string into floats

func DblSum

func DblSum(v []float64) (sum float64)

DblSum sums all items in v

NOTE: this is not efficient and should be used for small slices only

func DblVals

func DblVals(n int, v float64) (res []float64)

DblVals generates a slice of double precision values

func DblsAlloc

func DblsAlloc(m, n int) (mat [][]float64)

DblsAlloc allocates a matrix of float64

func DblsGetColumn

func DblsGetColumn(j int, v [][]float64) (x []float64)

DblsGetColumn returns the column of a matrix of reals numbers

func DblsParetoMin

func DblsParetoMin(u, v []float64) (u_dominates, v_dominates bool)

DblsParetoMin compares two vectors using Pareto's optimal criterion

Note: minimum dominates (is better)

func DblsParetoMinProb

func DblsParetoMinProb(u, v []float64, φ float64) (u_dominates bool)

DblsParetoMinProb compares two vectors using Pareto's optimal criterion φ ∃ [0,1] is a scaling factor that helps v win even if it's not smaller. If φ==0, deterministic analysis is carried out. If φ==1, probabilistic analysis is carried out. As φ → 1, v "gets more help".

Note: (1) minimum dominates (is better)
      (2) v dominates if !u_dominates

func Deep3Deserialize

func Deep3Deserialize(I, P []int, S []float64, debug bool) (A [][][]float64)

Deep3Deserialize deserializes an array of array of array in column-compressed format

Example:

func Deep3GetInfo

func Deep3GetInfo(I, P []int, S []float64, verbose bool) (nitems, nrows, ncols_tot int, ncols []int)

Deep3GetInfo returns information of serialized array of array of array

Example:

func Deep3Serialize

func Deep3Serialize(A [][][]float64) (I, P []int, S []float64)

Deep3Serialize serializes an array of array of array in column-compressed format

Example:

func Deep3alloc

func Deep3alloc(n1, n2, n3 int) (a [][][]float64)

Deep3alloc allocates a slice of slice of slice

func Deep3set

func Deep3set(a [][][]float64, v float64)

Deep3set sets deep slice of slice of slice with v values

func Deep4alloc

func Deep4alloc(n1, n2, n3, n4 int) (a [][][][]float64)

Deep4alloc allocates a slice of slice of slice of slice

func Deep4set

func Deep4set(a [][][][]float64, v float64)

Deep4set sets deep slice of slice of slice of slice with v values

func Digits

func Digits(maxint int) (ndigits int, format string)

Digits returns the nubmer of digits

func DoProf

func DoProf(silent bool, option int) func()

DoProf runs either CPU profiling or MEM profiling

Input:
  silent -- show message
  option -- 1=CPU profiling, 2=memory profiling
Output:
  1) returns a "stop()" function to be called before shutting down
  2) output files are saved to "/tmp/gosl/cpu.pprof"; or
                               "/tmp/gosl/mem.pprof"
  3) run analysis with (e.g.):
       go tool pprof binary /tmp/gosl/mem.pprof

func Dot3d

func Dot3d(u, v []float64) (s float64)

Dot3d returns the dot product between two 3D vectors

func DurSum

func DurSum(v []time.Duration) (seconds float64)

DurSum sums all seconds in v

NOTE: this is not efficient and should be used for small slices only

func Expon

func Expon(val float64) (ndigits int)

Expon returns the exponent

func FlipCoin

func FlipCoin(p float64) bool

FlipCoin generates a Bernoulli variable; throw a coin with probability p

func GetITout

func GetITout(all_output_times, time_stations_out []float64, tol float64) (I []int, T []float64)

GetITout returns indices and output times

Input:
  all_output_times  -- array with all output times. ex: [0,0.1,0.2,0.22,0.3,0.4]
  time_stations_out -- time stations for output: ex: [0,0.2,0.4]
  tol               -- tolerance to compare times
Output:
  iout -- indices of times in all_output_times
  tout -- times corresponding to iout
Notes:
  use -1 in all_output_times to enforce output of last timestep

func GtPenalty

func GtPenalty(x, b, penaltyM float64) float64

GtPenalty implements a 'greater than' penalty function where x must be greater than b; otherwise the error is magnified

func GtePenalty

func GtePenalty(x, b, penaltyM float64) float64

GtePenalty implements a 'greater than or equal' penalty function where x must be greater than b or equal to be; otherwise the error is magnified

func Imax

func Imax(a, b int) int

Imin returns the maximum between two integers

func Imin

func Imin(a, b int) int

Imin returns the minimum between two integers

func IntAddScalar

func IntAddScalar(a []int, s int) (res []int)

IntAddScalar adds a scalar to all values in a slice of integers

func IntBoolMapSort

func IntBoolMapSort(m map[int]bool) (sorted_keys []int)

func IntFill

func IntFill(s []int, val int)

IntFill fills a slice of integers

func IntFilter

func IntFilter(a []int, out func(idx int) bool) (res []int)

IntFilter filters out components in slice

NOTE: this function is not efficient and should be used with small slices only

func IntIndexSmall

func IntIndexSmall(a []int, val int) int

IntIndexSmall finds the index of an item in a slice of ints

NOTE: this function is not efficient and should be used with small slices only; say smaller than 20

func IntIndexSorted

func IntIndexSorted(a []int, val int, aSortedIn []int) (idx int, aSortedOut []int)

IntIndexSorted finds the index of an item in a slice of ints by means of searching a sorted list (given or computed here)

Input:
 a         -- the list
 val       -- value to be searched for
 aSortedIn -- an existing sorted 'a' list or <nil>
Output:
 idx        -- position in 'a' where val is located if found; otherwise returns -1
 aSortedOut -- sorted list if the input aSortedIn is <nil>
Note: If you were to sort the array with MergeSort or any other O(nlogn) algorithm then
      the complexity would be O(nlogn).
      O(logn) < O(n) < O(nlogn)

TODO: this function is finished yet

func IntIntsMapAppend

func IntIntsMapAppend(m *map[int][]int, key int, item int)

IntIntsMapAppend appends a new item to a map of slice.

Note: this function creates a new slice in the map if key is not found.

func IntMinMax

func IntMinMax(v []int) (mi, ma int)

IntMinMax returns the maximum and minimum elements in v

NOTE: this is not efficient and should be used for small slices only

func IntNegOut

func IntNegOut(a []int) (res []int)

IntNegOut filters out negative components in slice

NOTE: this function is not efficient and should be used with small slices only

func IntPy

func IntPy(a []int) (res string)

IntPy returns a Python string representing a slice of integers

func IntRange

func IntRange(n int) (res []int)

IntRange generates a slice of integers from 0 to n-1

func IntRange2

func IntRange2(start, stop int) []int

IntRange2 generates slice of integers from start to stop (but not stop)

func IntRange3

func IntRange3(start, stop, step int) (res []int)

IntRange3 generates a slice of integers from start to stop (but not stop), afer each 'step'

func IntSort3

func IntSort3(a, b, c *int)

IntSort3 sorts 3 values in ascending order

func IntSort4

func IntSort4(a, b, c, d *int)

IntSort4 sort four values in ascending order

func IntUnique

func IntUnique(slices ...[]int) (res []int)

IntUnique returns a unique and sorted slice of integers

func IntVals

func IntVals(n int, val int) (s []int)

IntVals allocates a slice of integers with size==n, filled with val

func IntsAlloc

func IntsAlloc(m, n int) (mat [][]int)

IntsAlloc allocates a matrix of integers

func IntsClone

func IntsClone(a [][]int) (b [][]int)

IntsClone allocates and clones a matrix of integers

func L2norm

func L2norm(p, q []float64) (dist float64)

L2norm returns the Euclidean distance between p and q

func LinSpace

func LinSpace(start, stop float64, num int) (res []float64)

LinSpace returns evenly spaced numbers over a specified closed interval.

func LinSpaceOpen

func LinSpaceOpen(start, stop float64, num int) (res []float64)

LinSpaceOpen returns evenly spaced numbers over a specified open interval.

func Max

func Max(a, b float64) float64

Max returns the maximum between two float point numbers

func MeshGrid2D

func MeshGrid2D(xmin, xmax, ymin, ymax float64, nx, ny int) (x, y [][]float64)

MeshGrid2D creates a grid with x-y coordinates

x -- [ny][nx]
y -- [ny][nx]

func Min

func Min(a, b float64) float64

Min returns the minimum between two float point numbers

func ParetoFront

func ParetoFront(Ovs [][]float64) (front []int)

ParetoFront computes the Pareto optimal front

Input:
 Ovs -- [nsamples][ndim] objective values
Output:
 front -- indices of pareto front
Note: this function is slow for large sets

func PrintDeep3

func PrintDeep3(name string, A [][][]float64)

PrintDeep3 prints an array of array of array

func PrintDeep4

func PrintDeep4(name string, A [][][][]float64, format string)

PrintDeep4 prints an array of array of array

func PrintMemStat

func PrintMemStat(msg string)

PrintMemStat prints memory statistics

func ProbContestSmall

func ProbContestSmall(u, v, φ float64) float64

ProbContestSmall computes the probability for a contest between u and v where u wins if it's the smaller value. φ ∃ [0,1] is a scaling factor that helps v win even if it's not smaller. If φ==0, deterministic analysis is carried out. If φ==1, probabilistic analysis is carried out. As φ → 1, v "gets more help".

func ProfCPU

func ProfCPU(dirout, filename string, silent bool) func()

ProfCPU activates CPU profiling

Note: returns a "stop()" function to be called before shutting down

func ProfMEM

func ProfMEM(dirout, filename string, silent bool) func()

ProfMEM activates memory profiling

Note: returns a "stop()" function to be called before shutting down

func Scaling

func Scaling(s, x []float64, ds, tol float64, reverse, useinds bool) (xmin, xmax float64)

Scaling computes a scaled version of the input slice with results in [0.0, 1.0]

Input:
 x       -- values
 ds      -- δs value to be added to all 's' values
 tol     -- tolerance for capturing xmax ≅ xmin
 reverse -- compute reverse series;
            i.e. 's' decreases from 1 to 0 while x goes from xmin to xmax
 useinds -- if (xmax-xmin)<tol, use indices to generate the 's' slice;
            otherwise, 's' will be filled with δs + zeros
Ouptut:
 s          -- scaled series; pre--allocated with len(s) == len(x)
 xmin, xmax -- min(x) and max(x)

func SortQuadruples

func SortQuadruples(i []int, x, y, z []float64, by string) (I []int, X, Y, Z []float64, err error)

SortQuadruples sorts i, x, y, and z by "i", "x", "y", or "z"

Note: either i, x, y, or z can be nil; i.e. at least one of them must be non nil

func Str2Dbl

func Str2Dbl(s []string) (v []float64)

Str2Dbl converts a slice of strings to a slice of doubles (float64)

func StrBoolMapSort

func StrBoolMapSort(m map[string]bool) (sorted_keys []string)

func StrBoolMapSortSplit

func StrBoolMapSortSplit(m map[string]bool) (sorted_keys []string, sorted_vals []bool)

func StrDblMapSort

func StrDblMapSort(m map[string]float64) (sorted_keys []string)

func StrDblMapSortSplit

func StrDblMapSortSplit(m map[string]float64) (sorted_keys []string, sorted_vals []float64)

func StrDblsMapAppend

func StrDblsMapAppend(m *map[string][]float64, key string, item float64)

StrDblsMapAppend appends a new item to a map of slice.

Note: this function creates a new slice in the map if key is not found.

func StrIndexSmall

func StrIndexSmall(a []string, val string) int

StrIndexSmall finds the index of an item in a slice of strings

NOTE: this function is not efficient and should be used with small slices only; say smaller than 20

func StrIntMapSort

func StrIntMapSort(m map[string]int) (sorted_keys []string)

func StrIntMapSortSplit

func StrIntMapSortSplit(m map[string]int) (sorted_keys []string, sorted_vals []int)

func StrIntsMapAppend

func StrIntsMapAppend(m *map[string][]int, key string, item int)

StrIntsMapAppend appends a new item to a map of slice.

Note: this function creates a new slice in the map if key is not found.

func StrVals

func StrVals(n int, val string) (s []string)

StrVals allocates a slice of strings with size==n, filled with val

func StrsAlloc

func StrsAlloc(m, n int) (mat [][]string)

StrsAlloc allocates a matrix of strings

Types

type DblList

type DblList struct {
	Vals [][]float64 // values
}

DblList implements a tabular list with variable number of columns

Example:
  Vals = [][]float64{
           {0.0},
           {1.0, 1.1, 1.2, 1.3},
           {2.0, 2.1},
           {3.0, 3.1, 3.2},
         }

func (*DblList) Append

func (o *DblList) Append(rowidx int, value float64)

Append appends items to DblList

type DblSlist

type DblSlist struct {
	Vals []float64 // values
	Ptrs []int     // pointers
}

DblSlist implements a tabular list with variable number of columns using a serial representation

Example:
    0.0
    1.0  1.1  1.2  1.3
    2.0  2.1
    3.0  3.1  3.2
becomes:
           (0)   (1)    2    3    4   (5)    6   (7)    8    9   (10)
    Vals = 0.0 | 1.0  1.1  1.2  1.3 | 2.0  2.1 | 3.0  3.1  3.2 |
    Ptrs = 0 1 5 7 10
Notes:
    len(Ptrs) = nrows + 1
    Ptrs[len(Ptrs)-1] = len(Vals)

func (*DblSlist) Append

func (o *DblSlist) Append(startRow bool, value float64)

Append appends item to DblSlist

func (DblSlist) Print

func (o DblSlist) Print(fmt string)

Print prints the souble-serial-list

type Decoder

type Decoder interface {
	Decode(e interface{}) error
}

Decoder defines decoders; e.g. gob or json

func GetDecoder

func GetDecoder(r goio.Reader, enctype string) Decoder

GetDecoder returns a new decoder

type Encoder

type Encoder interface {
	Encode(e interface{}) error
}

Encoder defines encoders; e.g. gob or json

func GetEncoder

func GetEncoder(w goio.Writer, enctype string) Encoder

GetEncoder returns a new encoder

type Quadruple

type Quadruple struct {
	I int
	X float64
	Y float64
	Z float64
}

Quadruple helps to sort a quadruple of 1 int and 3 float64s

type Quadruples

type Quadruples []*Quadruple

Quadruples helps to sort quadruples

func BuildQuadruples

func BuildQuadruples(i []int, x, y, z []float64) (q Quadruples)

Init initialise Quadruples with i, x, y, and z

Note: i, x, y, or z can be nil; but at least one of them must be non nil

func (Quadruples) I

func (o Quadruples) I() (i []int)

I returns the 'i' in quadruples

func (Quadruples) Len

func (o Quadruples) Len() int

Len returns the length of Quadruples

func (Quadruples) String

func (o Quadruples) String() string

String returns the string representation of Quadruples

func (Quadruples) Swap

func (o Quadruples) Swap(i, j int)

Swap swaps two quadruples

func (Quadruples) X

func (o Quadruples) X() (x []float64)

X returns the 'x' in quadruples

func (Quadruples) Y

func (o Quadruples) Y() (y []float64)

Y returns the 'y' in quadruples

func (Quadruples) Z

func (o Quadruples) Z() (z []float64)

Z returns the 'z' in quadruples

type QuadruplesByI

type QuadruplesByI struct{ Quadruples }

Sort Quadruples by I

func (QuadruplesByI) Less

func (o QuadruplesByI) Less(i, j int) bool

type QuadruplesByX

type QuadruplesByX struct{ Quadruples }

Sort Quadruples by X

func (QuadruplesByX) Less

func (o QuadruplesByX) Less(i, j int) bool

type QuadruplesByY

type QuadruplesByY struct{ Quadruples }

Sort Quadruples by Y

func (QuadruplesByY) Less

func (o QuadruplesByY) Less(i, j int) bool

type QuadruplesByZ

type QuadruplesByZ struct{ Quadruples }

Sort Quadruples by Z

func (QuadruplesByZ) Less

func (o QuadruplesByZ) Less(i, j int) bool

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL