/* COPYRIGHT NOTICE Copyright (C) 2014, 2015 Edwin L. (Ted) Woollett http://www.csulb.edu/~woollett k2util.mac is a utility file associated with Ch. 2 of Computational Physics with Maxima or R, Initial Value Problems. See cp2.pdf for more info. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details at http://www.gnu.org/copyleft/gpl.html */ /* k2util.mac May, 2014 */ head(mylist) := block([numL,nleft:6], numL : length(mylist), rest (mylist, -(numL - nleft)))$ tail(mylist) := block([numL,nleft:6], numL : length(mylist), rest (mylist, numL - nleft))$ take(%aL,%nn) := (map(lambda([x],part(x,%nn)), %aL))$ range(aaL) := print(" min = ",lmin(aaL)," max = ", lmax(aaL))$ fll(x) := [first(x),last(x),length(x)]$ jitter1(val,amount) := val + amount*(random(2.0) - 1.0)$ /* add random noise to a list of numbers */ jitter(aaL, amount) := ( if not listp(aaL) then return("first arg must be a list"), map('lambda([xx],jitter1(xx,amount)),aaL))$ /* mean value of a list of numbers */ mymean(bL) := block([numer],numer:true, apply("+", bL)/length(bL))$ /* MA_smooth is basic function for smoothing values of a time series (say), using a "window" which should preferably be an odd integer, such as 3. The length of the returned list of "smoothed numbers" is less than the length of the given list seriesL */ MA_smooth(seriesL,window) := block([stsL:[],i,j:1,nearL], if not listp(seriesL) then return("first arg to MA_smooth must be a list"), for i:window thru length(seriesL) do ( nearL : makelist(seriesL[k],k,j,i), stsL : cons(mymean(nearL), stsL), j : j + 1), reverse(stsL))$ xyData(uuL,vvL) := (makelist([uuL[kk],vvL[kk]],kk,1,length(uuL)))$ /* "logistic function": logistic(x,1,100) is an analytic "step function", rising rapidly from zero to unity near x = 0. logistic(x-3,1,100) rises near x=3 */ logistic(x,a,b) := 1/(1+a*exp(-b*x))$ /* match only tested with integers, and: pos, neg, zero. if mn not found in list abL, then returns 0 else returns position of first match found. */ match(mn,abL) := block([ pos:0 ], if not member(mn,abL) then return(pos), for j thru length(abL) do ( if mn = abL[j] then ( pos : j, return())), pos)$ /* (%i2) myL : [0,1,1,1,0,-1,-1,-1,1,1,1]$ (%i6) match(0,myL); (%o6) 1 (%i7) match(1,myL); (%o7) 2 (%i8) match(-1,myL); (%o8) 6 (%i9) match(2,myL); (%o9) 0 (%i15) myLs : map('sign,myL); (%o15) [zero,pos,pos,pos,zero,neg,neg,neg,pos,pos,pos] (%i16) match(zero,myLs); (%o16) 1 (%i17) match(pos,myLs); (%o17) 2 (%i18) match(neg,myLs); (%o18) 6 */ /* to_integer converts words zero,pos,neg to integers */ to_integer(aa) := (if aa = pos then 1 else if aa = neg then -1 else 0)$ /* int_sign(list) converts a list of numbers to a list of the elements 0,1,-1 */ int_sign(aaL) := (map('to_integer, map('sign, aaL)))$ /* (%i31) xL : [0,1.1,2.2,1.1,-1.1,-1.1]$ (%i32) int_sign(xL); (%o32) [0,1,1,1,-1,-1] */ ratprint:false$