This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I am not a specialist in C/C++.
I found this declaration today:
typedef NS_OPTIONS(NSUInteger, PKRevealControllerType)
{
PKRevealControllerTypeNone = 0,
PKRevealControllerTypeLeft = 1 << 0,
PKRevealControllerTypeRight = 1 << 1,
PKRevealControllerTypeBoth = (PKRevealControllerTypeLeft | PKRevealControllerTypeRight)
};
Can you guys translate what values every value will have?
opertor << is bitwise left shift operator. Shift all the bits to left a specified number of times: (arithmetic left shift and reserves sign bit)
m << n
Shift all the bits of m to left a n number of times. (notice one shift == multiply by two).
1 << 0 means no shift so its equals to 1 only.
1 << 1 means one shift so its equals to 1*2 = 2 only.
I explain with one byte: one in one byte is like:
MSB
+----+----+----+---+---+---+---+---+
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1
+----+----+----+---+---+---+---+---+
7 6 5 4 3 2 1 / 0
| / 1 << 1
| |
▼ ▼
+----+----+----+---+---+---+---+---+
| 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 2
+----+----+----+---+---+---+---+---+
7 6 5 4 3 2 1 0
Whereas 1 << 0 do nothing but its like figure one. (notice 7th bit is copied to preserve sign)
OR operator: do bit wise or
MSB PKRevealControllerTypeLeft
+----+----+----+---+---+---+---+---+
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | == 1
+----+----+----+---+---+---+---+---+
7 6 5 4 3 2 1 0
| | | | | | | | OR
MSB PKRevealControllerTypeRight
+----+----+----+---+---+---+---+---+
| 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | == 2
+----+----+----+---+---+---+---+---+
7 6 5 4 3 2 1 0
=
MSB PKRevealControllerTypeBoth
+----+----+----+---+---+---+---+---+
| 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | == 3
+----+----+----+---+---+---+---+---+
7 6 5 4 3 2 1 0
| is bit wise operator. in below code it or 1 | 2 == 3
PKRevealControllerTypeNone = 0, // is Zero
PKRevealControllerTypeLeft = 1 << 0, // one
PKRevealControllerTypeRight = 1 << 1, // two
PKRevealControllerTypeBoth = (PKRevealControllerTypeLeft |
PKRevealControllerTypeRight) // three
There is not more technical reason to initialized values like this, defining like that makes things line up nicely read this answer:define SOMETHING (1 << 0)
compiler optimization convert them in simpler for like: (I am not sure for third one, but i think compiler will optimize that too)
PKRevealControllerTypeNone = 0, // is Zero
PKRevealControllerTypeLeft = 1, // one
PKRevealControllerTypeRight = 2, // two
PKRevealControllerTypeBoth = 3, // Three
Edit: #thanks to Till.
read this answer App States with BOOL flags show the usefulness of declarations you got using bit wise operators.
It's an enum of bit flags:
PKRevealControllerTypeNone = 0 // no flags set
PKRevealControllerTypeLeft = 1 << 0, // bit 0 set
PKRevealControllerTypeRight = 1 << 1, // bit 1 set
And then
PKRevealControllerTypeBoth =
(PKRevealControllerTypeLeft | PKRevealControllerTypeRight)
is just the result of bitwise OR-ing the other two flags. So, bit 0 and bit 1 set.
The << operator is the left shift operator. And the | operator is bitwise OR.
In summary the resulting values are:
PKRevealControllerTypeNone = 0
PKRevealControllerTypeLeft = 1
PKRevealControllerTypeRight = 2
PKRevealControllerTypeBoth = 3
But it makes a lot more sense to think about it in terms of flags of bits. Or as a set where the universal set is: { PKRevealControllerTypeLeft, PKRevealControllerTypeRight }
To learn more you need to read up about enums, shift operators and bitwise operators.
This looks like Objective C and not C++, but regardless:
1 << 0
is just one bitshifted left (up) by 0 positions. Any integer "<<0" is just itself.
So
1 << 0 = 1
Similarly
1 << 1
is just one bitshifted left by 1 position. Which you could visualize a number of ways but the easiest is to multiply by 2.[Note 1]
So
x << 1 == x*2
or
1 << 1 == 2
Lastly the single pipe operator is a bitwise or.
So
1 | 2 = 3
tl;dr:
PKRevealControllerTypeNone = 0
PKRevealControllerTypeLeft = 1
PKRevealControllerTypeRight = 2
PKRevealControllerTypeBoth = 3
[1] There are some limitations on this generalization, for example when x is equal to or greater than 1/2 the largest value capable of being stored by the datatype.
This all comes down to bitwise arithmetic.
PKRevealControllerTypeNone has a value of 0 (binary 0000)
PKRevealControllerTypeLeft has a value of 1 (binary 0001)
PKRevealControllerTypeRight has a value of 2 (binary 0010) since 0001 shifted left 1 bit is 0010
PKRevealControllerTypeBoth has a value of 3 (binary 0011) since 0010 | 0001 (or works like addition) = 0011
In context, this is most-likely used to determine a value. The property is & (or bitwise-and) works similar to multiplication. If 1 ands with a number, then the number is preserved, if 0 ands with a number, then the number is cleared.
Thus, if you want to check if a particular controller is specifically type Left and it has a value of 0010 (i.e. type Right) 0010 & 0001 = 0 which is false as we expect (thus, you have determined it is not of correct type). However, if the controller is Both 0011 & 0001 = 1 so the result is true which is correct since we determined this is of Both types.
Related
I can't figure out how to attach the .csv or workbook. Can someone explain?
I feel like I am overcomplicating this. I have a simple line chart, comparing 2 SUM values, in 3 measures, across an assigned day number. I've attached a .csv with sample data & tableau wbk. If I need to provide anything else, please let me know.
The viz also needs to by dynamic, so they can toggle between months past using a parameter [Selected Month] to toggle between months; in this example September is selected.
The [Contributing Day#] number is a field in the data provided by the user, not a calculation. I cannot use the workday feature, because weekend dates and holidays are sometimes considered a [Contributing Day#].
A date is matched to a Contributing_Day#. I can't compare date to date, they want to see it across Contributing_Day# instead.
I need to aggregate daily for the smaller number of days; in this example I would want to exclude Contributing_Day# 6 & 7 from the viz since one month only went to 5.
It needs to be MTD minus today, meaning if only Contributing_Day# <= 4 has passed, all 3 measures would null after Contributing_Day# 4
And if that wasn't enough, I need to exclude a dimension [Role] = 'Role' string type as well.
Measure_1 = SUM(Value_Sum) for [Selected Month]
Measure_2 = SUM(Value_Sum) for [Selected Month] - 1
Measure_3 = SUM(Value_Goal) for [Selected Month]
September Date | [Contributing_Day#] | Measure_1 | Measure_3
9/1/22 | 1 | 105 | 96
9/2/22 | 2 | 112 | 92
9/6/22 | 3 | 129 | 99
9/7/22 | 4 | 108 | 100
9/8/22 | 5 | 108 | 98
August Date | [Contributing_Day#] | Measure_2
8/1/22 | 1 | 105
8/2/22 | 2 | 112
8/3/22 | 3 | 129
8/4/22 | 4 | 108
8/5/22 | 5 | 108
8/8/22 | 6 | 112
8/9/22 | 7 | 92
Here is what I have tried that was almost working but not, and a screen shot of my visual when I have September selected and October.
Measure_1
{Fixed [Date],[Role] :SUM(IF MONTH([Date]) = MONTH(TODAY())AND DAY ([Date]) < DAY(TODAY())AND
[Role] <> 'Role' THEN [Value_1] END) }
Measure_2
{Fixed [Date],[Role] :
SUM(
IF ISNULL([Measure_1]) THEN NULL
ELSEIF MONTH([Date]) = MONTH(TODAY())-1
AND DAY ([Date]) < DAY(TODAY())
AND [Role] <> 'Role'
THEN [Value_1] END) }
Measure_3
{Fixed [Date],[Role] :
SUM(
IF MONTH([Date]) = MONTH(TODAY())
AND DAY ([Date]) < DAY(TODAY())
AND [Role] <> 'Role'
THEN [Value_2] END) }
Other calculations I've tried...
SUM(
{ FIXED [Date] :
MAX(IF DATEPART('year',[Date]) = DATEPART('year',[Select Month])
AND DATEPART('month',[Date]) <= DATEPART('month',[Select Month])
AND [Contributing_Day#] = 1
THEN 1 ELSE NULL END)
})
COUNTD(IF DATEPART('year',[Date]) = DATEPART('year',[Select Month])
AND [Contributing_Day#] = 1
THEN [Date] ELSE NULL END)
[% of Month Complete]
would be count of [Contributing_Day#] past / total count of [Contributing_Day#]
But I haven't been able to get that to work
[Role Flag]
{Fixed [Date],[Role] :
SUM(IF [Role] <> 'Role' THEN 0 ELSE 1 END) }
Measure_1
{Fixed [Date],[Role] : IF [% of Month Complete] = 1 THEN
SUM([Value_1]) ELSEIF MAX(MONTH([Date])) = MAX(MONTH([Select Month]))
AND MAX(DAY([Date])) < MAX(DAY([Select Month])) AND MAX([Role Flag])
<> 1 THEN SUM([Value_1]) END }
Measure_2
{Fixed [Date],[Role] :
IF MAX([Measure_1]) = 0 THEN NULL
ELSEIF [% of Month Complete] = 1
AND MAX(MONTH([Date])) = MAX(MONTH([Select Month]))-1
AND MAX([Role Flag]) <> 1
THEN SUM([Value_1])
ELSEIF MAX(MONTH([Date])) = MAX(MONTH([Select Month]))-1
AND MAX(DAY([Date])) < MAX(DAY([Select Month]))
AND MAX([Role Flag]) <> 1
THEN SUM([Value_1]) END }
This is a ramp least squares estimation problem, better described in math formula elsewhere:
https://scicomp.stackexchange.com/questions/33524/ramp-least-squares-estimation
I used Disciplined Convex-Concave Programming and DCCP package based on CVXPY. The code follows:
import cvxpy as cp
import numpy as np
import dccp
from dccp.problem import is_dccp
# Generate data.
m = 20
n = 15
np.random.seed(1)
X = np.random.randn(m, n)
Y = np.random.randn(m)
# Define and solve the DCCP problem.
def loss_fn(X, Y, beta):
return cp.norm2(cp.matmul(X, beta) - Y)**2
def obj_g(X, Y, beta, sval):
return cp.pos(loss_fn(X, Y, beta) - sval)
beta = cp.Variable(n)
s = 10000000000000
constr = obj_g(X, Y, beta, s)
t = cp.Variable(1)
t.value = [1]
cost = loss_fn(X, Y, beta) - t
problem = cp.Problem(cp.Minimize(cost), [constr >= t])
print("problem is DCP:", problem.is_dcp()) # false
print("problem is DCCP:", is_dccp(problem)) # true
problem.solve(verbose=True, solver=cp.ECOS, method='dccp')
# Print result.
print("\nThe optimal value is", problem.value)
print("The optimal beta is")
print(beta.value)
print("The norm of the residual is ", cp.norm(X*beta - Y, p=2).value)
Because of the large value s, I would hope to get a solution similar to the least squares estimation. But there is no solution as the output shows (with different solver, dimension of the problem, etc):
problem is DCP: False
problem is DCCP: True
ECOS 2.0.7 - (C) embotech GmbH, Zurich Switzerland, 2012-15. Web: www.embotech.com/ECOS
It pcost dcost gap pres dres k/t mu step sigma IR | BT
0 +0.000e+00 -0.000e+00 +2e+01 9e-02 1e-04 1e+00 9e+00 --- --- 1 1 - | - -
1 -7.422e-04 +2.695e-09 +2e-01 1e-03 1e-06 1e-02 9e-02 0.9890 1e-04 2 1 1 | 0 0
2 -1.638e-05 +5.963e-11 +2e-03 1e-05 2e-08 1e-04 1e-03 0.9890 1e-04 2 1 1 | 0 0
3 -2.711e-07 +9.888e-13 +2e-05 1e-07 2e-10 2e-06 1e-05 0.9890 1e-04 4 1 1 | 0 0
4 -3.991e-09 +1.379e-14 +2e-07 1e-09 2e-12 2e-08 1e-07 0.9890 1e-04 1 0 0 | 0 0
5 -5.507e-11 +1.872e-16 +3e-09 2e-11 2e-14 2e-10 1e-09 0.9890 1e-04 1 0 0 | 0 0
OPTIMAL (within feastol=1.6e-11, reltol=4.8e+01, abstol=2.6e-09).
Runtime: 0.001112 seconds.
ECOS 2.0.7 - (C) embotech GmbH, Zurich Switzerland, 2012-15. Web: www.embotech.com/ECOS
It pcost dcost gap pres dres k/t mu step sigma IR | BT
0 +0.000e+00 -5.811e-01 +1e+01 6e-01 6e-01 1e+00 2e+00 --- --- 1 1 - | - -
1 -7.758e+00 -2.575e+00 +1e+00 2e-01 7e-01 6e+00 3e-01 0.9890 1e-01 1 1 1 | 0 0
2 -3.104e+02 -9.419e+01 +4e-02 2e-01 8e-01 2e+02 8e-03 0.9725 8e-04 2 1 1 | 0 0
3 -2.409e+03 -9.556e+02 +5e-03 2e-01 8e-01 1e+03 1e-03 0.8968 5e-02 3 2 2 | 0 0
4 -1.103e+04 -5.209e+03 +2e-03 2e-01 7e-01 6e+03 4e-04 0.9347 3e-01 2 2 2 | 0 0
5 -1.268e+04 -1.592e+03 +8e-04 1e-01 1e+00 1e+04 2e-04 0.7916 4e-01 3 2 2 | 0 0
6 -1.236e+05 -2.099e+04 +9e-05 1e-01 1e+00 1e+05 2e-05 0.8979 9e-03 1 1 1 | 0 0
7 -4.261e+05 -1.850e+05 +4e-05 2e-01 7e-01 2e+05 1e-05 0.7182 3e-01 2 1 1 | 0 0
8 -2.492e+07 -1.078e+07 +7e-07 1e-01 7e-01 1e+07 2e-07 0.9838 1e-04 3 2 2 | 0 0
9 -2.226e+08 -9.836e+07 +5e-08 9e-02 5e-01 1e+08 1e-08 0.9339 2e-03 2 3 2 | 0 0
UNBOUNDED (within feastol=1.0e-09).
Runtime: 0.001949 seconds.
The optimal value is None
The optimal beta is
None
The norm of the residual is None
I used random forest algorithm and got this result
=== Summary ===
Correctly Classified Instances 10547 97.0464 %
Incorrectly Classified Instances 321 2.9536 %
Kappa statistic 0.9642
Mean absolute error 0.0333
Root mean squared error 0.0952
Relative absolute error 18.1436 %
Root relative squared error 31.4285 %
Total Number of Instances 10868
=== Confusion Matrix ===
a b c d e f g h i <-- classified as
1518 1 3 1 0 14 0 0 4 | a = a
3 2446 0 0 0 1 1 27 0 | b = b
0 0 2942 0 0 0 0 0 0 | c = c
0 0 0 470 0 1 1 2 1 | d = d
9 0 0 9 2 19 0 3 0 | e = e
23 1 2 19 0 677 1 22 6 | f = f
4 0 2 0 0 13 379 0 0 | g = g
63 2 6 17 0 15 0 1122 3 | h = h
9 0 0 0 0 9 0 4 991 | i = i
I wonder how Weka evaluate errors(mean absolute error, root mean squared error, ...) using non numerical values('a', 'b', ...).
I mapped each classes to numbers from 0 to 8 and evaluated errors manually, but the evaluation was different from Weka.
How to reimplemen the evaluating steps of Weka?
I need transform view from origin(250, 250) to origin(352, 315), and width/height change from (100.0, 100.0) to (68, 68).
I know I can combine several CGAffineTransform function together, such as scale, rotate, translate.
But i don't know how to count the order of those transformations, and the exact parameter of them.
I have try several time, but can't move the view to correct position.
Anyone can help?
A little understanding about what is happening behind the scenes is always nice in these matrix transformations.
Apple docs has a great documentation about transforms, so let's use it.
A translation matrix looks like :
| 1 0 0 |
| 0 1 0 |
| tx ty 1 |
where (tx, ty) is your translation vector.
A scaling matrix looks like :
| sx 0 0 |
| 0 sy 0 |
| 0 0 1 |
where sxand sy are the scale factor in the X and Y axis.
You want to concatenate these matrix using CGAffineTransformConcat, but as according to its doc :
Note that matrix operations are not commutative—the order in which you
concatenate matrices is important. That is, the result of multiplying
matrix t1 by matrix t2 does not necessarily equal the result of
multiplying matrix t2 by matrix t1.
You have to translate your view before scaling it, otherwise your translation vector will be scaled according to sx and sy coefficients.
Let's show it easily :
let scaleMatrix = CGAffineTransformMakeScale(0.68, 0.68)
let translateMatrix = CGAffineTransformMakeTranslation(102, 65)
let translateThenScaleMatrix = CGAffineTransformConcat(scaleMatrix, translateMatrix)
NSLog("translateThenScaleMatrix : \(translateThenScaleMatrix)")
// outputs : CGAffineTransform(a: 0.68, b: 0.0, c: 0.0, d: 0.68, tx: 102.0, ty: 65.0)
// the translation is the same
let scaleThenTranslateMatrix = CGAffineTransformConcat(translateMatrix, scaleMatrix)
NSLog("scaleThenTranslateMatrix : \(scaleThenTranslateMatrix)")
// outputs : CGAffineTransform(a: 0.68, b: 0.0, c: 0.0, d: 0.68, tx: 69.36, ty: 44.2)
// the translation has been scaled too
And let's prove it mathematically. Please note that when you perform an operation A then an operation B, the related matrix is computed by doing matB*matA, the first operation is on the right. Since multiplication is not commutative for matrix, it's important.
// Translate then scaling :
| sx 0 0 | | 1 0 0 | | sx 0 0 |
| 0 sy 0 | . | 0 1 0 | = | 0 sy 0 |
| 0 0 1 | | tx ty 1 | | tx ty 1 |
// The resulting matrix has the same value for translation
// Scaling then translation :
| 1 0 0 | | sx 0 0 | | sx 0 0 |
| 0 1 0 | . | 0 sy 0 | = | 0 sy 0 |
| tx ty 1 | | 0 0 1 | | sx.tx sy.ty 1 |
// The translation values are affected by scaling coefficient
struct CGAffineTransform {
CGFloat a, b, c, d;
CGFloat tx, ty;
};
You can get parameters by this struct.And transforms always override,in another words,they won't superpose,pay attention to this.
This question already has answers here:
Multiple value enum in Obj-C
(2 answers)
Closed 9 years ago.
I was looking into how one uses the UISwipeGestureRecognizer class in Objective-C and happened to run across an enum named UISwipeGestureRecognizerDirection.
Apple defines this as:
typedef enum {
UISwipeGestureRecognizerDirectionRight = 1 << 0,
UISwipeGestureRecognizerDirectionLeft = 1 << 1,
UISwipeGestureRecognizerDirectionUp = 1 << 2,
UISwipeGestureRecognizerDirectionDown = 1 << 3
}
I wasn't sure how the compiler interpreted the above, specifically the << operator. From looking it up, it appears to be the bitwise shift left - But I am afraid I dont understand how the operator works above.
Any direction is greatly appreciated. Thanks in advance!
In a computer, integer numbers are represented as 1's and 0's.
So for a 1 bit computer, we can only use 1 or 0:
0 or 1 - 1 is the maximum number.
0 = 0
1 = 1
2-bit computer: - 3 is the maximum number. Remember you aren't
counting from 0 to 9. You can only count to 1 then you have to
increase the next column to the left.
00 = 0 + 0 = 0
01 = 0 + 1 = 1
10 = 2 + 0 = 2
11 = 2 + 1 = 3
3-bit computer: - 7 is the maximum number
000 = 0 + 0 + 0 = 0
001 = 0 + 0 + 1 = 1
010 = 0 + 2 + 0 = 2
011 = 0 + 2 + 1 = 3
100 = 4 + 0 + 0 = 4
101 = 4 + 0 + 1 = 5
110 = 4 + 2 + 0 = 6
111 = 4 + 2 + 1 = 7
All left bit-shifting (1<<3) means is place a 1 at the right most
column (the zero position) and move the specified number of places to the left.
001 - 1<<0 -- Move it 0 times - Value is 1
010 - 1<<0 -- Move it 1 times - Value is 2
100 - 1<<0 -- Move it 2 times - Value is 4
Here's a table to help you think about an 8-bit computer:
| 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| 128| 64| 32| 16| 8| 4| 2| 1|
| 2^7| 2^6 | 2^5| 2^4| 2^3| 2^2| 2^1| 2^0|
|1 << 7 | 1 << 6 | 1 << 5 | 1 << 4 | 1 << 3 | 1 << 2 | 1 << 1 | 1 << 0 |
It is a bitwise shift left. This is the equivalent of
typedef enum {
UISwipeGestureRecognizerDirectionRight = 1,
UISwipeGestureRecognizerDirectionLeft = 2,
UISwipeGestureRecognizerDirectionUp = 4,
UISwipeGestureRecognizerDirectionDown = 8
}
It's a convenience to allow bitwise testing. The actual value means nothing much except that they all can be packed into one variable - enum values can be anything at all as long as they are unique.