How to get result ode2 as f(x)? - maxima

(%i1) 'diff(h,t) = - (r1^2/r2^2)*sqrt(2*g*h);
(%o1) 'diff(h,t,1)=-(sqrt(2)*sqrt(g*h)*r1^2)/r2^2
(%i2) ode2 (%o1,h,t);
(%o2) -(sqrt(2)*sqrt(g*h)*r2^2)/(g*r1^2)=t+%c
How to get result as function h(t)?

You can use to_poly_solve
load("to_poly_solve") $
eq: 'diff(h,t) = - (r1^2/r2^2)*sqrt(2*g*h) $
sol_diff: ode2 (eq,h,t) $
/* solve initial value problem */
sol_diff_ic: ic1(sol_diff, t=0, h=h0) $
assume(g>0, h0>0, r1>0, r2>0, t>0) $
/* get solution as a %union object, %ifs suggest that you have a
critical `t' where `h' becomes negative */
sol_union: to_poly_solve(sol_diff_ic, h);
/* "quick and dirty" extraction of one of the branches */
hext(e):= block([opsubst: true, ans: []], subst(lambda([r, l], if r='h then ans: cons(l, ans)), "=", e), ans) $
sol_h: hext(sol_union)[1] $
/* plot solutions */
num1: subst([r1=1, r2=1, g=1/10, h0=1], sol_h) $
num2: subst([r1=1, r2=1, g=1, h0=1], sol_h) $
plot2d([num1, num2], [t, 0, 1], [legend, false], [ylabel, "h"], [gnuplot_term, "dumb"]) $
1 +--------------------+
|++++ + + + |
0.9 |++ ++++ +|
0.8 |+ + ++++ +|
| + ++++ |
0.7 |+ ++ +++++ +|
0.6 |+ ++ +++|
| ++ |
0.5 |+ ++ +|
| +++ |
0.4 |+ ++ +|
0.3 |+ +++ +|
| +++ |
0.2 |+ +++ +|
0.1 |+ +++|
| + + + + |
0 +--------------------+
0 0.2 0.4 0.6 0.8 1
t

Related

Rearranging equation with division using Maxima

I'm trying to rearrange equation using maxima, but coefmatrix function does not work well with equation that has some division such as RL_parallel
I am getting all zeroes for the coefficients
RL_series: Rs + %i*w*Ls;
RL_parallel: ratsimp( 1 / [1/Rp + 1/(%i*w*Lp)] );
display2d : false $
eq : RL_series = RL_parallel $
vars : [Rs, Rp, Ls, Lp] $
coeffs : coefmatrix ([eq], vars);
coeffs . vars;
The main problem here is that the equation isn't linear in the variables specified, so extracting a coefficient matrix doesn't make any sense, unfortunately.
I am guessing that you want to solve the equation for vars. It seems to me that you'll need additional data, but it could well be that I'm missing something.
Here's what I get. Note that [ ... ] is a list; use ( ... ) for grouping expressions.
(%i1) RL_series: Rs + %i*w*Ls;
(%o1) %i Ls w + Rs
(%i2) RL_parallel: ratsimp( 1 / (1/Rp + 1/(%i*w*Lp)) );
Lp Rp w
(%o2) ------------
Lp w - %i Rp
(%i3) eq : RL_series = RL_parallel;
Lp Rp w
(%o3) %i Ls w + Rs = ------------
Lp w - %i Rp
(%i4) vars : [Rs, Rp, Ls, Lp];
(%o4) [Rs, Rp, Ls, Lp]
(%i5) denom(rhs(eq)) * eq;
(%o5) (Lp w - %i Rp) (%i Ls w + Rs) = Lp Rp w
(%i6) expand (%);
2
(%o6) %i Lp Ls w + Lp Rs w + Ls Rp w - %i Rp Rs = Lp Rp w
Assuming that vars are real, we can separate the real and imaginary parts of the equation.
(%i7) [realpart(%), imagpart(%)];
2
(%o7) [Lp Rs w + Ls Rp w = Lp Rp w, Lp Ls w - Rp Rs = 0]
(%i8) first(%) / (Lp*Rp*w);
Lp Rs w + Ls Rp w
(%o8) ----------------- = 1
Lp Rp w
(%i9) expand (%);
Rs Ls
(%o9) -- + -- = 1
Rp Lp
So one of the equations has a product Xs*Xp and the other has the ratio Xs/Xp. Maybe that helps direct the search for additional data.
At this point I don't know what more can be said. If anyone can comment on this point I would be interested to hear it.
EDIT: OP says that the goal is to solve for Rp and Lp in terms of Rs and Ls. Given that, we can solve the two equations we obtained in %o7.
(%i10) %o7[2];
2
(%o10) Lp Ls w - Rp Rs = 0
(%i11) solve ([%o9, %o10], [Rp, Lp]);
2 2 2 2 2 2
Ls w + Rs Ls w + Rs
(%o11) [[Rp = ------------, Lp = ------------], [Rp = 0, Lp = 0]]
Rs 2
Ls w
(%i12) subst (%o11[1], [%o9, %o10]);
2 2 2
Ls w Rs
(%o12) [------------ + ------------ = 1, 0 = 0]
2 2 2 2 2 2
Ls w + Rs Ls w + Rs
(%i13) ratsimp(%);
(%o13) [1 = 1, 0 = 0]

Grammar for expressions. Four levels of precedence. LL(1)

I need to know how to make a grammar for expressions to create a parser and ast. I have four levels of precedence:
1. ** !
2. * / % &
3. + - ^ |
4. <= >= < >
I have made this:
Exps -> RExp4
RExp4 -> OpEq Exps | RExp3
RExp3 -> OpAd Exps | RExp2
RExp2 -> OpMul Exps | RExp1
RExp1 -> OpExp Exps | Exp
Exp -> Val | '('Exps')'
OpExp -> ** | !
OpMul -> * | / | % | &
OpAd -> + | - | ^
OpEq -> <= | >= | < | >
Val -> Id | Int
I'm not sure if this will work because when I make the tree in a paper I do not get the correct form for expressions like:
x*7+7
Basically because I make the sum first. My gramma have to be LL(1) and recursive by the right because my compiler will be top-down.
Thanks for help and sorry for my english
Edit
Sorry, I was wrong, I mean parser top-down.The problem I see on paper is the next. I have the next expression "7*5+5" for example and the order that my BNF follow is the next:
Take 7 with Exp in Exps, that follow to Val
Go to RExp and continue to RExp3.
Take '*' and then return to Exps.
The tree that I see on paper is the next:
*
/ \
3 +
/ \
7 5
The tree that I should have is the next:
+
/ \
* 5
/ \
7 5

Why doesn't ANTLR "over-reduce" this expression?

I have the following grammar:
expr : factor op ;
op
: '+' factor op
| // Blank rule for left-recursion elimination
;
factor
: NUM
| '(' expr ')'
;
NUM : ('0'..'9')+ ;
I supply 2 + 3, using expr as the start rule. The resulting parse tree from ANTLR is correct; however, I think I am misunderstanding the shift-reduce methods it uses.
I would expect the following to happen:
Step # | Parse Stack | Lookahead | Unscanned | Action
1 | | NUM | + 3 | Shift
2 | NUM | + | 3 | Reduce by factor -> NUM
3 | factor | + | 3 | Shift a 'null'?
4 | factor null | + | 3 | Reduce by op -> null
5 | factor op | + | 3 | Reduce by expr -> factor op
6 | expr | + | 3 | Shift
7 | expr + | NUM | | Shift
8 | expr + NUM | | | Reduce by factor -> NUM
9 | expr + factor | | | ERROR (no production)
I would've expected an error to occur at step 3 wherin the parser would shift a null onto the stack as a prerequisite to reduceing the factor "up" to an expr.
Does ANTLR only shift a null when it's strictly "required" because the resulting reduce will satisfy the grammar?
It seems to me that ANTLR doesn't use a shift-reduce parser; the generated parsers are recursive descent using an arbitrary amount of lookahead.
The steps of the parser would be something like:
Rule | Consummed | Input
--------------+-----------+------
expr | | 2 + 3
..factor | | 2 + 3
....NUM | 2 | + 3
..op | 2 | + 3
....'+' | 2 + | 3
....factor | 2 + | 3
......NUM | 2 + 3 |
....op | 2 + 3 |
......(empty) | 2 + 3 |
From what I read about ANTLR, you could achieve the same result with the following changes to the original grammar:
expr: factor op*;
op: '+' factor;
...

Neo4j Divide ( / ) by Zero ( 0 )

In neo4j I am querying
MATCH (n)-[t:x{x:"1a"}]->()
WHERE n.a > 1 OR n.b > 1 AND toFloat(n.a) / (n.a+n.b) * 100 < 90
RETURN DISTINCT n, toFloat(n.a) / (n.a + n.b) * 100
ORDER BY toFloat(n.a) / (n.a + n.b) * 100 DESC
LIMIT 10
but I got / by zero error.
Since I declared one of n.a or n.b should be 1, if both zero it should skip that row and I shouldn't get this error. This looks like a logic issue in Neo4j. There is no problem when I delete AND toFloat(n.a)/(n.a+n.b)*100 < 90 from WHERE clause. But I want the results only lower than 90. How can I overcome this?
Can either of n.a or n.b be negative? I was able to reproduce this with:
WITH -2 AS na, 2 AS nb
WHERE (na > 1 OR nb > 1) AND toFloat(na)/(na+nb)*100 < 90
RETURN na, nb
And I get: / by zero
Perhaps try changing your WHERE clause to:
WITH -2 AS na, 2 AS nb
WHERE (na + nb > 0) AND toFloat(na)/(na+nb)*100 < 90
RETURN na, nb
And I get: zero rows.
It seems the second condition, toFloat(na) / (na + nb) * 100 < 90, is tested before the first. Look at the Filter(1) operator in this execution plan:
+--------------+---------------+------+--------+--------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Operator | EstimatedRows | Rows | DbHits | Identifiers | Other |
+--------------+---------------+------+--------+--------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Projection | 1 | 3 | 0 | anon[111], anon[138], n, toFloat(n.a)/(n.a + n.b)* 100 | anon[111]; anon[138] |
| Top | 1 | 3 | 0 | anon[111], anon[138] | { AUTOINT6}; |
| Distinct | 0 | 3 | 24 | anon[111], anon[138] | anon[111], anon[138] |
| Filter(0) | 0 | 3 | 6 | anon[29], n, t | t.x == { AUTOSTRING0} |
| Expand(All) | 1 | 3 | 6 | anon[29], n, t | ( n#7)-[t:x]->() |
| Filter(1) | 1 | 3 | 34 | n | (Ors(List(n#7.a > { AUTOINT1}, Multiply(Divide(ToFloatFunction( n#7.a),Add( n#7.a, n#7.b)),{ AUTOINT3}) < { AUTOINT4})) AND Ors(List( n#7.a > { AUTOINT1}, n.b > { AUTOINT2}))) |
| AllNodesScan | 4 | 4 | 5 | n | |
+--------------+---------------+------+--------+--------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
You can get around this by force breaking the filter into two clauses.
MATCH (n)-[t:x { x:"1a" }]->()
WHERE n.a > 1 OR n.b > 1
WITH n
WHERE toFloat(n.a) / (n.a + n.b) * 100 < 90
RETURN DISTINCT n, toFloat(n.a) / (n.a + n.b) * 100
ORDER BY toFloat(n.a) / (n.a + n.b) * 100 DESC
LIMIT 10
I found this behavior surprising, but as I think about it I suppose it isn't wrong for the execution engine to rearrange the filter in this way. There may be the assumption that the condition will abandon early on failing the first declared condition, but Cypher is exactly that: declarative. So we express the "what", not the "how", and in terms of the "what" A and B is equivalent to B and A.
Here is the query and a sample graph, you can check if it translates to your actual data:
http://console.neo4j.org/r/f6kxi5

Need documentation for GPUImageColorMatrixFilter. Where can I find documentation for what the matrix means?

I would like to implement a bandpass filter using GPUImageColorMatrixFilter. Basically, blue would equal floor(blue - (k*red)) and both red and green would just end up being zero. Where can I find documentation indicating what the columns and rows of the matrix mean?
My intuition would suggest that the 4x4 matrix is following the standard RGBA order and judging by the examples (see for instance GPUImageSepiaFilter) it looks like I'm right.
For instance, this is the identity GPUMatrix4x4
R G B A
| 1 0 0 0 | red
| 0 1 0 0 | green
| 0 0 1 0 | blue
| 0 0 0 1 | alpha
Let's name each coefficient
R G B A
| a b c d | red
| e f g h | green
| i j k l | blue
| m n o p | alpha
Applying the matrix to a RGBA color will result in the following R'G'B'A' color where the components are computed as
R' = a*R + b*G + c*B + d*A
G' = e*R + f*G + g*B + h*A
B' = i*R + j*G + k*B + l*A
A' = m*R + n*G + o*B + p*A
which is nothing but the following matrix multiplication
| a b c d | |R| |R'|
| e f g h | x |G| = |G'|
| i j k l | |B| |B'|
| m n o p | |A| |A'|

Resources