What is the flow of execution in the following functions? - scala

I dont undertand after of call y(k), in the function "y" what execute first? the parameters or the body
the function. How the number 5 arrive at the function k
def k(x:Int) = x*x
def y(h:Int => Int) = h(5)
y(k)
OUTPUT:
25

So the beauty of Functional Programming is that we can reason about or programs as expressions.
Given:
def k(x: Int) = x * x // 1.
def y(h: Int => Int) = h(5) // 2.
Then:
y(k) = k(5) // By definition of y (2).
y(k) = 5 * 5 // By definition of k (1).
y(k) = 25 // By definition of multiplication.
Here I made some simplifications. like I didn't do type checking, but that should be pretty straight forward.

Related

Returning an object of the same class from an overloaded method operation "plus" rather than struct?

I have a MATLAB class that has a simple overloaded plus function, and I can't get it to return an object. I want the function to add each field value together and output an object of the same class, with the field values being sums of the two inputs. When I add the two objects together, I get a struct, not an object. I am new to classes and am likely doing this wrong; any help would be great.
The code is as follows:
classdef Molar
properties
A = 0;
B = 0;
C = 0;
end
methods
function M = Molar(val)
M.A = val;
M.B = val+1;
M.C = val+2;
end
function M = plus(M1,M2)
M.A = M1.A + M2.A;
M.B = M1.B + M2.B;
M.C = M1.C + M2.C;
end
end
end
When it runs and I do:
>> x = Molar(2)
x =
Molar with properties:
A: 2
B: 3
C: 4
>> y = Molar(3)
y =
Molar with properties:
A: 3
B: 4
C: 5
Then I get a struct when I do the + operation. How can I get this to return another Molar object?
>> x+y
ans =
struct with fields:
A: 5
B: 7
C: 9
I wonder if it has to do with needing to use the constructor method differently?
Any help in this regard would be appreciated.
The first parameter should be the returned value:
function M = plus(M,M2)
M.A = M.A + M2.A;
M.B = M.B + M2.B;
M.C = M.C + M2.C;
end

efficient, but still precise, multiple of exponent

I need to compute efficiently an array of something like f(i,a) = exp(-0.5 * (i-1) * i * a) for all i in (0..n), with n up to 20.000 and a a positive value very close to 0.
To avoid computing exp n times, I used an incremental approach such as (writing in scala):
def fInc(n: Int, a: Double)
val expA = Math.exp(-a)
var u = 1.0
var v = 1.0
var i = 1
while(i < n){
u *= expA
v *= u // in practice I store that value in an array, for all i
i += 1
}
}
// reference by calling exp directly
def fRef(n: Int, a: Double) = Math.exp(-0.5 * (i-1) * i * a)
This is mathematically correct, but then the difference with direct exp computation is too big. Here are some results:
n a v Math.exp diff
1000 1E-6 0.6068340008761639 0.6068340008714599 4.704014955336788E-12
1000 1E-9 0.9995006247427483 0.9995006247293567 1.3391510123028638E-11
1000 1E-12 0.9999995005111699 0.9999995005001248 1.1045164782785832E-11
1000 1E-15 0.9999999995008992 0.9999999995005 3.992361996552063E-13
10000 1E-6 1.938417748402E-22 1.938417746809E-22 1.5929953847004499E-31
10000 1E-9 0.9512341819777599 0.9512341806597269 1.3180330160622589E-9
10000 1E-12 0.9999500073554776 0.9999500062497292 1.1057483817467073E-9
10000 1E-15 0.9999999500449599 0.9999999500050013 3.995859199079632E-11
As you can see, for some values,the difference goes up to 1e-9, while I can accept maybe 1e-13
So question:
Is there a way to get a better approximate with an algorithm that is still much more efficient than calling exp on all i?
Notes:
I use apache FastMath exp, which gives almost the same results as standard java exp.
The actual algorith is more complex, with other such incremental exp (not quadratic though)
Here is the best solution I found:
The error being incremented (kind of) linearly with each multiplication by the "unitary exp(a)". We can think of the error as a function similar to err(i) ~= i*i*err0 for some err0. The point is that the error of v is quadratic w.r.t i.
The best I found is:
reset the v to the correct value at some chosen frequency (each k iteration)
improve the correctness of u each k iteration, using incremental exp computation
.
val k = 100
val expA = Math.exp(-a)
val expAk = Math.exp(-k*a)
var u = 1.0
var uk = 1.0
var v = 1.0
var i = 1
while(i < n){
if(i%k==0){
uk *= expAk
u = uk
v = Math.exp(- 0.5*(i+1)*i * a)
} else{
u *= expA
v *= u
}
i += 1
}
This method require n / k + 2 call to exp, not quite satifying but the best I have for now. It can probably be improved by choosing the best frequency parameter k.

Determine a function f(n) that describes number of times a statement is executed

How can I determine a function f(n) describing number of times the statement x = x + 2 is executed in terms of n in the following algorithm?
input j
x = 0,
for i: j to j^2 do
x: = x + 1
Note: I'm not looking for run time.
f(n) = n^2-n
essentially just directly calculating x

How to write/code several functions as one

I am trying to write a line composed of two segments as a single equation in :
y = m1*x + c1 , for x<=x1
y = m2*x + c2 , for x>=x1
My questions are:
How can I write the function of this combined line as a single equation?
How can I write multiple functions (valid in separate regions of a linear parameter space) as a single equation?
Please explain both how to express this mathematically and how to program this in general and in Matlab specifically.
You can write this equation as a single line by using the Heaviside step function, https://en.wikipedia.org/wiki/Heaviside_step_function.
Combining two functions into one:
In fact, what you are trying to do is
f(x) = a(x) (for x < x1)
f(x) = q (for x = x1), where q = a(x1) = b(x1)
f(x) = b(x) (for x > x1)
The (half-maximum) Heaviside function is defined as
H(x) = 0 (for x < 0)
H(x) = 0.5 (for x = 0)
H(x) = 1 (for x > 0)
Hence, your function will be
f(x) = H(x1-x) * a(c) + H(x-x1) * b(x)
and, therefore,
f(x) = H(x1-x) * (m1*x+c1) + H(x-x1) * (m2x+c2)
If you want to implement this, note that many programming languages will allow you to write something like
f(x) = (x<x1)?a(x):b(x)
which means if x<x1, then return value a(x), else return b(x), or in your case:
f(x) = (x<x1)?(m1*x+c1):(m2x+c2)
Matlab implementation:
In Matlab, you can write simple functions such as
a = #(x) m1.*x+c1,
b = #(x) m2.*x+c2,
assuming that you have previously defined m1, m2, and c1, c2.
There are several ways to using/implementing the Heaviside function
If you have the Symbolic Math Toolbox for Matlab, you can directly use heaviside() as a function.
#AndrasDeak (see comments below) pointed out that you can write your own half-maximum Heaviside function H in Matlab by entering
iif = #(varargin) varargin{2 * find([varargin{1:2:end}], 1, 'first')}();
H = #(x) iif(x<0,0,x>0,1,true,0.5);
If you want a continuous function that approximates the Heaviside function, you can use a logistic function H defined as
H = #(x) 1./(1+exp(-100.*x));
Independently of your implementation of the Heaviside function H, you can, create a one-liner in the following way (I am using x1=0 for simplicity) :
a = #(x) 2.*x + 3;
b = #(x) -1.5.*x + 3;
Which allows you to write your original function as a one-liner:
f = #(x) H(-x).*a(x) + H(x).*b(x);
You can then plot this function, for example from -10 to 10 by writing plot(-10:10, f(-10:10)) you will get the plot below.
Generalization:
Imagine you have
f(x) = a(x) (for x < x1)
f(x) = q (for x = x1), where q = a(x1) = b(x1)
f(x) = b(x) (for x1 < x < x2)
f(x) = r (for x = x2), where r = b(x2) = c(x2)
f(x) = c(x) (for x2 < x < x3)
f(x) = s (for x = x2), where s = c(x3) = d(x3)
f(x) = d(x) (for x3 < x)
By multiplying Heaviside functions, you can now determine zones where specific functions will be computed.
f(x) = H(x1-x)*a(c) + H(x-x1)*H(x2-x)*b(x) + H(x-x2)*H(x3-x)*c(x) + H(x-x3)*d(x)
PS: just realized that one of the comments above talks about the Heaviside function, too. Kudos to #AndrasDeak .

Not enough input arguments error in matlab

This is my matlab code , I got Not enough input argument error in line 2 and i don't know how to fix it. Anyhelp ? Thanks in advance .
function [] = Integr1( F,a,b )
i = ((b - a)/500);
x = a;k = 0; n = 0;
while x <= b
F1 = F(x);
x = x + i;
F2 = F(x);
m = ((F1+F2)*i)/2;
k = k +m;
end
k
x = a; e = 0; o = 0;
while x <= (b - 2*i)
x = x + i;
e = e + F(x);
x = x + i;
o = o + F(x);
end
n = (i/3)*(F(a) + F(b) + 2*o + 4*e)
This code performs integration by the trapezoidal rule. The last line of code gave it away. Please do not just push the Play button in your MATLAB editor. Don't even think about it, and ignore that it's there. Instead, go into your Command Prompt, and you need to define the inputs that go into this function. These inputs are:
F: A function you want to integrate:
a: The starting x point
b: The ending x point
BTW, your function will not do anything once you run it. You probably want to return the integral result, and so you need to modify the first line of your code to this:
function n = Integr1( F,a,b )
The last line of code assigns n to be the area under the curve, and that's what you want to return.
Now, let's define your parameters. A simple example for F is a linear function... something like:
F = #(x) 2*x + 3;
This defines a function y = 2*x + 3. Next define the starting and ending points:
a = 1; b = 4;
I made them 1 and 4 respectively. Now you can call the code:
out = Integr1(F, a, b);
out should contain the integral of y = 2*x + 3 from x = 1 to x = 4.