I have a function that looks like
function eom!(du, u, p)
#views a, b = u[:,1], u[:,2];
#views da, db = du[:,1], du[:,2];
y = # some stuff involving p and a;
da .= f(a, b, y);
db .= g(b, a);
end
I now want to create a second a function that is the exact same, except the last line reads
db .= g(b, y);
How can I do this most cleanly? Of course, I could just copy and paste and give the functions slightly different names, but this seems unideal, especially if, as is plausible, I later want more functions where the second argument of g could be something else. Is there perhaps a way that I could pass into the function eom! an expression (via the argument p) that would specify the second argument of g? Or is there a way I could make some function eom_generator which can output all of the functions that I want? Perhaps macros are the central tool in doing this, but I am not sure.
You could produce a closure:
function eom_generator(g)
return function eom!(du, u, p)
#views a, b = u[:,1], u[:,2]
#views da, db = du[:,1], du[:,2]
y = nothing # some stuff involving p and a;
da .= f(a, b, y)
db .= g(a, b, y)
end
end
const eom1! = eom_generator((a, b, y) -> g(b, a))
const eom2! = eom_generator((a, b, y) -> g(b, y))
But since this is at the core of a differential equation, be sure to test whether you don't have any performance issues that way.
If you decide that you really need metaprogramming, you can use #eval in a loop:
for (i, expr) in enumerate((:(g(b, a)), :(g(b, y))))
#eval function $(Symbol("eom", i, "!"))(du, u, p)
#views a, b = u[:,1], u[:,2]
#views da, db = du[:,1], du[:,2]
y = nothing # some stuff involving p and a;
da .= f(a, b, y)
db .= $expr
end
end
end
Related
I have a system of n equations and n unknown variables under symbol sum. I want to create a loop to solve this system of equations when inputting n.
y := s -> 1/6cos(3s);
A := (k, s) -> piecewise(k <> 0, 1/2exp(ksI)/abs(k), k = 0, ln(2)exp(s0I) - sin(s));
s := (j, n) -> 2jPi/(2*n + 1);
n := 1;
for j from -n to n do
eqn[j] := sum((A(k, s(j, n))) . (a[k]), k = -n .. n) = y(s(j, n));
end do;
eqs := seq(eqn[i], i = -n .. n);
solve({eqs}, {a[i]});
enter image description here
Please help me out!
I added some missing multiplication symbols to your plaintext code, to reproduce it.
restart;
y:=s->1/6*cos(3*s):
A:=(k,s)->piecewise(k<>0,1/2*exp(k*s*I)/abs(k),
k=0,ln(2)*exp(s*I*0)-sin(s)):
s:=(j,n)->2*j*Pi/(2*n+1):
n:=1:
for j from -n to n do
eqn[j]:=add((A(k,s(j,n)))*a[k],k=-n..n)=y(s(j,n));
end do:
eqs:=seq(eqn[i],i=-n..n);
(-1/4+1/4*I*3^(1/2))*a[-1]+(ln(2)+1/2*3^(1/2))*a[0]+(-1/4-1/4*I*3^(1/2))*a[1] = 1/6,
1/2*a[-1]+ln(2)*a[0]+1/2*a[1] = 1/6,
(-1/4-1/4*I*3^(1/2))*a[-1]+(ln(2)-1/2*3^(1/2))*a[0]+(-1/4+1/4*I*3^(1/2))*a[1] = 1/6
You can pass the set of names (for which to solve) as an optional argument. But that has to contain the actual names, and not just the abstract placeholder a[i] as you tried it.
solve({eqs},{seq(a[i],i=-n..n)});
{a[-1] = 1/6*I/ln(2),
a[0] = 1/6/ln(2),
a[1] = -1/6*I/ln(2)}
You could also omit the indeterminate names here, as optional argument to solve (since you wish to solve for all of them, and no other names are present).
solve({eqs});
{a[-1] = 1/6*I/ln(2),
a[0] = 1/6/ln(2),
a[1] = -1/6*I/ln(2)}
For n:=3 and n:=4 it helps solve to get a result quicker here if exp calls are turned into trig calls. Ie,
solve(evalc({eqs}),{seq(a[i],i=-n..n)});
If n is higher than 4 you might have to wait long for an exact (symbolic) result. But even at n:=10 a floating-point result was fast for me. That is, calling fsolve instead of solve.
fsolve({eqs},{seq(a[i],i=-n..n)});
But even that might be unnecessary, as it seems that the following is a solution for n>=3. Here all the variables are set to zero, except a[-3] and a[3] which are both set to 1/2.
cand:={seq(a[i]=0,i=-n..-4),seq(a[i]=0,i=-2..2),
seq(a[i]=0,i=4..n),seq(a[i]=1/2,i=[-3,3])}:
simplify(eval((rhs-lhs)~({eqs}),cand));
{0}
I'm an economics student slowly switching from MATLAB to Julia.
Currently, my problem is that I don't know how to declare (preallocate) a vector that could store interpolations.
Specifically, when I execute something close to:
function MyFunction(i)
# x, y vectors are some functions of 'i' defined here
f = LinearInterpolation(x,y,extrapolation_bc=Line())
return f
end
g = Vector{Function}(undef, N)
for i = 1:N
g[i] = MyFunction(i)
end
I get:
ERROR: LoadError: MethodError: Cannot `convert` an object of type Interpolations.Extrapolation{Float64,1,Interpolations.GriddedInterpolation{Float64,1,Float64,Gridded{Linear},Tuple{Array{Float64,1}}},Gridded{Linear},Line{Nothing}} to an object of type Function
If I, instead of g=Vector{Function}(undef, N), declare g=zeros(N), I get a similar error message (ending with with ...Float64 rather than with ... Function).
When I, instead, declare:
g = Interpolations.Extrapolation{Float64,1,Interpolations.GriddedInterpolation{Float64,1,Float64,Gridded{Linear},Tuple{Array{Float64,1}}},Gridded{Linear},Line{Nothing}}(N)
I get:
LoadError: MethodError: no method matching Interpolations.Extrapolation{Float64,1,Interpolations.GriddedInterpolation{Float64,1,Float64,Gridded{Linear},Tuple{Array{Float64,1}}},Gridded{Linear},Line{Nothing}}(::Int64) Closest candidates are: Interpolations.Extrapolation{Float64,1,Interpolations.GriddedInterpolation{Float64,1,Float64,Gridded{Linear},Tuple{Array{Float64,1}}},Gridded{Linear},Line{Nothing}}(::Any, !Matched::Any) where {T, N, ITPT, IT, ET}
When I don't declare "g" at all, then I get:
ERROR: LoadError: UndefVarError: g not defined
Finally, when I declare:
g = Vector{Any}(undef, N)
the code works, though I'm afraid this might induce some type-change of a variable g, thereby slowing down my performance-sensitive code.
How, ideally then, should I declare g in this case?
EDIT:
In reality, my problem is a bit more complex, more like the following:
function MyFunction(i)
# x, y vectors are some functions of 'i' defined here
f = LinearInterpolation(x,y,extrapolation_bc=Line())
h = is a T-vector of some functions of x,y
A = is some matrix depending on x,y
return h, A, f
end
h = Matrix{Function}(undef, T, N)
A = zeros(T,I,N)
g = Vector{Any}(undef, N)
for i = 1:N
h[:,i], A[:,:,i], g[i] = MyFunction(i)
end
So, when I use either comprehension or broadcasting (like h, A, g = [MyFunction(i) for i in 1:N] or h, A, g = MyFunction.(1:N)), as users Benoit and DNS suggested below, the outputs of my function are 3 tuples, h, A, g, each containing {h[i], A[i], g[i]} for i=1,2,3. If I use only 1 output variable on the LHS, instead, i.e.: MyOutput = [MyFunction(i) for i in 1:N] or MyOutput[i] = MyFunction.(1:N), then MyOutput becomes a vector with N tuple entries, every tuple consisting of {h[i], A[i], g[i]} i=1,2,3,...,N. I bet there's a way of extracting these elements from the tuples in MyOutput and filling them inside h[:,i], A[:,:,i], g[i], but that seems a bit cumbersome and slow.
You could do
f = MyFunction(1)
g = Vector{typeof(f)}(undef, N)
g[1] = f
for i = 2:N
g[i] = MyFunction(i)
end
I think also map should figure out the type:
map(MyFunction, 1:N)
A simple solution is to use a comprehension:
g = [MyFunction(i) for i in 1:N]
or elegantly use the dot syntax:
g = MyFunction.(1:N)
(Credit to DNF for the dot-syntax solution suggested in the comments.)
In the image above, circuit is Sum of Products
(B’+D’) (A+D) (A+C)
The image below is my attempt on using NAND and NOT gates only. However, my senses is telling me that I am doing it wrongly. Please help!
The first circuit actually implements
which is this circuit:
Using De Morgan's Laws, this is equivalent to
which is this circuit:
This Python program can be used to compare the circuits:
import itertools
# Create all the possible input combinations
x = (True, False)
comb = set(itertools.product(x, x, x, x))
# AD + AC + B'D'
def c1(a, b, c, d):
return ((a and d) or (a and c) or ((not b) and (not d)))
# ((AD)'(AC)'(B'D')')'
def c2(a, b, c, d):
return not ((not (a and d)) and (not (a and c)) and (not ((not b) and (not d))))
# For each input, verify that the results are the same
for x in comb:
r1 = c1(*x)
r2 = c2(*x)
if r1 != r2:
print "Error: Input %s produced %s != %s" % (x, r1, r2)
You can replace all AND gates with NAND gates and just negate the result. I can see that you negated the inputs, which is wrong because:
(ab)' != a'b'
As an example think about signals (a, b) = (1, 0). If you negate them and calculate output, you get 0. If you first calculate and then negate output, you get 1.
About the OR gate:
a + b + c -> ((a + b + c)')' -> (a'b'c')'
So OR gate is NAND gate with all signals negated.
I am new to Octave although I can say I am an expert Matlab user. I am running Octave on a Linux server (Red Hat) remotely through PuTTY, from a windows machine.
I am observing a very strange behavior in Octave. I call myfun(a) which performs as expected giving the sought results. Now, if I run, say, myfun(b) with b!=a, I get again myfun(a). Clear -f does not solve the problem. I need to reboot octave to change the parameters.
What am I doing wrong?
Thanks a lot
Francesco
This is the code for the function I mentioned:
function [a, v, obj, infos, iter] = mle_garch( p )
#{
% this function estimates the GARCH(1,1) parameters
% it is assumed we pass the adjusted price level p
#}
global y = (diff(log(p))-mean(diff(log(p))))*100;
global h = zeros(size(y));
a0 = [var(y)*0.9; 0.8; 0.1];
[a, obj, infos, iter] = sqp(a0, #loglike_garch, [], #loglike_con, [], [], 1000);
v = sqrt(h * 260);
endfunction
function g = loglike_garch( a )
global y h
n = length(y);
h(1) = var(y);
for i = 2 : n,
h(i) = a(1) + a(2) * h(i-1) + a(3) * y(i-1)^2;
endfor
g = 0.5 * ( sum(log(h)) + sum(y.^2./h) ) / n;
endfunction
function f = loglike_con( a )
f = [1;0;0;0] + [0 -1 -1;eye(3)] * a;
endfunction
I'm assuming the myfun you mentioned is mle_garch. The problem is the way you're initializing the global h and v variables (do you really need them to be global?). When you have a piece of code like this
global y = (diff(log(p))-mean(diff(log(p))))*100;
global h = zeros(size(y));
the values of y and h are defined the first time only. You can change their values later on, but this specific lines will never be ran again. Since your code only uses the input argument to define these two variables, the value which you use to run the function the first time will be used every single other time. If you really want to keep those variables global, replace it with the following:
global y;
global h;
y = (diff(log(p))-mean(diff(log(p))))*100;
h = zeros(size(y));
But I don't see any reason to keep them global so just don't make them global.
Also, you mentioned this code worked fine in Matlab. I was under the impression that you couldn't initialize global and persistent variables in Matlab which would make your code illegal in Matlab.
How is the scoping of variables handled during exceptions? I suppose this will be language specific, and answers for any specific language are greatly appreciated. At least maybe the big ones? C++, python, Java. This is what I mean:
python
try:
for k, v in map.iteritems():
cnf.conf.set( section, k, v )
for i, j in map2.iteritems():
dosomethingelse()
for m in range(10):
morestuff()
except SpecificError:
vars = (k, v, i, j, m)
finally:
vars in scope #?
Or something more complicated, like nested blocks:
try:
try:
for k, v in map.iteritems():
cnf.conf.set( section, k, v )
for i, j in map2.iteritems():
dosomethingelse()
for m in range(10):
morestuff()
except SpecificError:
vars = (k, v, i, j, m)
except:
vars in scope #?
In java, I believe you can not do the following:
try {
String s = "Hello, finally!";
...
}
finally {
System.out.println(s);
}
You must instead do:
String s = null;
try {
s = "Hello, finally!";
...
}
finally {
System.out.println(s);
}
In other words, the scope of the variable is limited to the block in which it is defined.
HTH