Maxima -- Reduction of Logical expressions - logical-operators

Consider the following sequences of maxima's instructions:
(in1) x=0;
(out) x=0
(in2) y>0;
(out) y>0
(in3) x>0 and y>0;
(out) x>0 and y>0
(in4) x=0 or y>0;
(out) y>0
(in5) x=0 and y>0;
(out) false
The first three instructions produce expected outputs.
however, why does the logical proposition "x=0" in the inputs (in4) and (in5) are considered to be equal to false ?
We can see that when the expression "x=0" is isolated, it doesn't give a specific value (here false) to it, I don't understand these outputs conceptually and mathematically, it is not the mathematical behavior expected..
Here are some strange behavior when I try to debug.
(in1) prederror:true$
(in2) debugmode(true)$
(in3) x=0;
(out) x=0
(in4) y>0;
(out) y>0
(in5) x=0 or y>0;
(out) Unable to evaluate predicate y > 0
What does it mean ?

What happens here?
x > 0 and y > 0;
Arguments [x > 0] and [y > 0] are evaluated to themself. [[x > 0]
and [y > 0]] is also evaluated to itself.
x = 0 or y > 0;
[x = 0] is evaluated to false and [y > 0] is evaluated to itself, [false
or [y > 0]] is evaluated to [y > 0].
x = 0 and y > 0;
[x = 0] is evaluated to false and [false and [y > 0]] is immediately
evaluted to false without evaluting a second argument.
Relevent sections of documentation are
evalution
and
=
equal

Related

Prime or not using oz language

I am trying to make a function to check whether the value is a prime number or not using tail recursive function, so please can someone help me with this
`
declare
fun {PrimeR X D}
if X==2 orelse X==3 then 1
elseif X<2 andthen ((X mod D)==0) andthen D =< X then 0
elseif ((X mod D)\=0) then 1
else {PrimeR X D+1}
end
end
{Browse {PrimeR 6 1}}
`
Something along those lines if you want to use an iterative function
declare
fun {IsPrime X}
fun {PrimeItter X N}
case (X mod N) of 0 then X==N
else {PrimeItter X N+1} end
end
in
{PrimeItter X 2}
end
{Browse {IsPrime 31}}

How do I raise a float to an exponent in OCaml?

I am trying to write a function that takes x and raises it to the power of n.
This code works if x and n are integers:
let rec pow x n =
if n == 0 then 1 else
if (n mod 2 = 0) then pow x (n/2) * pow x (n/2) else
x * pow x (n/2) * pow x (n/2);;
If I try to change the code to work if x is a float, it falls apart:
let rec float_pow x n =
if n == 0.0 then 1.0 else
if n mod_float 2.0 == 0.0 then float_pow x (n /. 2) *. float_pow x (n /. 2) else
x *. float_pow x (n /. 2) *. float_pow x (n /. 2);;
I get this error:
Error: This expression has type float
This is not a function; it cannot be applied.
What do I do?
The key problem, I think, is that mod is a keyword in OCaml, and is treated as an infix operator. But mod_float is just an ordinary function. You need to use it in prefix form.
So x mod n should be translated to mod_float x n.
You have another problem, which is that you're using the special-purpose == operator for equality comparison. You want to use = for equality comparisons in OCaml unless you need a "physical" comparison (which is not what you want here).
This isn't just stylistic--it really makes a difference. Note the following results:
# 0.0 == 0.0;;
- : bool = false
# 0.0 = 0.0;;
- : bool = true

How can I use universal and existential quantification in julia?

I want to code domination definition in Julia. x dom y. x , y are 2 vectors.
b=all(x<=y) && any(x<y)
would you please help me. How can I code this concept in Julia?
Thank you
The simplest approach can be almost like you have specified it:
dom(x, y) = all(x .<= y) && any(x .< y)
You could also use a loop e.g. like this:
function dom(x::AbstractVector, y::AbstractVector)
#assert length(x) == length(y)
wasless = false
for (xi, yi) in zip(x, y)
if xi < yi
wasless = true
elseif xi > yi
return false
end
end
return wasless
end

Calculating the modulo of two intervals

I want to understand how the modulus operator works when applied to two intervals. Adding, subtracting and multiplying two intervals is trivial to implement in code, but how do you do it for modulus?
I'd be happy if someone can show me the formula, sample code or a link which explains how it works.
Background info: You have two integers x_lo < x < x_hi and y_lo < y < y_hi. What is the the lower and upper bound for mod(x, y)?
Edit: I'm unsure if it is possible to come up with the minimal bounds in an efficient manner (without calculating the mod for all x or for all y). If so, then I'll accept an accurate but non-optimal answer for the bounds. Obviously, [-inf,+inf] is a correct answer then :) but I want a bound that is more limited in size.
It turns out, this is an interesting problem. The assumption I make is that for integer intervals, modulo is defined with respect to truncated division (round towards 0).
As a consequence, mod(-a,m) == -mod(a,m) for all a, m. Moreover, sign(mod(a,m)) == sign(a).
Definitions, before we start
Closed interval from a to b: [a,b]
Empty interval: [] := [+Inf,-Inf]
Negation: -[a,b] := [-b,-a]
Union: [a,b] u [c,d] := [min(a,c),max(b,d)]
Absolute value: |m| := max(m,-m)
Simpler Case: Fixed modulus m
It is easier to start with a fixed m. We will later generalize this to the modulo of two intervals. The definition builds up recursively. It should be no problem to implement this in your favorite programming language. Pseudocode:
def mod1([a,b], m):
// (1): empty interval
if a > b || m == 0:
return []
// (2): compute modulo with positive interval and negate
else if b < 0:
return -mod1([-b,-a], m)
// (3): split into negative and non-negative interval, compute and join
else if a < 0:
return mod1([a,-1], m) u mod1([0,b], m)
// (4): there is no k > 0 such that a < k*m <= b
else if b-a < |m| && a % m <= b % m:
return [a % m, b % m]
// (5): we can't do better than that
else
return [0,|m|-1]
Up to this point, we can't do better than that. The resulting interval in (5) might be an over-approximation, but it is the best we can get. If we were allowed to return a set of intervals, we could be more precise.
General case
The same ideas apply to the case where our modulus is an interval itself. Here we go:
def mod2([a,b], [m,n]):
// (1): empty interval
if a > b || m > n:
return []
// (2): compute modulo with positive interval and negate
else if b < 0:
return -mod2([-b,-a], [m,n])
// (3): split into negative and non-negative interval, compute, and join
else if a < 0:
return mod2([a,-1], [m,n]) u mod2([0,b], [m,n])
// (4): use the simpler function from before
else if m == n:
return mod1([a,b], m)
// (5): use only non-negative m and n
else if n <= 0:
return mod2([a,b], [-n,-m])
// (6): similar to (5), make modulus non-negative
else if m <= 0:
return mod2([a,b], [1, max(-m,n)])
// (7): compare to (4) in mod1, check b-a < |modulus|
else if b-a >= n:
return [0,n-1]
// (8): similar to (7), split interval, compute, and join
else if b-a >= m:
return [0, b-a-1] u mod2([a,b], [b-a+1,n])
// (9): modulo has no effect
else if m > b:
return [a,b]
// (10): there is some overlapping of [a,b] and [n,m]
else if n > b:
return [0,b]
// (11): either compute all possibilities and join, or be imprecise
else:
return [0,n-1] // imprecise
Have fun! :)
Let see mod(x, y) = mod.
In general 0 <= mod <= y. So it's always true: y_lo < mod < y_hi
But we can see some specific cases below:
- if: x_hi < y_lo then div(x, y) = 0, then x_low < mod < x_hi
- if: x_low > y_hi then div(x, y) > 0, then y_low < mod < y_hi
- if: x_low < y_low < y_hi < x_hi, then y_low < mod < y_hi
- if: x_low < y_low < x_hi < y_hi, then y_low < mod < x_hi
- if: y_low < x_low < y_hi < x_hi, then y_low < mod < y_hi
....

How can I show that a function is always not commutative

I have the following vexing problem.
I have implemented the following function:
function bool less(nat x, nat y) {
if (y<>0) then
if (x<>0) then
return less(x-1,y-1);
else
return true;
end;
else
return false;
end;
end;
How can I show that for all x,y the following
less(x,y) and less(y,x) are not possible at the
same time?
Bye
Well, first of all I would ask you to consider the case of less(-1, -2), so we will have to define the function to be on the bounds of n ≥ 0. Also when the first input is equal to the second it will return true for both orderings, so we will have to assume that x ≠ y.
I would use Proof by Contradiction.
Assume that for some x and some y where x and y are both ≥ 0 and x≠y, that less(x,y) and less(y,x) are both true.
This would imply that while x and y are both nonzero, you subtract one from them n times until one of them is zero, checking x first. The function returns true when the first operand reaches zero, false when the second operand reaches zero while the first is nonzero.
There are two cases for this:
x reaches zero first. In this case n = x, because 0 = x - n(1).
y reaches zero first. In this case n = y, because 0 = y - n(1).
By our assumption, less(x,y) returned true, meaning that the function iterated x times, after which x - x(1) = 0 and y - x(1) > 0 (because y ≠ x, and the function didn't return false before hand).
Similarly, less(y,x) returned true, meaning that the function iterated y times, after which y- y(1) = 0 and x - y(1) > 0 (same reasons as before).
This gives us two useful relations: y - x > 0 and x - y > 0. rearranged: y > x and x > y (the semantic meaning of the function, but we have achieved this from the definition of how the function works and we have reduced it to the pure mathematics which we can work with certain axioms for).
From y > x and x > y, you can rearrange as x > x and y > y (If x is greater than y, then it is greater than all things y is greater than. y is greater than x, therefore x is greater than x).
This is a logical contradiction, and therefore our assumption (that they were both true) is incorrect.
Therefore, by Proof by Contradiction, when x ≠ y, and x,y ≥ 0, the function less cannot return true for both less(x,y) and less(y,x).
(it's been a while since I had to do a proof (though I'm going to have to do some coming up so it is good practice) so I might be a bit rusty. If any one sees an error, please point it out and I will try to fix it)

Resources