rslt ← {guess←1+⎕ct} (fn ##.invr) argt ⍝ Inverse of real-valued function. Suggested by Mike Day, operator [invr] uses the Newton-Raphson method to find a [rslt], such that: argt ≡ fn rslt where operand [fn] is a monadic continuous real-valued function and [argt] is a numeric array. The left argument (default 1+⎕ct), is a starting point for the iteration. Some functions, such as sin←1∘○, are "many-to-one", which means that the inverse could be any of a number of values. In this case the left argument should be chosen to be close to the desired one. See the "ArcSin" example below. The choice of ⎕ct as a default starting point is arbitrary. 0, 1 or ⍵ would be just as good. 1+⎕ct has the small advantage that it tends to avoid the discont- inuity at 0, of primitive functions ⍟ and ÷. Bugs: With an unfortunate choice of starting value, [invr] may generate a "no inverse" error or may even fail to terminate. Examples: ○invr ○ 2 3⍴⍳6 ⍝ inverse of primitive function pi-times. 1 2 3 4 5 6 {⍵+⍵}invr 2 4 6 ⍝ inverse of doubling function. 1 2 3 {2*⍵}invr 16 ⍝ inverse of 2-exp is 2-log 4 {⍵*2}invr 49 ⍝ inverse of square is sqrt. 7 c2f←{32+⍵×1.8} ⍝ define Celsius to Fahrenheit f2c←c2f invr ⍝ derive Fahrenheit to Celsius c2f (0 100) (¯40 ¯273.15) ⍝ convert nested array of temperatures. ┌──────┬───────────┐ │32 212│¯40 ¯459.67│ └──────┴───────────┘ f2c (32 212) (¯40 ¯459.67) ⍝ convert using inverse function. ┌─────┬───────────┐ │0 100│¯40 ¯273.15│ └─────┴───────────┘ sin←1∘○ ⍝ Sin(⍵) sin 10 ¯0.54402 arcs←sin invr ⍝ ArcSin(⍵) arcs sin 10 ⍝ ArcSin(Sin(10)). 68.54 9 arcs sin 10 ⍝ ArcSin(Sin(10)) near 9. 10 See also: limit pow Back to: contents Back to: Workspaces