I have a symbolic expressions as below
y1 = (1/a)-(b/a^2)+x*a*b-x/b
y2 = a*b+a*x+b*sqrt(x)
now I need to get the partial expressions which have specific term. Like
xFunction(y1, x) # should return x*a*b-x/b
xFunction(y2,x) # should return a*x+b*sqrt(x)
any suggestions or idea are very healpful
Thank you
restart;
y1 := (1/a)-(b/a^2)+x*a*b-x/b:
y2 := a*b+a*x+b*sqrt(x):
K := (ee,x) -> `if`(ee::`+`,select(depends,ee,x),ee):
K( y1, x );
x
x a b - -
b
K( y2, x );
(1/2)
a x + b x
#
# Leave alone an expression which is not a sum of terms.
#
K( sin(x+4)*x^3, x );
3
sin(x + 4) x
#
# Don't select subterms in which `x` is a just dummy name.
#
K( x^3 + sin(x) + Int(sqrt(x), x=a..b), x );
3
x + sin(x)
[edited]
y1 := (1/a)-(b/a^2)+x*a*b-x/b;
1 b x
y1 := - - -- + x a b - -
a 2 b
a
op(3,y1);
x a b
depends(op(3,y1), x);
true
The select command maps its first argument over
all the operands of its second argument.
select( s->depends(s,x), y1 );
x
x a b - -
b
A more terse syntax, where select maps its first
argument depends over the operands of its second
argument, and passes its third argument as additional
options (to the selector).
select( depends, y1, x );
x
x a b - -
b
Now create a procedure to do it. Use a conditional
test, so that it returns the first argument itself
whenever that is not a sum of terms.
K1 := proc(ee, x)
if type(ee,`+`) then
select( depends, ee, x );
else
# leave it alone
ee;
end if;
end proc:
K1( y1, x);
x
x a b - -
b
Using a more terse syntax for that type-check.
K2 := proc(ee, x)
if ee::`+` then
select( depends, ee, x );
else
# leave it alone
ee;
end if;
end proc:
K2( y1, x);
x
x a b - -
b
Using a more terse syntax for that if..then..end if.
This is the so-called operator form of if. The word
if is within name-quotes, to distinguish it from the
language keyword within an if...then...end if .
K3 := proc(ee, x)
`if`( ee::`+` , select( depends, ee, x ), x );
end proc:
K3( y1, x);
x
x a b - -
b
Since the body of the procedure K3 has only a single statement then
we can make it more terse, using the so-called operator
form.
K4 := (ee, x) -> `if`( ee::`+` , select( depends, ee, x ), x ):
K4( y1, x);
x
x a b - -
b
listOfTerms = op(expression); # y1 or y2
numberOfSubExpressions=nops(expression); # for y1 or y2
requiredTerm = 0;
for i 1 to numberOfSubExpressions do
if has(listOfTerms [i], x) then # x is our required term
requiredTerm := requiredTerm +listOfTerms [i]
end if
end do
Above code does my requirement. But, if are there any bugs for special expressions please let me know. Because op function behaves differently when we have functions like(sin,cos Log ..etc)
Related
There must be an easier way to do this, optimization method is also welcome. I have an array 'Y' and many parameters that has to be adjusted such that Y nears zero (= 'X') as given in the MWE. Is there a much better procedure to minimize this difference? This is just an example equation, there can be 6 coefficients to optimized.
x = zeros(10,1)
y = rand(10,1)
for a=1:0.1:4
for b=2:0.1:5
for c = 3:0.1:6
z = (a * y .^ 3 + b * y + c) - x
if -1<= range(z) <= 1
a, b, c
break
end
end
end
end
I believe
p = polyfit(y,x,2);
is what you are looking for.
where p will be an array of your [a, b, c] coefficients.
This question already has answers here:
Numerical derivative of a vector
(3 answers)
Closed 7 years ago.
I have two arrays: x and y. In practice, y is dependent on x, but both arrays are measured values. I would like to obtain the derivative of y with respect to x. If x were uniformly spaced (i.e. x=[1 2 3 4 5]), I could do something with diff like this:
h = 0.001;
x = -pi:h:pi;
f = sin(X);
y = diff(f)/h;
However, x is not uniformly spaced (i.e. x=[1 1.9 2.8 4.1]). How can I obtain the partial derivative of this data set?
A good way to do it is gradient,
dydx = gradient(y, x);
I like it because it returns a vector which is the same length as x and y. The downside though, is it's first order accurate. This can sometimes be a problem, a fix could be to write your own,
x = unique([linspace(0, 2*pi, 50), logspace(0, log10(2*pi), 50)]);
y = cos(x) ;
subplot(2,1,1) ;
plot(x, Gradient(y, x), x, gradient(y,x), x, -sin(x));
legend('2^{nd} order', '1^{st} order', 'exact') ;
subplot(2,1,2) ;
plot(x, Gradient(y, x) + sin(x), x, gradient(y,x) + sin(x));
legend('2^{nd} order - exact', '1^{st} order - exact')
With Gradient being
function dydx = Gradient(y,x)
y = y(:);
p = x(3:end) - x(2:end-1);
p = p(:);
m = x(2:end-1) - x(1:end-2);
m = m(:);
p1 = x(2) - x(1);
p2 = x(3) - x(1);
m1 = x(end) - x(end-1);
m2 = x(end) - x(end-2);
dydx = reshape([ ((-p1^2 + p2^2)*y(1) - p2^2*y(2) + p1^2*y(3))/(p1*(p1 - p2)*p2);
((-m.^2 + p.^2).*y(2:end-1) - p.^2.*y(1:end-2) + m.^2.*y(3:end))./(m.*p.*(m + p));
((m1^2 - m2^2)*y(end) + m2^2*y(end-1) - m1^2*y(end-2))/(m1^2*m2 - m1*m2^2) ...
], size(x));
end
Edit:
Improved it for multidimensional array and constant spacing support
function dydx = Gradient(y,x)
if length(y) < 3
dydx = gradient(y,x);
return
end
[~, n] = max(size(y));
N = ndims(y);
i = repmat({':'},1,N-1);
y = permute(y, [n, 1:n-1, n+1:N]);
if isscalar(x)
%"x" is actually a spacing value
p = x;
m = x;
p1 = x;
p2 = x;
m1 = x;
m2 = x;
else
if isvector(x)
x = repmat(x(:), size(y(1, i{:})));
else
x = permute(x, [n, 1:n-1, n+1:N]);
end
if all(size(x) ~= size(y))
error('Sizes of arrays must be the same.')
end
p = x(3:end, i{:}) - x(2:end-1, i{:});
m = x(2:end-1, i{:}) - x(1:end-2, i{:});
p1 = x(2, i{:}) - x(1, i{:});
p2 = x(3, i{:}) - x(1, i{:});
m1 = x(end, i{:}) - x(end-1, i{:});
m2 = x(end, i{:}) - x(end-2, i{:});
end
dydx = ipermute([ ((-p1.^2 + p2.^2).*y(1,i{:}) - p2.^2.*y(2,i{:}) + p1.^2.*y(3,i{:}))./(p1.*(p1 - p2).*p2);
((-m.^2 + p.^2).*y(2:end-1,i{:}) - p.^2.*y(1:end-2,i{:}) + m.^2.*y(3:end,i{:}))./(m.*p.*(m + p));
((m1.^2 - m2.^2).*y(end,i{:}) + m2.^2.*y(end-1,i{:}) - m1.^2.*y(end-2,i{:}))./(m1.^2.*m2 - m1.*m2.^2) ...
], [n, 1:n-1, n+1:N]);
end
I've been trying to extend the xor-swap to more than two variables, say n variables. But I've gotten nowhere that's better than 3*(n-1).
For two integer variables x1 and x2 you can swap them like this:
swap(x1,x2) {
x1 = x1 ^ x2;
x2 = x1 ^ x2;
x1 = x1 ^ x2;
}
So, assume you have x1 ... xn with values v1 ... vn. Clearly you can "rotate" the values by successively applying swap:
swap(x1,x2);
swap(x2,x3);
swap(x3,x4);
...
swap(xm,xn); // with m = n-1
You will end up with x1 = v2, x2 = v3, ..., xn = v1.
Which costs n-1 swaps, each costing 3 xors, leaving us with (n-1)*3 xors.
Is a faster algorithm using xor and assignment only and no additional variables known?
As a partial result I tried a brute force search for N=3,4,5 and all of these agree with your formula.
Python code:
from collections import *
D=defaultdict(int) # Map from tuple of bitmasks to number of steps to get there
N=5
Q=deque()
Q.append( (tuple(1<<n for n in range(N)), 0) )
goal = (tuple(1<<( (n+1)%N ) for n in range(N)))
while Q:
masks,ops = Q.popleft()
if len(D)%10000==0:
print len(D),len(Q),ops
ops += 1
# Choose two to swap
for a in range(N):
for b in range(N):
if a==b:
continue
masks2 = list(masks)
masks2[a] = masks2[a]^masks2[b]
masks2 = tuple(masks2)
if masks2 in D:
continue
D[masks2] = ops
if masks2==goal:
print 'found goal in ',ops
raise ValueError
Q.append( (masks2,ops) )
How do I translate this portion of C code into Haskell? From what I know, I must use the State monad, but I don't know how.
int x = 1;
int y = 2;
x = x * y;
y = y + x;
Let's assume, you have that pair of integers as state:
f = do put (1,2)
modify (\(x,y) -> (x*y,y))
modify (\(x,y) -> (x,y+x))
Is that, what you want?
A literal translation would use IORefs:
import Data.IORef
main :: IO ()
main = do x <- newIORef 1
y <- newIORef 2
y_val <- readIORef y
modifyIORef x (\v -> v * y_val)
x_val <- readIORef x
modifyIORef y (\v -> v + x_val)
As you can see, imperative programming is ugly in Haskell. This is intentional, to coax you into using functional style. You can define some helper functions, though, to make this more bearable:
import Data.IORef
-- x := f x y
combineToR :: (a -> t -> a) -> IORef a -> IORef t -> IO ()
combineToR f x y = do y_val <- readIORef y
modifyIORef x (\v -> f v y_val)
addTo :: Num a => IORef a -> IORef a -> IO ()
addTo = combineToR (+)
multWith :: Num a => IORef a -> IORef a -> IO ()
multWith = combineToR (*)
main :: IO ()
main = do x <- newIORef 1
y <- newIORef 2
multWith x y
addTo y x
The point of functional languages for you to not do that, make a new value, or use recursion.
If you like to just print these values,
x = 1
y = 2
a = x*y
b = y+x
main = do
putStrLn ("x*y: " ++ a)
putStrLn ("y+x: " ++ b)
If this is a homework assignment, please mark it as so, and I will change my answer.
another way is to think about "versions" of the variable - the x at the start is different than the x at the end. For example, in C say you have a variable that sometimes stores a number in fahrenheit and then you convert it to centigrade, like this:
temp = 40;
temp = convertFtoC(temp);
then you could think about these as two different variables:
tempF = 40;
tempC= convertFtoC(tempF);
Without knowing what your x and y are to invent better names for them, you might end up writing in haskell:
xa = 1;
ya = 2;
xb = xa * ya;
yb = ya + xb;
In some cases, that can be a nice way to think about how to make your code more functional and less imperative.
If you identify your "mutable" variables with a tuple, you can define transformation operations on it and "chain" it together:
vars x y = (x,y)
setX (x,y) x' = (x', y)
setY (x,y) y' = (x, y')
appX (x,y) f = (f x, y)
appY (x,y) f = (x, f y)
app2X (x, y) f = (f x y, y)
app2Y (x, y) f = (x, f x y)
set... sets a value, app... applies a function on it, app2... applies a function on both values and stores it in x or y. Then you can do something like:
(vars 3 5) `setX` 14 `appY` (2*)
-- result: (14,10)
Your example would become:
(vars 1 2) `app2X` (*) `app2Y` (+)
-- result: (2,4)
Of course this stretches the definition of "mutable" a bit, but this solution is already half way to the State or Writer monad.
I am to implement the following formula in Delphi:
Understanding the formula:
Y_k is a floating point number which we'll call just Y.
w[i][j] is an array containing floating point numbers as well (1<=i<=43 and 1<=j<=30).
According to my source paper (p. 12) nabla(w)*Y_k is the partial derivative of (column) vector w with respect to the value Y". Is this correct?
Coding in Delphi (implementation):
So how do I implement this in Delphi?
Thanks a lot in advance!
I will assume that the problem is: "How can I compute a gradient of a scalar function in Delphi?" [I still think that the symbol given above looks more like a connection/covariant derivative as known from differential geometry/tensor calculus!]
You need to specify the input you got. The simplest case is when you got the function f whose gradient you wish to compute. Per definition, if f is a function of the k variables x1, x2, ..., xk, that is, if you'd write
f(x1, x2, ..., xk)
which is a scalar field in ℝk then the gradient is
∇f = (∂f/∂x1, ∂f/∂x2, ..., ∂f/∂xk)
that is, a vector field in ℝk (at each point in ℝk you get a k-dimensional vector).
This is rather easily implemented in Delphi. The following is an example for the case where k = 3.
type
TVector = record
x, y, z: real;
constructor Create(ax, ay, az: real);
end;
TRealValuedFunction = function(v: TVector): real;
function gradient(f: TRealValuedFunction; v: TVector): TVector;
const
h = 0.001;
begin
result.x := (f(TVector.Create(v.x + h, v.y, v.z)) - f(TVector.Create(v.x - h, v.y, v.z))) / (2*h);
result.y := (f(TVector.Create(v.x, v.y + h, v.z)) - f(TVector.Create(v.x, v.y - h, v.z))) / (2*h);
result.z := (f(TVector.Create(v.x, v.y, v.z + h)) - f(TVector.Create(v.x, v.y, v.z - h))) / (2*h);
end;
where, of course,
constructor TVector.Create(ax, ay, az: real);
begin
x := ax;
y := ay;
z := az;
end;
Sample usage:
function SampleFunction(v: TVector): real;
begin
result := 5*v.x + 7*v.y;
end;
procedure TForm4.FormCreate(Sender: TObject);
begin
with gradient(SampleFunction, TVector.Create(2, 6, 3)) do
ShowMessage(FloatToStr(x) + ', ' + FloatToStr(y) + ', ' + FloatToStr(z))
end;
The result is 5.00000000000256, 7.000000000005, 0 which is a very good approximation to the gradient of SampleFunction at v (in this case the gradient is constant in space, that is, it doesn't depend on which point v in space you select).
Of course, if you are writing anything serious, you will probably use your own vector algebra library.
Also, h = 0.001 might not be a great value in your case.