1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
|
/*
* emulate_yeti.i --
*
* Interpreted functions which emulate some builtin functions of Yeti.
*
* Copyright (c) 2005-2007, Eric Thiebaut, Observatoire de Lyon (France).
*
* Routines:
* swap - exchanges contents of two variables.
* unref - returns X, destroying X in the process.
* is_vector - check if argument is a vector.
* is_matrix - check if argument is a matrix.
* is_real - check if argument is of float or double type.
* is_complex - check if argument is of complex type.
* is_integer - check if argument is of integer type.
* is_numerical - check if argument is of numerical type.
* make_dimlist - build-up dimension list
*
* History:
* $Id: emulate_yeti.i,v 1.2 2010/04/07 06:15:23 paumard Exp $
* $Log: emulate_yeti.i,v $
* Revision 1.2 2010/04/07 06:15:23 paumard
* - remove strchr, strrchr, is_scalar and is_vector, they are in string.i (beware, no autoloads) ;
* - move is_integer_scalar from utils.i to emulate_yeti.i
*
* Revision 1.1 2010/04/06 15:36:09 paumard
* - move emulate-yeti*.i to emulate_yeti*.i
*
* Revision 1.2 2010/04/06 14:21:51 paumard
* - move strlower & strupper from utils.i to emulate-yeti.i;
* - move round from util_fr.i to emulate-yeti.i;
* - round returns a double, like the Yeti implementation;
* - review autoloads (adding emulate-yeti_start.i);
* - add missing files to Makefile.
*
* Revision 1.1 2010/02/10 13:27:12 paumard
* - Synchronize files with Eric Thiebaut's: fft_utils.i, img.i, plot.i, utils.i.
* - Import emulate_yeti.i
* - Remove basename() and dirname(), standard in pathfun.i.
* - Remove the accents in Erics name to prevent a crash on amd64.
*
* Revision 1.1 2007/04/24 06:58:44 eric
* Initial revision
*
*
*-----------------------------------------------------------------------------
*/
if (is_func(yeti_init)) {
error, "This file define functions redundant with Yeti package.";
}
func is_integer_scalar(x)
/* DOCUMENT is_integer_scalar(x)
Check whether or not X is an integer scalar.
SEE ALSO is_scalar, is_integer. */
{ return (((s=structof(x))==long || s==int || s==short || s==char) &&
! dimsof(x)(1)); }
// The following requires Yorick >= 1.6.02
func strlower(s) { return strcase(0, s); }
func strupper(s) { return strcase(1, s); }
/* DOCUMENT strlower(s)
-or- strupper(s)
Convert (array of) string(s) S to lower/upper case letters.
SEE ALSO strcase */
local make_dimlist;
func grow_dimlist(&dimlist, arg)
/* DOCUMENT make_dimlist, dimlist, next_argument;
*
* Builds a dimension list DIMLIST, as used in the array function
* (which see). Use like this (all extra arguments in your function
* are considered as dimensions or dimension lists):
*
* func your_function(arg1, arg2, etc, ..)
* {
* dimlist = [0];
* while (more_args()) make_dimlist, dimlist, next_arg();
* ...
* }
*
* After this, DIMLIST will be an array of the form [ndims, dim1,
* dim2,...], compounded from the multiple arguments in the same way
* as the array function. Another possibility is to define your
* function as:
*
* func your_function(arg1, arg2, etc, dimlist, ..)
* {
* while (more_args()) make_dimlist, dimlist, next_arg();
* ...
* }
*
* But in this latter case, if no DIMLIST arguments given, DIMLIST will
* be [] instead of [0], which will act the same in most situations. If
* that possibility is unacceptable, you may add
*
* if (is_void(dimlist)) dimlist= [0];
*
* before/after the while loop.
*
*
* SEE ALSO:
* array, build_dimlist.
*/
{
if (is_array(arg)) {
if (structof((n = arg(1)+0)) == long) {
if (! (d1 = dimsof(arg)(1))) {
if (is_void(dimlist)) {
dimlist = [1, n];
} else {
grow, dimlist, n;
++dimlist(1);
}
return;
} else if (d1 == 1) {
if (is_void(dimlist)) {
dimlist = long(arg);
return;
} else {
if (n == numberof(arg)-1) {
grow, dimlist, long(arg(2:0));
dimlist(1) += n;
return;
}
}
}
error, "bad dimension list";
}
} else if (is_void(arg)) {
if (is_void(dimlist)) dimlist = [0];
return;
}
error, "bad data type in dimension list";
}
if (is_func(make_dimlist) != 2) {
make_dimlist = grow_dimlist; /* for old code */
}
//========================================================================
// the following are builtin function in yorick 2.1.05x, > april 2010.
// we will only redefine these functions if they don't already exist
func __unref(&x) /* interpreted version */
/* DOCUMENT unref(x)
returns X, destroying X in the process (useful to deal with temporary
big arrays). Written after Yorick's FAQ.
SEE ALSO: eq_nocopy, swap. */
{
local y;
eq_nocopy, y, x;
x = [];
return y;
}
if (typeof(unref)!="builtin") unref=__unref;
func __swap(&a, &b) /* interpreted version */
/* DOCUMENT swap, a, b;
Exchanges the contents of variables A and B without requiring any
temporary copy.
SEE ALSO: eq_nocopy, unref. */
{
local tmp;
eq_nocopy, tmp, a;
eq_nocopy, a, b;
eq_nocopy, b, tmp;
}
if (typeof(swap)!="builtin") swap=__swap;
func __is_matrix(x) { return (is_array(x) && dimsof(x)(1) == 2); }
/* DOCUMENT is_matrix(x)
* Returns true if X is a matrix (i.e. a 2-D array).
*
* SEE ALSO: dimsof, is_array, is_integer, is_scalar, is_vector
*/
if (typeof(is_matrix)!="builtin") is_matrix=__is_matrix;
func __is_integer(x) {return ((s=structof(x))==long || s==int || s==char ||
s==short);}
func __is_real(x) {return ((s=structof(x))==double || s==float);}
func __is_complex(x) {return structof(x)==complex;}
func __is_numerical(x) {return ((s=structof(x))==long || s==double || s==int ||
s==char || s==complex || s==short || s==float);}
func __is_string(x) { return structof(x)==string;}
/* DOCUMENT is_integer(x)
* -or- is_real(x)
* -or- is_complex(x)
* -or- is_numerical(x)
* -or- is_string(x)
* These functions return true if X is an array of type: integer, real
* (i.e. double or float), complex, numerical (i.e. integer, real or
* complex) or string.
*
* SEE ALSO: structof, dimsof, is_array, is_scalar.
*/
if (typeof(is_integer)!="builtin") is_integer =__is_integer;
if (typeof(is_real)!="builtin") is_real =__is_real;
if (typeof(is_complex)!="builtin") is_complex =__is_complex;
if (typeof(is_numerical)!="builtin") is_numerical =__is_numerical;
if (typeof(is_string)!="builtin") is_string =__is_string;
func __round(arg)
/* DOCUMENT round(arg)
* Returns the rounded version of a floating point argument
* modified 2007dec06 to fix problem with negative numbers
* F.Rigaut 2001/10, Return double TP 2010/04
* SEE ALSO: ceil, floor
*/
{return double(long(arg+0.5)-(arg<0));}
if (typeof(round)!="builtin") round =__round;
/*---------------------------------------------------------------------------*/
#if 0 /* obsolete since Yorick 1.6 */
local _strlower, _strupper;
/* DOCUMENT local _strlower, _strupper;
Private arrays to convert char to upper/lowercase letters.
SEE ALSO strlower, strupper */
(_strlower = char(indgen(0:255)))(1+'A':1+'Z') = _strlower(1+'a':1+'z');
(_strupper = char(indgen(0:255)))(1+'a':1+'z') = _strupper(1+'A':1+'Z');
local strlower, strtolower; /* needed for documentation */
func __strlower(s) /* interpreted version */
/* DOCUMENT strlower(s)
-or- strtolower(s)
Convert a string or an array of strings S to lower case letters.
SEE ALSO strupper */
{
/* fool codger */ extern _strlower;
n = numberof((r = array(string, dimsof(s))));
for (i=1; i<=n; ++i) r(i)= string(&_strlower(1+*pointer(s(i))));
return r;
}
local strupper, strtoupper; /* needed for documentation */
func __strupper(s) /* interpreted version */
/* DOCUMENT strupper(s)
-or- strtoupper(s)
Convert a string or an array of strings S to upper case letters.
SEE ALSO strlower */
{
/* fool codger */ extern _strupper;
n = numberof((r = array(string, dimsof(s))));
for (i=1; i<=n; ++i) r(i)= string(&_strupper(1+*pointer(s(i))));
return r;
}
/* replace non-builtin functions by interpreted ones */
if (is_func(strupper) != 2) strupper = __strupper;
if (is_func(strlower) != 2) strlower = __strlower;
if (is_func(strtoupper) != 2) strtoupper = strupper;
if (is_func(strtoupper) != 2) strtolower = strtolower;
#endif
/*---------------------------------------------------------------------------*
* Local Variables: *
* mode: Yorick *
* tab-width: 8 *
* fill-column: 75 *
* coding: latin-1 *
* End: *
*---------------------------------------------------------------------------*/
|