How to tell maple that derivative of zero is zero when substituting? - substitution

I'm trying to use subs in maple to replace derivatives in a longer formula with 0:
subs(diff(u(r),r) = 0, formula);
It seems that if formula only involves first derivatives of u(r) this works as I expect. For example,
formula := diff(u(r),r);
subs(diff(u(r),r) = 0, formula);
0
But if formula involves second derivatives I get a diff(0,r) in the result that won't go away even when using simplify:
formula := diff(u(r),r,r);
subs(diff(u(r),r) = 0, formula);
d
-- 0
dr
(My actual formula is quite long involving first and second derivatives of two variables. I know that all derivatives with respect to a certain variable are 0 and I'd like to remove them).

One way is to use the simplify command with so-called side-relations.
formula := diff(u(r),r,r) + 3*cos(diff(u(r),r,r))
+ diff(u(r),r) + x*(4 - diff(u(r),r,r,r)):
simplify( formula, { diff(u(r),r) = 0 } );
3 + 4 x
formula2 := diff(u(r,s),s,s) + 3*cos(diff(u(r,s),r,r))
+ diff(u(r,s),r) + x*(4 - diff(u(r,s),r,s,r,r)):
simplify( formula2, { diff(u(r,s),r) = 0 } );
/ 2 \
| d |
3 + |---- u(r, s)| + 4 x
| 2 |
\ ds /
[edit] I forgot to answer your additonal query about why you got d/dr 0 before. The answer is because you used subs instead of 2-argument eval. The former does purely syntactic substitution, and doesn't evaluate the result. The latter is the one that people often need, without knowing it, and does "evaluation at a (particular) point".
formulaA := diff(u(r),r,r):
subs(diff(u(r),r) = 0, formulaA);
d
--- 0
dr
%; # does an evaluation
0
eval(formulaA, diff(u(r),r) = 0);
0
formulaB := diff(u(r,s),s,r,r,s):
eval(formulaB, diff(u(r,s),r) = 0);
0
You can see that any evaluation of those d/dr 0 objects will produce 0. But it's is often better practice to use 2-argument eval than it is to do eval(subs(...)). People use subs because it sounds like "substitution", I guess, or they see others use it. Sometimes subs is the right tool for the job, so it's important to know the difference.

Related

Julia: calculate an inner product using boolean algebra

I've got two boolean vectors a = [1,1,1] and b = [0,1,1] where obviously 1 stands for true and 0 for false.
I want to calculate their inner product using Boolean algebra. The result I want is therefore
1*0 + 1*1 + 1*1 = 0 + 1 + 1 = 0
because addition plays the role of exclusive or (XOR).
I know that the product part can be done like this
a = [true, true, true] # could also use ones(Bool, 3)
b = [false, true, true]
bitwise_prod = a .& b
but I don't know how to do the sum. Any ideas?
I've actually found a good way now. First of all I don't need to use Boolean variables
a = [1, 1, 1] # or ones(Int, 3)
b = [0, 1, 1]
I can then use reduce with the xor function.
reduce(xor, a .& b)
Note that I tried using the bitwise xor operator $ that is found in the documentation (inside an anonymous function) but this operator is deprecated and Julia 0.6.2 suggests the xor function instead. I do think having the function name makes it very neat.

Nim enumerate function like Python

Learning Nim and I like it resemblence of Python (but fast). In Python I can do this:
item_index = [(idx, itm) for idx, itm in enumerate(row)]
Im looking for a way to enumerate a Nim sequence so I would write this:
item_index = lc[(idx, itm) | (idx, itm <- enumerate(row))]
Does this functionality exist? I'm sure you could create it, maybe with a proc, template or macro it but I'm still very new, and these seem hard to create myself still. Here is my attempt:
iterator enumerate[T](s: seq[T]): (int, T) =
var i = 0
while i < len(s):
yield (i, s[i])
i += 1
I'm a newbie with nim, and I'm not really sure what you want, but...
If you use two variables in a for statement, you will get the index and the value:
for x, y in [11,22,33]:
echo x, " ", y
Gives:
0 11
1 22
2 33
HTH.

Using solve and/or linsolve with the symbolic toolbox in R2010b

I asked a question a few days ago here and got an answer that seems like it would work- it involves using linsolve to find the solutions to a system of equations that are all modulo p, where p is a non-prime integer.
However, when I try to run the commands from the provided answer, or the linsolve help page, I get an error saying linsolve doesn't support arguments of type 'sym'. Is using linsolve with sym variables only possible in R2013b? I've also tried it with my school's copy, which is R2012b. Here is the code I'm attempting to execute (from the answer at the above link):
A = [0 5 4 1;1 7 0 2;8 1 0 2;10 5 1 0];
b = [2946321;5851213;2563617;10670279];
s = mod(linsolve(sym(A),sym(b)),8)
And the output is:
??? Undefined function or method linsolve' for input arguments of type 'sym'.
I've also tried to use the function solve for this, however even if I construct the equations represented by the matrices A and b above, I'm having issues. Here's what I'm attempting:
syms x y z q;
solve(5*y + 4*z + q == 2946321, x + 7*y + 2*q == 5851213, 8*x + y + 2*q == 2563617, 10*x + 5*y + z == 10670279,x,y,z,q)
And the output is:
??? Error using ==> char
Conversion to char from logical is not possible.
Error in ==> solve>getEqns at 169
vc = char(v);
Error in ==> solve at 67
[eqns,vars] = getEqns(varargin{:});
Am I using solve wrong? Should I just try to execute my code in R2013b to use linsolve with symbolic data types?
The Symbolic Math toolbox math toolbox has changed a lot (for the better) over the years. You might not have sym/linsolve, but does this work?:
s = mod(sym(A)\sym(b),8)
That will basically do the same thing. sym/linsolve just does some extra input checking and and rank calculation to mirror the capabilities of linsolve.
You're using solve correctly for current versions, but it looks like R2010b may not understand the == operator (sym/eq) in this context. You can use the old string format to specify your equations:
eqs = {'5*y + 4*z + q = 2946321',...
'x + 7*y + 2*q = 5851213',...
'8*x + y + 2*q = 2563617',...
'10*x + 5*y + z = 10670279'};
vars = {'x','y','z','q'};
[x,y,z,q] = solve(eqs{:},vars{:})

Name for the logical operator A & (~B)

Is there a name for logical AND with the negation (~) of the second variable, i.e:
A & (~B)
The truth table for such operation is:
0 & (~0) = 0
0 & (~1) = 0
1 & (~0) = 1
1 & (~1) = 0
And in longer sequences of bits,
A = 10110011
B = 10111001
A & B = 10110001
A &(~B) = 00000010
PS - I'm interested with OR with the negation of the second variable, too.
Incredible. A & (~B) is called Material nonimplication, and A | (~B) is called Material implication Seems that every possible binary operation has a name.
The set theoretic term is the "relative complement" of B with respect to A.
I like to call it bit clearer. You find it in code also in the
form by using an assignment:
A = A & ~B
Or more compact as:
A &= ~B
Example: Before: A = 0x0007, B=0x0004
After: A = 0x0003
It has the effect that it clears the bits B from A. But relative
complement, and hence the name difference, you could write
it as follows A \ B, like the set difference, is also a good name.

if a<x<b in matlab

I need any help for Matlab's thinking method.Ithink I can explaine my problem with a simple example better. Let's say that I have a characteristic function x=y+x0, x0's are may starting values.Then I want to define my function in a grid.Then I define a finer grid and I want to ask him if he knows where an arbitrary (x*,y*) is.To determine it mathematically I should ask where the corresponding starting point (x0*) is. If this startig point stay between x(i,1)
clear
%%%%%%%%%%&First grid%%%%%%%%%%%%%%%%%%%%
x0=linspace(0,10,6);
y=linspace(0,5,6);
for i=1:length(x0)
for j=1:length(y)
x(i,j)=y(j)+x0(i);
%%%%%%%%%%%%%%%%%%%Second grid%%%%%%%%%%%%%%%%%%
x0fine=linspace(0,10,10);
yfine=linspace(0,5,10);
for p=1:length(x0fine)
for r=1:length(yfine)
xfine(p,r)=yfine(r)+x0fine(p);
if (x(i,1)<xfine(p,1)')&(x0fine(p,1)'<x(i+1,1))%%%%I probabliy have my first mistake %here
% if y(j)<yfine(r)<y(j+1)
% xint(i,j)=(x(i,j)+x(i,j+1)+x(i+1,j)+x(i+1,j+1))./4;
% else
% xint(i,j)= x(i,j);
%end
end
end
end
end
While a < b < c is legal MATLAB syntax, I doubt that it does what you think it does. It does not check that a < b and b < c. What it does is, it checks whether a < b, returning a logical value (maybe an array of logicals) and then, interpreting this logical as 0 or 1, compares it against c:
>> 2 < 0 < 2
ans =
1
>> 2 < 0 < 1
ans =
1
>> 0 < 0 < 1
ans =
1
First in matlab you should avoid as much as possible to do loops.
For instance you can compute x and xfine, with the following code:
x0=linspace(0,10,6);
y=linspace(0,5,6);
x=bsxfun(#plus,x0',y);
x0fine=linspace(0,10,10);
yfine=linspace(0,5,10);
xfine=bsxfun(#plus,x0fine',yfine);
Then given (X*,y*) your want to fine x0*, in your simple example, you can just do: x0*=x*-y*, I think.

Resources