Is "or" defined differently for Mathematica and Mathics? - mathics

In Mathematica, "or" looks to be defined with a double vertical bar. See https://reference.wolfram.com/language/ref/Or.html or https://mathematica.stackexchange.com/a/119763.
In Mathics 2.1.0, that doesn't seem to work:
In[16]:= If[1<0 || 2<3 || 3<4, 0, 1]
Syntax::sntxf: "If[1<0 " cannot be followed by " 2<3  3<4, 0, 1]" (line 1 of "<stdin>").
whereas the word "or" seems to work:
In[16]:= If[1<0 or 2<3 or 3<4, 0, 1]
Out[16]= 1
So do I have to use || in Mathematica and or in Mathics, or am I mistaken?

Mathics has a documentation, though it's a pdf for some reason, https://mathics.org/docs/mathics-latest.pdf
Page 11 at the moment (part of 3. Language Tutorials):
Comparisons and Boolean Logic
[...]
Truth values can be negated using ! (logical not) and combined using && (logical and) and || (logical or):
>> !True
False
>> !False
True
>> 3 < 4 && 6 > 5
True
&& has higher precedence than ||, i.e. it binds stronger:
>> True && True || False && False
True
>> True && (True || False) && False
False
based on that I would indeed expect || and && to work.
Also, there is an example on page 44 with If[x == 0.0 && y <= 0, 0.0, Sin[x ^ y] + 1 / Min[x, 0.5]] + 0.5].
Your error message may be the key here as the || became those boxed question marks, which may be something about character encoding. If it is a file you are running, it may be worth checking if it's ASCII.

Related

Check for equality between more than 2 values in Lua

In Lua 5.3.3, equalities between two values work normally using the == operator.
However, equalities between more than two values don't work.
> print(1 == 1 == 1)
false
>
How can I check if more than 2 values are equal to one another in Lua?
You should compare 2 values at a time:
print((1 == 1) and (1 == 1))
If you have specific needs and if this approach is not acceptable, then you could also write a dedicated function:
function EqualsAll (Values, Number)
local Equals = true
local Index = 1
while (Equals and (Index <= #Values)) do
if Values[Index] == Number then
Index = Index + 1
else
Equals = false
end
end
return Equals
end
You could use it as following:
> EqualsAll({1, 2, 3}, 1)
false
> EqualsAll({1, 1, 1}, 1)
true

Whats wrong with this if statement? Rails

I am building a reputation system where users get points if milestones (10, 100, 1000, ...) are archieved. I have this if statement line:
if (before_points < ((10 || 100 || 1000 || 10000 || 100000 || 1000000))) && (after_points >= ((10 || 100 || 1000 || 10000 || 100000 || 1000000)))
It should return true if the points where either less than 10 or 100 or 1000 ...before, and if the points were more or equal to either 10 or 100 or 1000 ... afterwards.
It works if it was below 10 before, and more than 10 afterwards, and I am not quite sure if it works with 100, but it doesnt work if the points were below 1000 before and more than 1000 afterwards.
Is this the correct way to do this? Is it better to do this with a switch/case?
A more compact way you could do it...
[10, 100, 1000, 10000, 100000, 1000000].any?{|n| before_points < n && after_points >= n}
That expression will return true if a boundary is crossed, or false otherwise
That's not really how logic operation work. The statement:
(10 || 100 || 1000 || 10000 || 100000 || 1000000)
will evaluate to 10. The || operator between 2 or more numbers will return first non-nil value, in this case that's 10, the first value. Related question.
And even if that weren't the case, if the before_points < 10 is true, the before_points < 1000000 would also be true and if only before_points < 1000000 was true, the if statement would still execute just the same as with before_points < 10, so the logic would be wrong.
Depending on what you want to solve, you could either use case or define your milestones in array and iterate values 10,100,...,1000000, setting new milestone each time the condition is still true.
Your assumption is wrong.
if (before_points < ((10 || 100 || ...
will first evaluate the part
10 || 100
which will always return 10 because 10 evaluates to truthy, hence this line
if (before_points < ((10 || 100 || 1000 || 10000 || 100000 || 1000000))) && (after_points >= ((10 || 100 || 1000 || 10000 || 100000 || 1000000)))
is effectively the same of
if (before_points < 10) && (after_points >= 10)
I'm not sure what you want to achieve, but it's probably better to use a case (this is just an example)
case
when before_points < 10 && after_points >= 10
# ...
when before_points < 100 && after_points >= 100
# ...
else
# ...
end

Ruby on Rails function defined failed tests

I have defined a function that takes in a number and returns true if it is a
power of 2. Otherwise, return false:
def is_power_of_two?(num)
n = 0
res = false
if num % 2 == 0
while 2^n <= num
if 2^n == num
res = true
end
n += 1
end
end
puts(n.to_s)
return res
end
# These are tests to check that your code is working. After writing
# your solution, they should all print true.
puts('is_power_of_two?(1) == true: ' + (is_power_of_two?(1) == true).to_s)
puts('is_power_of_two?(16) == true: ' + (is_power_of_two?(16) == true).to_s)
puts('is_power_of_two?(64) == true: ' + (is_power_of_two?(64) == true).to_s)
puts('is_power_of_two?(78) == false: ' + (is_power_of_two?(78) == false).to_s)
puts('is_power_of_two?(0) == false: ' + (is_power_of_two?(0) == false).to_s)
However, my test results turn out to fail four out of five:
0
is_power_of_two?(1) == true: false
16
is_power_of_two?(16) == true: false
64
is_power_of_two?(64) == true: false
77
is_power_of_two?(78) == false: false
0
is_power_of_two?(0) == false: true
The result printed out seems to match what's expected, however, the tests still failed. Does anyone know why this happened?
if you expecting ^ to calculate the power then that is wrong ^ is XOR to calculate power use **
2^2 # 0
2**2 # 4
You always want to check if it is true that it is a power of two, this way it returns false when it is not a power of two.
This feels like a homework question so I'm not going to give you the exact answer, but this should nudge you in the correct direction.
As mohamed-ibrahm says, you're using the wrong operator.
The caret is a bitwise XOR operation. So 2^3 == 1 (because decimal 2 is 010 in binary and decimal 3 is 011 in binary and as all bits are the same except the last, the result is 001 or decimal 1).
Exponentiation is done by double asterisks, so 2**3 == 8
Here's a description of the various operators.
http://www.tutorialspoint.com/ruby/ruby_operators.htm

Difference between == and === in Mathematica

I was under the impression that = is an assignment, == is a numeric comparison, and === is a symbolic comparison (as well as in some other languages == being equal to and === being identical to. However, looking at the following it would appear that this is not necessarily the case...
In: x == x
Out: True
In: x === x
Out: True
In: 5 == 5
Out: True
In: 5 === 5
Out: True
In: x = 5
Out: 5
In: 5 == x
Out: True
In: 5 === x
Out: True
In: 5 5 == 5x
Out: True
In: 5 5 === 5x
Out: True
In: x == y
Out: x == y
In: x === y
Out: False
In: y = x
Out: 5
In: x == y
Out: True
In: x === y
Out: True
So what exactly is the difference between == and === in Mathematica? I have been looking at the documentation but I still don't quite understand it.
One important difference is that === always returns True or False. == can return unevaluated (which is why it's useful for representing equations.)
In[7]:= y == x^2 + 1
Out[7]= y == 1 + x^2
In[8]:= y === x^2 + 1
Out[8]= False
There are some interesting cases where == returns unevaluated that are worth being aware of while programming. For example:
In[10]:= {} == 1
Out[10]= {} == 1
which can affect things like If[foo=={}, <true>, <false>].
== and === are very similar in the sense that it returns True if the lhs and rhs are equal. One example where they differ is when you compare numbers in different representation formats.
In: 5.==5
Out: True
In: 5.===5
Out: False
Although they are the same numerically, (which is why == returns True), they aren't exactly identical.
FYI, they are different functions internally. == is Equal, whereas === is SameQ.
Equal refers to semantic equality whereas SameQ is syntactic equality. For instance, Sin[x]^2+Cos[x]^2 and 1 are the same number, so they are equal semantically. Since this can not be determined without more transformations, Equal returns unevaluated. However, actual expressions are different, so SameQ gives False.
Sin[x]^2 + Cos[x]^2 == 1
Sin[x]^2 + Cos[x]^2 === 1
Simplify[Sin[x]^2 + Cos[x]^2 == 1]
Note that there's special handling of Real numbers, SameQ[a,b] can return True if a and b differ in the last binary digit. To do more restrictive identity testing, use Order[a,b]==0
a = 1. + 2^-52;
b = 1.;
a === b
Order[a, b]==0
SameQ can return True for expressions that are syntactically different because expression heads may sort arguments automatically. You can prevent automatic sorting by using holding attributes. For instance
c + d === d + c
SetAttributes[SameQ, HoldAll]
c + d === d + c
lhs===rhs yields True if the
expression lhs is identical to rhs,
and yields False otherwise.
and
lhs==rhs returns True if lhs and rhs
are identical.
Reference from here and here.
I direct you to section 2.5: Equality checks of an excellent book by Leonid Shifrin.

all values same sign validation

User should insert all the values either positive or negative.
How may i set same sign validation ?
Right i have written this on before_save ..
unless (self.alt_1 >= 0 && self.alt_2 >=0 && self.alt_3 >= 0 &&
self.alt_4 >= 0 && self.alt_5 >= 0 && self.alt_6 >= 0) ||
(self.alt_1 <= 0 && self.alt_2 <=0 && self.alt_3 <= 0 &&
self.alt_4 <= 0 && self.alt_5 <= 0 && self.alt_6 <= 0)
self.errors.add_to_base(_("All values sign should be same."))
end
first_sign = self.alt_1 <=> 0
(2..6).each do |n|
unless (self.send("alt_#{n}") <=> 0) == first_sign
errors.add_to_base(_("All values' signs should be same."))
break
end
end
With this method we first get the sign of alt_1, and then see if the signs of the rest of the elements (alt_2 through alt_6) match. As soon as we find one that doesn't match we add the validation error and stop. It will run a maximum of 6 iterations and a minimum of 2.
Another more clever, but less efficient method, is to use the handy method Enumerable#all?, which returns true if the block passed to it returns true for all elements:
range = 1..6
errors.add_to_base(_("All values' signs should be same.")) unless
range.all? {|n| self.send("alt_#{n}") >= 0 } ||
range.all? {|n| self.send("alt_#{n}") <= 0 }
Here we first check if all of the elements are greater than 0 and then if all of the elements are less than 0. This method iterates a maximum of 12 times and a minimum of 6.
Here's a slightly different approach for you:
irb(main):020:0> def all_same_sign?(ary)
irb(main):021:1> ary.map { |x| x <=> 0 }.each_cons(2).all? { |x| x[0] == x[1] }
irb(main):022:1> end
=> nil
irb(main):023:0> all_same_sign? [1,2,3]
=> true
irb(main):024:0> all_same_sign? [1,2,0]
=> false
irb(main):025:0> all_same_sign? [-1, -5]
=> true
We use the spaceship operator to obtain the sign of each number, and we make sure that each element has the same sign as the element following it. You could also rewrite it to be more lazy by doing
ary.each_cons(2).all? { |x| (x[0] <=> 0) == (x[1] <=> 0) }
but that's less readable in my opinion.
unless
[:<=, :>=].any? do |check|
# Check either <= or >= for all values
[self.alt1, self.alt2, self.alt3, self.alt4, self.alt5, self.alt6].all? do |v|
v.send(check, 0)
end
end
self.errors.add_to_base(_("All values sign should be same."))
end

Resources