How to substitute expressions containing units when using the ezunits package? - maxima

Without specifying units, I can express area and volume and have Maxima show the relationship:
(%i1) areaNoUnits: area = width * length$
(%i2) volumeNoUnits: volume = area * height$
(%i3) volumeNoUnits, areaNoUnits;
(%o3) volume = height length width
(%i4) subst(areaNoUnits, volumeNoUnits);
(%o4) volume = height length width
Now I want to specify units so I will use the ezunits package.
The ` (backtick) operator is the building block of ezunits:
An expression a ` b represents a dimensional quantity, with a indicating a nondimensional quantity and b indicating the dimensional units.
When I add units to the area and volume expressions, evaluation and substitution do not work:
(%i1) load ("ezunits")$
(%i2) areaWithUnits: area ` m^2 = (width ` m) * (length ` m);
2 2
(%o2) area ` m = length width ` m
(%i3) volumeWithUnits: volume ` m^3 = (area ` m^2) * (height ` m);
3 3
(%o3) volume ` m = area height ` m
(%i4) volumeWithUnits, areaWithUnits;
3 3
(%o4) volume ` m = area height ` m
(%i5) subst(areaWithUnits, volumeWithUnits);
3 3
(%o5) volume ` m = area height ` m
The expected output is:
volumeWithUnits, areaWithUnits;
3 3
volume ` m = height length width ` m
I do not see a function in the ezunits package to do evaluation or substitution. What is the right way to do this?

I would phrase it like this:
(%i2) load (ezunits) $
(%i3) width: W ` m;
(%o3) W ` m
(%i4) length: L ` m;
(%o4) L ` m
(%i5) area: width * length;
2
(%o5) L W ` m
(%i6) height: H ` m;
(%o6) H ` m
(%i7) volume: area * height;
3
(%o7) H L W ` m
I wrote each part as conceptualname: symbolforquantity ` unit and then wrote just conceptualname in further calculations, instead of conceptualname ` unit.
The substitution you tried in %i5 didn't work because subst is a purely formal substitution -- if there isn't a literal subexpression which is the same as the substituted-for expression, it doesn't match; subst doesn't look for rearrangements or factorizations which could help make a match. There are ways to work around that, so it might be possible to make your original formulation work, but I think it's better overall to sidestep the problem and work with conceptualname and symbolforquantity ` unit.
To say a little about what more one could do with expressions like %o7 above. There are at least two ways to replace symbols H, L, and W with specific values. One is to call subst:
(%i2) load (ezunits) $
(%i3) volume: H*L*W ` m^3;
3
(%o3) H L W ` m
(%i4) subst ([L = 20, W = %pi], volume);
3
(%o4) 20 %pi H ` m
Another is to make use of ev.
(%i5) ev (volume, L = 20, W = %pi);
3
(%o5) 20 %pi H ` m
Note that at the input prompt, something, someflags, somevalues is equivalent to ev(something, someflags, somevalues).
(%i6) volume, L = 20, W = %pi;
3
(%o6) 20 %pi H ` m
This is just a convenience. Within a function, one has to say ev(...); the shorter syntax isn't understood there.
ev is often convenient, but it's generally simpler to predict what the result is going to be with subst instead.

Related

Transforming Equations WxMaxima Ezunits

I'm having problems transforming equations, again...
Setting up the functions b(a) and c(b) works. Inserting them into each other also works to get from a temperature to a current c(b(a)). But now I want to flip it around a(c). The result should be like this a(c):= (c-(4`mA))*(25`degC)/(4`mA); But it's not working even with the ''-trick.
(%i1) load(ezunits);
(%o1) "C:/maxima-5.44.0/share/maxima/5.44.0/share/ezunits/ezunits.mac"
(%i7) a0: 0`degC;
am: 100`degC;
b0: 0`mV;
bm: 4`mV;
c0: 4`mA;
cm: 20`mA;
(a0) 0 ` degC
(am) 100 ` degC
(b0) 0 ` mV
(bm) 4 ` mV
(c0) 4 ` mA
(cm) 20 ` mA
(%i8) b(a):= (bm-b0)/(am-a0)*(a-a0)+b0;
(%o8) b(a):=(bm-b0)/(am-a0)*(a-a0)+b0
(%i9) c(b):= (cm-c0)/(bm-b0)*(b-b0)+c0;
(%o9) c(b):=(cm-c0)/(bm-b0)*(b-b0)+c0
(%i10) c(b(50`degC));
(%o10) 12 ` mA
(%i11) a(c):= dimensionally(solve(c(b(T)), T));
(%o11) a(c):=dimensionally(solve(c(b(T)),T))
(%i12) a(12`mA);
(%o12) [T=(-25) ` degC]
(%i13) a(c):= ''(dimensionally(solve(c(b(T)), T)));
(%o13) a(c):=[T=(-25) ` degC]
(%i14) a(12`mA);
(%o14) [T=(-25) ` degC]
(%i15) oi: T, dimensionally(solve(c(b(T)), T));;
(oi) (-25) ` degC
(%i16) a(c):= (c-(4`mA))*(25`degC)/(4`mA);
(%o16) a(c):=((c-4 ` mA)*(25 ` degC))/4 ` mA
(%i17) a(12`mA);
(%o17) 50 ` degC
-->
It looks like you have omitted the specific value of c from solve(c(b(T)), T) -- what I mean is you need something like solve(c(b(T)) = c1, T) where c1 is the input value such as 12 ` mA.
This definition seems to work --
a(c1):= dimensionally(solve(c(b(T)) = c1, T));
Then I get
(%i22) a(12`mA);
(%o22) [T = 50 ` degC]
When you omit the ... = c1, you are effectively solving for ... = 0, that's why you get T = (- 25) ` degC.
The other variation a(c1) := ''(...) should also work, although I didn't try it.
You can write a(c) := dimensionally(solve(c(b(T)) = c, T)), i.e., using the same name for the variable c and the function c, but it's easy to get mixed up, and also I am hoping the change that behavior in the near future (with the implementation of lexical scope of symbols) which will make that not work anymore.

Caffe - Concat layer input and output

I read about Concat layer on Caffe website. However, I don't know if my understanding of it is right.
Let's say that as an input I have two layers that can be described as W1 x H1 x D1 and W2 x H2 x D2, where W is the width, H is height and D is depth.
Thus, as I understand with Axis set to 0 output will be (W1 + W2) x (H1 + H2) x D, where D = D1 = D2.
With Axis set to 1 output will be W x H x (D1 + D2), where H = H1 = H2 and W = W1 = W2.
Is my understanding correct? If no I would be grateful for an explanation.
I'm afraid you are a bit off...
Look at this caffe.help.
Usually, data in caffe is stored in 4D "blobs": BxCxHxW (that is, batch size by channel/feature/depth by height by width).
Now if you have two blobs B1xC1xH1xW1 and B2xC2xH2xW2 you can concatenate them along axis: 1 (along channel dimension) to form an output blob with C=C1+C2. This is only possible iff B1==B2 and H1==H2 and W1==W2, resulting with B1x(C1+C2)xH1xW1

Rearrange equation using Maxima

In Maxima I have an equation that is like:
eq : c0*a + d0*a + c1*b - c2*p - c4*q = c5*r
Is there a command that allows me to arrive at:
(c0 + d0)*a + c1*b = c2*p + c4*q + c5*r
In short, I want to choose which variables end on either the left or right hand side and I want to write it such that there is only one occurence of the variables I select (in this case a, b, p, q, r).
Perhaps coefmatrix is useful for this.
(%i1) display2d : false $
(%i2) eq : c0*a + d0*a + c1*b - c2*p - c4*q = c5*r $
(%i3) vars : [a, b, p, q, r] $
(%i4) coeffs : coefmatrix ([eq], vars);
(%o4) matrix([d0+c0,c1,-c2,-c4,-c5])
(%i5) coeffs . vars;
(%o5) (-c5*r)-c4*q-c2*p+a*(c0+d0)+b*c1
Note that both arguments of coefmatrix must be lists.

Maxima - internal numeric representation ruins calculation

How can I tell Maxima to solve the following problem? (The "solve" part):
I did:
load(distrib);
fpprec: 100;
bftorat:true;
solve(2*bfloat(cdf_normal(x,0,1))-1=0.99999999999999999968130594071b0, [x]);
%,numer
Got:
(%o1) "/usr/share/maxima/5.32.1/share/distrib/distrib.mac"
(%o2) 100
(%o3) true
`rat' replaced -1.99999999999999999968130594071B0 by -199999999999999999968130594071/100000000000000000000000000000 = -1.99999999999999999968130594071B0
`rat' replaced 5.0B-1 by 1/2 = 5.0B-1
`rat' replaced 5.0B-1 by 1/2 = 5.0B-1
`rat' replaced 7.071067811865475244008443621048490392848359376884740365883398689953662392310535194251937671638207864B-1 by 118807941462947422469655519336079782367473013592460/168019802134529020067676914738440478110633605571601 = 7.071067811865475244008443621048490392848359376884740365883398689953662392310535194251937671638207864B-1
`rat' replaced 7.071067811865475244008443621048490392848359376884740365883398689953662392310535194251937671638207864B-1 by 118807941462947422469655519336079782367473013592460/168019802134529020067676914738440478110633605571601 = 7.071067811865475244008443621048490392848359376884740365883398689953662392310535194251937671638207864B-1
(%o4) [x=
(168019802134529020067676914738440478110633605571601*inverse_erf(99999999999999999968130594071/100000000000000000000000000000))/118807941462947422469655519336079782367473013592460]
inverse_erf: inverse_erf(1.0) is undefined.
-- an error. To debug this try: debugmode(true);
Further tried (to see if the rational replacement affects inverse_erf):
inverse_erf(9.9999999999999999968130594071b−1);
gamma_incomplete: continued fractions failed for gamma_incomplete(5.0b-1, 1.675965338889773975600843228854238162008399514002414970690458801529039878850010279559673197036592113916729947593696740535214189646774061729913734402353264492788885098143556404059170138591463120333687838496039284224858192931635551067412157341539627014907074717352945374476804912353312948754404927014555821149803440423871160460111635311071245528519256957555845418916034380535359079516879576795825857468710891474077746896341697834315575814989209244705740463652472196503944998297956825510866851943203353716451062616549067258800559231646552924469724160521456041856694702333938138297284123098699530288993519920353577729741393726951293645734447176179175560311787410907660921783058989196733914852345086206761158383783714360001646880454976428470630991611033582649957934844195398077660932657131767136966243415075424193909691302431604307524134764326959890911322928113456129784004060990585972427799869290459688878023964671879763390452043333273689436077956597199441415992202082578463153853017929328667898523224007b0).
-- an error. To debug this try: debugmode(true);
My advice is to try to solve the equation with a symbolic value and replace it with a numerical value later on. Here's what I get:
(%i1) load (distrib) $
(%i2) fpprec : 100 $
(%i3) solve (2 * cdf_normal (x, 0, 1) - 1 = a, x);
(%o3) [x = sqrt(2) inverse_erf(a)]
(%i4) %, a = 0.99999999999999999968130594071b0;
(%o4) [x = 6.33712711592763726142078700288254243769449484599872720866948195829\
6543071614144180808554952052800789b0 sqrt(2)]
(%i5) bfloat (%);
(%o5) [x = 8.96205111382716157629734310948891265577140990195458570850348910814\
5354833288951128435123731428126303b0]
Actually, after a bit of further study, replacing:
%,numer
with
%,bfloat
at the end of the original problem does the trick. I thought numer works with bfloats, but it only uses rational approximations, and that is why I received errors. However, using bfloat gives the correct answer.

How do I use lhs and rhs to define a function?

In the Maxima session below, how come f(1) is not 0?
(%i1) eq: 2 * x + 1 = 3;
(%o1) 2 x + 1 = 3
(%i2) f(x) := lhs(eq) - rhs(eq);
(%o2) f(x) := lhs(eq) - rhs(eq)
(%i3) f(1);
(%o3) 2 x - 2
the process of function calling in maxima here binds x to 1 in the function
definition, lhs(eq)-rhs(eq). That has no x in it, so that binding does nothing.
Next, lhs(eq) is evaluated to 2*x+1. rhs(eq) is evaluated to 3. etc.
Do you always want the same equation eq? perhaps you want to do
define(f(x),lhs(eq)-rhs(eq));
to check what the definition is, try
grind(f);
If you want to vary the equation maybe something like
g(val, eq) := subst(val,x, lhs(eq)-rhs(eq)) ;
would do.

Resources