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
|
package
global(input,int2str,compile,mean,stddev,Tr,FFT2,invFFT2,fib,docview,kron,fix,isfile,nrange,strstr)
{
/*
* This file is part of tela the Tensor Language.
* Copyright (c) 1994-1996 Pekka Janhunen
*/
function y = input(;s)
// y = input() returns a value entered by user.
// y = input("prompt") writes a prompt before asking.
local(str)
{
if (isstr(s)) format(s);
str = input_string();
if (streq(str,"")) y = : else y = evalexpr(str)
};
function s = int2str(x)
// int2str(x) converts an integer x to a string.
{
s = sformat("``",x)
};
function [] = compile(...)
// compile("file1.t",...) compiles given t-file(s) to object files.
{
for (i=1; i<=Nargin(); i++) {
tfile = argin(i);
L = length(tfile);
if (tfile[L]=='t' && tfile[L-1]=='.')
ctfile = #(tfile[1:L-1],"ct")
else
ctfile = #(tfile,".ct");
t2ct(tfile);
system(#("telakka -c -O1 ",ctfile));
}
};
function y = mean(x; d)
// mean(x) computes the arithmetic mean of a numeric array x.
// mean(x,d) maps the operation only along d'th dimension.
{
if (isdefined(d)) {
s = size(x);
y = sum(x,d)/s[d];
} else
y = sum(x)/length(x)
};
function y = stddev(x;d)
// stddev(x) computes the standard deviation of a numeric array x.
// It divides by N, not by N-1 as some other implementations.
// stddev(x,d) maps the operation only along d'th dimension.
// See also: mean.
{
if (isdefined(d))
y = sqrt( mean(x^2,d) - mean(x,d)^2 )
else
y = sqrt(mean( (x-mean(x))^2 ))
};
function y = Tr(x)
// Tr(x) returns the trace (diagonal sum) of a matrix.
{
y = sum(diag(x))
};
function f = FFT2(u)
// FFT2(u) gives a 2D discrete Fourier transform of 2D array u.
// See also: invFFT2, FFT.
{
f=FFT(FFT(u,1),2)
};
function f = invFFT2(u)
// invFFT2(u) gives the inverse 2D Fourier transform of u.
// See also: FFT2.
{
f=invFFT(invFFT(u,1),2)
};
function f = fib(n)
// fib(n) returns the nth Fibonacci number.
// Recursive (==> very slow) implementation, useful for
// benchmarking only!
{
if (n<2) f=n else f=fib(n-1)+fib(n-2)
};
function docview()
// docview() is an interactive program for viewing various
// kinds of Tela documentation.
local(directory,choicViewing,choiceDoc,editor,env)
{
directory = "/usr/lib/tela";
editor = "emacs";
pager = "less -Ms";
env = getenv("EDITOR");
if (isstr(env)) editor = env;
env = getenv("PAGER");
if (isstr(env)) pager = env;
format("Viewing Tela documentation:\n");
choiceViewing = smenu("Choose viewing method",
"ASCII",
"HTML/Xmosaic (requires X)",
"HTML/Lynx",
"Xdvi (requires X)",
"Xemacs (requires X)",
"Cancel");
if (strstarteq(choiceViewing,"Cancel")) return;
choiceDoc = smenu("Choose document to view",
"Basic help file",
"Builtin functions alphabetically sorted",
"Builtin functions sorted by sections",
"Cancel");
if (strstarteq(choiceDoc,"Cancel")) return;
if (strstarteq(choiceDoc,"Basic help"))
file = "telahelp"
else if (strstarteq(choiceDoc,"Builtin functions alpha"))
file = "telafuncs"
else if (strstarteq(choiceDoc,"Builtin functions sorted"))
file = "telafuncsSectioned";
if (strstarteq(choiceViewing,"ASCII"))
system(#(pager," ",directory,"/",file,".txt"))
else if (strstarteq(choiceViewing,"HTML/X"))
system(#("cd ",directory,"/html; xmosaic ",file,".html&"))
else if (strstarteq(choiceViewing,"HTML/Lynx"))
system(#("cd ",directory,"/html; lynx ",file,".html"))
else if (strstarteq(choiceViewing,"Xdvi"))
system(#("xdvi -geometry 847x570+0+0 -s 3 ",directory,"/",file,".dvi&"))
else if (strstarteq(choiceViewing,"Xemacs"))
system(#(editor," ",directory,"/",file,".txt&"));
};
function K = kron(A,B)
// kron(X,Y) is the Kronecker tensor product of X and Y.
// The result is a large matrix formed by taking all possible
// products between the elements of X and those of Y.
// For example, if X is 2 by 3, then KRON(X,Y) is
//
// #( X[1,1]*Y, X[1,2]*Y, X[1,3]*Y;
// X[2,1]*Y, X[2,2]*Y, X[2,3]*Y )
local(ma,na, mb,nb,i,j,jk,ik)
{
if (rank(A)!=2 || rank(B)!=2) {K=0; return};
[ma,na] = size(A);
[mb,nb] = size(B);
La = length(A);
Lb = length(B);
m = ma*mb;
n = na*nb;
if (isint(A) && isint(B))
K = izeros(m,n)
else if (iscomplex(A) || iscomplex(B))
K = czeros(m,n)
else
K = zeros(m,n);
if (La <= Lb) {
for (i=1; i<=ma; i++) {
ik = 1+(i-1)*mb:i*mb;
for (j=1; j<=na; j++) {
jk = 1+(j-1)*nb:j*nb;
K[ik,jk] = A[i,j]*B
}
}
} else {
for (i=1; i<=mb; i++) {
ik=i:mb:(ma-1)*mb+i;
for (j=1; j<=nb; j++) {
jk = j:nb:(na-1)*nb+j;
K[ik,jk] = A*B[i,j];
}
}
}
};
/*function y = where(c,a,b)
// where(c,a,b) returns a where c is nonzero and b where c is zero.
// Usually the arguments are arrays, and if they are, they must be
// of similar dimensions.
{
if (isarray(c)) {
y = c + 0*a + 0*b;
yes = find(c);
no = find(!c);
if (isarray(a))
y[yes] = a[yes]
else
y[yes] = a;
if (isarray(b))
y[no] = b[no]
else
y[no] = b;
} else {
if (c) y=a else y=b
}
};*/
function y = fix(x)
// fix(x) truncates x toward zero.
{
y = sign(x)*floor(abs(x))
};
function y = isfile(f)
// isfile("file") tests whether "file" exists in the current directory.
{
y = length(run(sformat("if test -f ``; then echo 1; fi",f)))
};
//function y = limit(x,a,b)
// THIS IS NOW BUILTIN FUNCTION
// limit(x,a,b) limits x to the range [a,b].
//{
// y = min(b,max(x,a))
//};
function y = nrange(a,b,n)
/* y=nrange(a,b,n) returns a range of values from a to b of length n.
The first y value is equal to a and the last y is equal to b.
If you want to ensure that the generated range has exactly n components,
it is safer to use nrange than the builtin a:s:b construct. */
{
y = a + (0:n-1)*(b-a)/(n-1)
};
function y = strstr(haystack,needle)
/* strstr(haystack,needle) find the first occurrence of string needle
in the string haystack. It returns the index of the first character
in haystack, or 0 if not found.*/
{
if (!isstring(haystack) || !isstring(needle)) {y=0; return};
Lhaystack = length(haystack);
Lneedle = length(needle);
if (Lneedle == 0) {y=0; return};
L = Lhaystack - Lneedle + 1;
for (i=1; i<=L; i++) {
if (streq(needle,haystack[i:i+Lneedle-1])) {
y = i;
return;
}
};
y = 0;
};
};
|