Maxima greek symbols display but dont work? - maxima

I am trying to calculate the energy of a photon in (wx)Maxima, using physical_constants and ezunits:
|lambda| : 800 * 10^-9 ` m;
Where | denotes Escape... which displays correctly as a greek lambda, but wxMaxima does not confirm the value as it does usually.
So next I try to use E = h*c/lambda
constvalue (%h * %c / |lambda|) `` J;
But again wxMaxima does not show any result.
Everything works fine if I use lambda spelled in full instead of a greek symbol...
Is the |greek| only good for text inputs?

It's fairly simple: lambda denotes the anonymous lambda function and that one can not be used as a symbol. Use %lambda instead.
A similar thing applies to phi: phi can be used as a symbol, but %phi is a constant with the value 1.61... (golden ratio).
Taking a derivative with respect to %phi instead of phi will for that reason always result in 0 ;-)

Related

Bad argument to 'random'

This is my code
while true do
script.Parent.Position = Vector3.new((math.random(-41.994,15.471)),0.5,(math.random(129.514,69.442)))
script.Parent.Color = Color3.new(math.random(0,255), math.random(0,255), math.random(0,255))
wait(1)
end
The programming language I am using is Lua
When I try to use this code I am presented with this error:
"15:50:47.926 - Workspace.rock outer walls.Model.Rocks.Part0.Script:2: bad argument #2 to 'random' (interval is empty)"
The purpose of the code is to randomly teleport the part the script is in around but not to far away and at the same y axis.
Can somebody please give me some form of explanation
Ps. A while ago I made a rude post on this website because I was confused at how to do a lot of things and now I understand some stuff better so I would like to apologize for my idiocy ~Zeeen
In Lua, math.random can be called 3 ways:
with no arguments
with 1 integer argument
with 2 integer arguments
It does not accept values like -41.994 or 15.471 this is why you're getting the error.
If your change your values to -41 or 15 you shouldn't see an error any more.
Lua 5.3 reference manual: http://www.lua.org/manual/5.3/manual.html#pdf-math.random
math.random ([m [, n]])
When called without arguments, returns a pseudo-random float with uniform distribution in the range [0,1). When called with two integers m and n, math.random returns a pseudo-random integer with uniform distribution in the range [m, n]. (The value n-m cannot be negative and must fit in a Lua integer.) The call math.random(n) is equivalent to math.random(1,n).
This function is an interface to the underling pseudo-random generator function provided by C.
As Nifim's answer correctly points out, there are three ways to call math.random in Lua.
With no arguments, it returns a real number in the range 0.0 to 1.0.
With one or two integer arguments, it returns an integer.
None of these directly give you want you want, which I presume is a random real number in a specified range.
To do that, you'll need to call math.random with no arguments and then adjust the result.
For example, if you wanted a random number between 5.0 and 10.0, you could use
math.random() * 5.0 + 5.0
Consider writing your own wrapper function that takes two floating-point arguments and calls math.random.
function random_real(x, y)
return x + math.random() * (y-x)
end

How do I tell Maxima about valid approximations of subexpressions of a large expression?

I have a fairly large expression that involves a lot of subexpressions of the form (100*A^3 + 200*A^2 + 100*A)*x or (-A^2 - A)*y or (100*A^2 + 100*A)*z
I know, but I don't know how to tell Maxima this, that it in this case is valid to make the approximation A+1 ~ A, thereby effectively removing anything but the highest power of A in each coefficient.
I'm now looking for functions, tools, or methods that I can use to guide Maxima in dropping various terms that aren't important.
I have attempted with subst, but that requires me to specify each and every factor separately, because:
subst([A+1=B], (A+2)*(A+1)*2);
subst([A+1=B], (A+2)*(A*2+2));
(%o1) 2*(A+2)*B
(%o2) (A+2)*(2*A+2)
(that is, I need to add one expression for each slightly different variant)
I tried with ratsimp, but that's too eager to change every occurrence:
ratsubst(B, A+1, A*(A+1)*2);
ratsubst(B, A+1, A*(A*2+2));
(%o3) 2*B^2-2*B
(%o4) 2*B^2-2*B
which isn't actually simpler, as I would have preferred the answer to have been given as 2*B^2.
In another answer, (https://stackoverflow.com/a/22695050/5999883) the functions let and letsimp were suggested for the task of substituting values, but I fail to get them to really do anything:
x:(A+1)*A;
let ( A+1, B );
letsimp(x);
(x)A*(A+1)
(%o6) A+1 --\> B
(%o7) A^2+A
Again, I'd like to approximate this expression to A^2 (B^2, whatever it's called).
I understand that this is, in general, a hard problem (is e.g. A^2 + 10^8*A still okay to approximate as A^2?) but I think that what I'm looking for is a function or method of calculation that would be a little bit smarter than subst and can recognize that the same substitution could be done in the expression A^2+A as in the expression 100*A^2+100*A or -A^2-A instead of making me create a list of three (or twenty) individual substitutions when calling subst. The "nice" part of the full expression that I'm working on is that each of these A factors are of the form k*A^n*(A+1)^m for various small integers n, m, so I never actually end up with the degenerate case mentioned above.
(I was briefly thinking of re-expressing my expression as a polynomial in A, but this will not work as the only valid approximation of the expression (A^3+A^2+A)*x + y is A^3*x + y -- I know nothing about the relative sizes of x and y.

Matrix calculator - pascal program - command line

I would like to make matrix calculator, but I struggle a little bit, how to make an input of the program. I have commands that user can use in calculator. Some takes 1 argument, 2 arguments or 3 arguments. I was inspired by program on this website http://www.ivank.net/blogspot/matrix_pascal/matrices.pas
But I don't really understand, how the input is made. Program from the website use parse, split procedures, but I don't know, how does it work. Does it exists some website, where it is good explained (Parse in Pascal)? I would like to really understand it.
This is, how it should looks like:
command: sum X Y
command: multiply X
command: transpose X
In the sample which inspired you, all the calculation is realized by the 'procedure parse(command:String);'.
The first step consists to extract the command and all parameters by:
com := Split(command, ' ');
In your case, you will obtain for 'command: sum X Y':
Length(com) = 3
com[0] = 'sum'; com[1] = 'X'; com[2] = 'Y';
But, be carefull, the 'X' and 'Y' parameters shall not have characters between numbers.

py3k print significant figures

In python3 is there a nice way to set significant figures - i.e if I have a list:
l = [2.2738257169723513, 2.2725769281387329, 2.3101812601089478]
I can use the nice new print system and do
print(*l,sep="\t")
But I'm unclear as to how to set the sigfig with out doing
m = "%.2f, %.2f, %.2f" % (l[0], l[1], l[2])
print(m)
I was wondering if there was an option to print to just say - print all floats to 2 dp?
I guess I could use a loop but that seems not very Python like
Actually, it is definitely pythonic, and it is the only way to do what you're asking. That said, you can still use a comprehension to make this more concise (in this cause a tuple, but you can use a list or use list(map():
# I've changed the name to float_list because l should not be
# used as a variable name in Python according to the standard
# style recommendations
print(*('{0:.2f}'.format(x) for x in float_list), sep="\t")

Small numbers in Objective C 2.0

I created a calculator class that does basic +,-, %, * and sin, cos, tan, sqrt and other math functions.
I have all the variables of type double, everything is working fine for big numbers, so I can calculate numbers like 1.35E122, but the problem is with extremely small numbers. For example if I do calculation 1/98556321 I get 0 where I would like to get something 1.01464E-8.
Should I rewrite my code so that I only manipulate NSDecimalNumber's and if so, what do I do with sin and cos math functions that accept only double and long double values.
1/98556321
This division gives you 0 because integer division is performed here - the result is an integer part of division. The following line should give you floating point result:
1/(double)98556321
integer/integer is always an integer
So either you convert the upper or the lower number to decimal
(double)1/98556321
or
1/(double)98556321
Which explicitely convert the number to double.
Happy coding....

Resources