For instance:
> TRUE * 0.5
0.5
> FALSE * 0.5
0
I don't know if the secret here is the * character itself or the way R encodes logical statements, but I can't understand why the results.
R has a fairly loose type system and rather freely does coercion, hopefully when it is sensible. When coerced to numeric by *, logical values become 0 (FALSE) and 1 (TRUE), your expression gets evaluated with the usual mathematical convention of all values times 0 equal 0, and all values times 1 equal the value. The one exception to that rule in the numeric domain is Inf * 0 returns NaN. Character values have no "destination"-type when composed with "*", so "1"*TRUE throws an error.
Related
I am using the Symbolics.jl package and trying to invert a matrix. However, the output is giving 'true' instead of 1. Here is the code:
using Symbolics
#variables x
mat=[x 0 0
0 x 0
0 0 x]
And the result I get is
inv(mat)= [true / x 0 0
0 true / x 0
0 0 true / x]
Any thought on why this is happening?
First of, this is not wrong as inv(mat) * mat will still give you the identity matrix. the problem is that the identity matrix in Num (the data type representing Symbolic.jl's variable is the diagonal matrix with true in its diagonal. This can be checked by looking at
Matrix{Num}(I, 3, 3).
The inverse is calculated by solving the system AX = I and the true is created when the dense identity matrix is created.
This is due to the definition of I, namely const I = UniformScaling(true). My guess (and correct me if i'm wrong) is that this is for maximum compatibility. Normally this gets translated to a 1 in integer types but Num is an exception.
This question already has answers here:
What is the point of allowing a zero index when subsetting? [closed]
(2 answers)
Closed 1 year ago.
Why does the matrix type in R allow for indexing with zero if indexing starts from 1 ?
> m = diag(10)
> dim(m[0,0])
[1] 0 0
Is this a bug in the language implementation or a feature ?
It's certainly intentional. From the relevant section of the R Language Definition:
All elements of i [an integer vector of indices] must have the same sign. If they are positive, the elements of x with those index numbers are selected. If i contains negative elements, all elements except those indicated are selected.
...
A special case [of using integers to index a vector] is the zero index, which has null effects: x[0] is an empty vector and otherwise including zeros among positive or negative indices has the same effect as if they were omitted.
You can argue about whether this is a good idea or not (should including a zero in an integer vector of indices throw an error? Return NA? Something else?), but that will rapidly descend into opinion-based territory.
In R the following code produces a result of Inf (infinity).
7/0
[1] Inf
In mathematics the result of division by 0 is undefined.
R conforms to IEE754
Even an integer like 7 is a double in R
> typeof(7)
[1] "double"
Under IEE754, the result of division by 0 is Infinity
Division by zero: an operation on finite operands gives an exact infinite result, e.g., 1/0 or log(0). By default, returns ±infinity.
I noticed that if I pass any number except 0 as an argument to an if statement, the code inside the if statement compiles. I am confused why this is happening! I understand that R internally recognizes 0 as FALSE and the statement inside the if condition is not evaluated, which makes sense, but why is it getting evaluated for other numbers?
if(5) {
5 * 5
}
I had expected that I will get an error, but the code compiles and I get 25 as an answer.
From the ifhelp page, if expects:
A length-one logical vector ... Other types are coerced to logical if possible, ignoring any class
So basically it does
as.logical(5)
# [1] TRUE
and since it's 5 != 0, that's TRUE
as.logical(0)
# [1] FALSE
it's pretty common in languages for any number that's not 0 to be interpreted as "true"
I am having some problems in my code because of the following behaviour of R:
> F * F
[1] 0
> !T * !T
[1] TRUE
Can somebody explain me how to avoid this?
I need to multiply some values and get the result of a logical multiplication:
!first * !second * !third
And I would like to get a "True" only if all the values are true.
This is a matter of precedence of the operators. The negation ! has a lower precedence than the multiplication.
A list, in descending order of the operator precedence, is given here. This list can also be displayed with ?Syntax.
If the negations of TRUE are put into braces, one obtains a result that is more intuitively clear:
(!TRUE) * (!TRUE)
[1] 0
In this case, each instance of (!TRUE) is coerced into 0 by the multiplication, and the result is obviously 0.
However, the operation !TRUE * !TRUE is equal to !(TRUE * !TRUE) which in turn is equal to !0, and, hence (with the negation coercing the zero into a logical FALSE), the result is equal to TRUE.
To summarize, paraphrasing the title of your question, one could state:
NOT(TRUE * NOT(TRUE)) equals to TRUE.