File: functional.chapt.txt

package info (click to toggle)
yacas 1.3.6-2
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 7,176 kB
  • ctags: 3,520
  • sloc: cpp: 13,960; java: 12,602; sh: 11,401; makefile: 552; perl: 517; ansic: 381
file content (283 lines) | stat: -rw-r--r-- 5,986 bytes parent folder | download | duplicates (6)
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
276
277
278
279
280
281
282
283

			Functional operators

*INTRO
These operators can help the user to program in the style of
functional programming languages such as Miranda or Haskell.

*CMD : --- prepend item to list, or concatenate strings
*STD
*CALL
	item : list
	string1 : string2
Precedence:
*EVAL OpPrecedence(":")

*PARMS
{item} -- an item to be prepended to a list

{list} -- a list

{string1} -- a string

{string2} -- a string

*DESC

The first form prepends "item" as the first entry to the list
"list". The second form concatenates the strings "string1" and
"string2".

*E.G.

	In> a:b:c:{}
	Out> {a,b,c};
	In> "This":"Is":"A":"String"
	Out> "ThisIsAString";

*SEE Concat, ConcatStrings

*CMD @ --- apply a function
*STD
*CALL
	fn @ arglist
Precedence:
*EVAL OpPrecedence("@")

*PARMS

{fn} -- function to apply

{arglist} -- single argument, or a list of arguments

*DESC

This function is a shorthand for {Apply}. It applies the
function "fn" to the argument(s) in "arglist" and returns the
result. The first parameter "fn" can either be a string containing
the name of a function or a pure function.

*E.G.

	In> "Sin" @ a
	Out> Sin(a);
	In> {{a},Sin(a)} @ a
	Out> Sin(a);
	In> "f" @ {a,b}
	Out> f(a,b);

*SEE Apply

*CMD /@ --- apply a function to all entries in a list
*STD
*CALL
	fn /@ list
Precedence:
*EVAL OpPrecedence("/@")

*PARMS

{fn} -- function to apply

{list} -- list of arguments

*DESC
This function is a shorthand for {MapSingle}. It
successively applies the function "fn" to all the entries in
"list" and returns a list contains the results. The parameter "fn"
can either be a string containing the name of a function or a pure
function.

*E.G.

	In> "Sin" /@ {a,b}
	Out> {Sin(a),Sin(b)};
	In> {{a},Sin(a)*a} /@ {a,b}
	Out> {Sin(a)*a,Sin(b)*b};

*SEE MapSingle, Map, MapArgs

*CMD .. --- construct a list of consecutive integers

*STD

*CALL
	n .. m
Precedence:
*EVAL OpPrecedence("..")

*PARMS

{n} -- integer. the first entry in the list

{m} -- integer, the last entry in the list

*DESC

This command returns the list {{n, n+1, n+2, ..., m}}. If {m} is
smaller than {n}, the empty list is returned. Note that the
{..} operator should be surrounded by spaces to keep the
parser happy, if "n" is a number. So one should write "{1 .. 4}" instead of "{1..4}".

*EG

	In> 1 .. 4
	Out> {1,2,3,4};

*SEE Table

*CMD NFunction --- make wrapper for numeric functions
*STD
*CALL
	NFunction("newname","funcname", {arglist})

*PARMS
{"newname"} -- name of new function

{"funcname"} -- name of an existing function

{arglist} -- symbolic list of arguments

*DESC
This function will define a function named "newname"
with the same arguments as an existing function named "funcname". The new function will evaluate and return the expression "funcname(arglist)" only when
all items in the argument list {arglist} are numbers, and return unevaluated otherwise.

This can be useful when plotting functions defined through other Yacas routines that cannot return unevaluated.

If the numerical calculation does not return a number (for example,
it might return the atom {nan}, "not a number", for some arguments),
then the new function will return {Undefined}.


*E.G. notest
	In> f(x) := N(Sin(x));
	Out> True;
	In> NFunction("f1", "f", {x});
	Out> True;
	In> f1(a);
	Out> f1(a);
	In> f1(0);
	Out> 0;
Suppose we need to define a complicated function {t(x)} which cannot be evaluated unless {x} is a number:

	In> t(x) := If(x<=0.5, 2*x, 2*(1-x));
	Out> True;
	In> t(0.2);
	Out> 0.4;
	In> t(x);
	In function "If" :
	bad argument number 1 (counting from 1)
	CommandLine(1) : Invalid argument
Then, we can use {NFunction()} to define a wrapper {t1(x)} around {t(x)} which will not try to evaluate {t(x)} unless {x} is a number.

	In> NFunction("t1", "t", {x})
	Out> True;
	In> t1(x);
	Out> t1(x);
	In> t1(0.2);
	Out> 0.4;
Now we can plot the function.

	In> Plot2D(t1(x), -0.1: 1.1)
	Out> True;

*SEE MacroRule



*CMD Where --- substitute result into expression
*STD
*CALL
	expr Where x==v
	expr Where x1==v1 And x2==v2 And ...
	expr Where {x1==v1 And x2==v2,x1==v3
	  And x2==v4,...}

*PARMS

{expr} - expression to evaluate

{x} - variable to set

{v} - value to substitute for variable

*DESC

The operator {Where} fills in values for variables, in its simplest form.
It accepts sets of variable/value pairs defined as 

	var1==val1 And var2==val2 And ...

and fills in the corresponding values. Lists of value pairs are
also possible, as:

	{var1==val1 And var2==val2, var1==val3
	  And var2==val4}

These values might be obtained through {Solve}.

*E.G.

	In> x^2+y^2 Where x==2
	Out> y^2+4;
	In> x^2+y^2 Where x==2 And y==3
	Out> 13;
	In> x^2+y^2 Where {x==2 And y==3}
	Out> {13};
	In> x^2+y^2 Where {x==2 And y==3,x==4 And y==5}
	Out> {13,41};

*SEE Solve, AddTo


*CMD AddTo --- add an equation to a set of equations or set of set of equations
*STD
*CALL
	eq1 AddTo eq2

*PARMS

{eq} - (set of) set of equations

*DESC

Given two (sets of) sets of equations, the command AddTo combines
multiple sets of equations into one. 

A list {a,b} means that a is a solution, OR b is a solution.
AddTo then acts as a AND operation:

	(a or b) and (c or d) => 
	(a or b) Addto (c or d) => 
	(a and c) or (a and d) or (b and c)
	  or (b and d)

This function is useful for adding an identity to an already
existing set of equations. Suppose a solve command returned
{a>=0 And x==a,a<0 And x== -a} from an expression x==Abs(a),
then a new identity a==2 could be added as follows:

	In> a==2 AddTo {a>=0 And x==a,a<0 And x== -a}
	Out> {a==2 And a>=0 And x==a,a==2 And a<0
	  And x== -a};

Passing this set of set of identities back to solve, solve
should recognize that the second one is not a possibility
any more, since a==2 And a<0 can never be true at the same time.

*E.G.

	In> {A==2,c==d} AddTo {b==3 And d==2}
	Out> {A==2 And b==3 And d==2,c==d
	  And b==3 And d==2};
	In> {A==2,c==d} AddTo {b==3, d==2}
	Out> {A==2 And b==3,A==2 And d==2,c==d
	  And b==3,c==d And d==2};

*SEE Where, Solve