scide: LookupDialog - redo lookup on classes after partial lookup
[supercollider.git] / HelpSource / Classes / AbstractFunction.schelp
blob45c27748cc3c880141996ebeaa94b05c22d9e525
1 class::AbstractFunction
2 summary::An object which responds to a set of messages that represent mathematical functions
3 categories::Core>Kernel
4 related::Classes/UGen,Classes/Pattern,Classes/Function,Overviews/Operators
6 description::
8 An AbstractFunction is an object which responds to a set of messages that represent
9 mathematical functions. Subclasses override a smaller set of messages to respond
10 to the mathematical functions. The intent is to provide a mechanism for functions
11 that do not calculate values directly but instead compose structures for calculating.
13 Function, Pattern, Stream and UGen are subclasses of AbstractFunction.
14 For example, if you multiply two UGens together the receiver responds by answering a new
15 instance of class BinaryOpUGen which has the two operands as inputs.
17 For an overview of common operators, see link::Overviews/Operators::.
18 To see which classes implements a specific method, see that method in the generated link::Overviews/Methods:: overview.
20 instanceMethods::
22 subsection::Unary Messages
24 All of the following messages send the message composeUnaryOp to the receiver with the
25 unary message selector as an argument.
26 See link::Classes/UnaryOpFunction::.
28 method::neg
29 method::reciprocal
30 method::bitNot
31 method::abs
32 method::asFloat
33 method::asInt
34 method::ceil
35 method::floor
36 method::frac
37 method::sign
38 method::squared
39 method::cubed
40 method::sqrt
41 method::exp
42 method::midicps
43 method::cpsmidi
44 method::midiratio
45 method::ratiomidi
46 method::ampdb
47 method::dbamp
48 method::octcps
49 method::cpsoct
50 method::log
51 method::log2
52 method::log10
53 method::sin
54 method::cos
55 method::tan
56 method::asin
57 method::acos
58 method::atan
59 method::sinh
60 method::cosh
61 method::tanh
62 method::rand
63 method::rand2
64 method::linrand
65 method::bilinrand
66 method::sum3rand
67 method::distort
68 method::softclip
69 method::coin
70 method::even
71 method::odd
72 method::isPositive
73 method::isNegative
74 method::isStrictlyPositive
75 method::rho
76 method::theta
78 subsection::Binary Messages
80 All of the following messages send the message composeBinaryOp to the receiver with the
81 binary message selector and the second operand as arguments.
82 See: link::Classes/BinaryOpFunction::.
84 method::+
85 method::-
86 method::*
87 method::/
88 method::div
89 method::%
90 method::**
91 method::min
92 method::max
93 method::<
94 method::<=
95 method::>
96 method::>=
97 method::&
98 method::|
99 method::lcm
100 method::gcd
101 method::round
102 method::trunc
103 method::atan2
104 method::hypot
105 method::hypotApx
106 method::>>
107 method::+>>
108 method::ring1
109 method::ring2
110 method::ring3
111 method::ring4
112 method::difsqr
113 method::sumsqr
114 method::sqrdif
115 method::absdif
116 method::amclip
117 method::scaleneg
118 method::clip2
119 method::excess
120 method::<!
121 method::rrand
122 method::exprand
123 method::rotate
124 method::dist
125 method::bitAnd
126 method::bitOr
127 method::bitXor
128 method::bitHammingDistance
129 method::@
131 subsection:: Messages with more arguments
133 All of the following messages send the message code::composeNAryOp:: to the receiver with the
134 binary message selector and the other operands as arguments.
135 See link::Classes/NAryOpFunction::.
137 method::clip
138 method::wrap
139 method::fold
140 method::blend
141 method::linlin
142 method::linexp
143 method::explin
144 method::expexp
146 subsection:: other
148 method::applyTo
150 Interface that allows us to combine selectors (Symbols) and Functions. Sends valueArray(args) to this.
151 discussion::
152 code::
153 // example:
155 f = [{ |a, b| a * b * 100.rand }, { |a, b| sin(a) * sin(b) }, '*', '/'];
156 f.choose.postcs.applyTo(3, 4);
158 // this is used in SequenceableCollection reduce:
159 (1..10).reduce('+');
160 (1..10).reduce({ |a, b| a * b * 1.0.rand });
163 method::asUGenInput
165 returns:: the result of sending the value(for) message to this.
166 discussion::
167 code::
168 // example:
170 var f, g, product;
171 f = { SinOsc.ar(400) };
172 g = { LFPulse.kr(8)  };
173 product = f * g * 0.1;
174 { Pan2.ar(product, SinOsc.kr(0.3)) }.play;
178 method::sampled
179 Sample a function.
180 discussion::
181 code::
182 //sample a function
183 f = { |x| sin(3*x)*cos(8*x) }
184 f.plotGraph2(from:0,to:2);
185 f.sampled(10,0,2).plotGraph2(from:0,to:2);
186 f.sampled(80,0,2).plotGraph2(from:0,to:2);
188 //on complicated functions a sampled function is less cpy heavy.
189 f = { |x| 60.collect{ 2**((x-rrand(0.0,1.0))) }.sum/60 };
190 f.plotGraph2(from:0,to:1);
191 g = f.sampled(200);
192 g.plotGraph2(from:0,to:1);
193 { 200.collect{ f.(rand(0.0,1.0)) } }.bench;
194 { 200.collect{ g.(rand(0.0,1.0)) } }.bench;
197 subsection::Function Composition
199 When unary, binary or n-ary operators are applied to an abstract function, it returns an object that represents
200 this operation, without evaluating the function: link::Classes/UnaryOpFunction::, link::Classes/BinaryOpFunction::, link::Classes/NAryOpFunction::.
201 Note that different subclasses like link::Classes/Pattern:: or link::Classes/UGen:: have their own composition scheme analogous to the one of AbstractFunction itself. For more about functions, see link::Classes/Function::.
203 examples::
205 code::
206 // examples
208 a = { 1.0.rand } + 8;
209 a.value;
212 y = { 8 } + { 1.0.rand };
213 y.value;
216 code::
217 // arguments are passed into both functions
219 y = { |x=0| x } + { 1.0.rand };
220 y.value(10);
223 y = { |x=0| x * 3 } + { |x=0| x + 1.0.rand };
224 y.value(10);
226 y.postcs;
228 y = { |x=0| x * 3 } + { |x=0| x + 1.0.rand } * { |x=0| [50, 100].choose + x } + 1.0;
229 y.value(10);
232 code::
233 // environments can be used as a lookup with valueEnvir:
236 Environment.use {
237         ~y = 10;
238         ~x = 2;
239         ~z = { |x=8| x } + { |y=0| y + 1.0.rand };
240         ~z.valueEnvir;
245 code::
246 // n-ary operators:
248 a = blend({ 3.0.rand }, { 1000.rand }, { |frac| frac });
249 a.value(0.5);
251 a.value((0, 0.06..1)); // creates a range of values..