Invert vector position transformation - algorithm

Help me please to solve this problem:
I have a vector A and I get vector B this way:
B = M1 * A * 0.5 + M2 * A * 0.5;
M1 - rotation matrix 0 deg.
M2 - rotation Matrix 45 deg.
I need to get a way to compute A if B is known. For instance if B == (0.8535, 0.3535), then A should be (1.0, 0.0). How can I get the inverted formula?
UPD: for 0.4/0.6 the result formula is:
A=(M1*0.4+M2*0.6)^-1 * B

Bring this equation into a single matrix-vector product
B = M1 * A * 0.5 + M2 * A * 0.5
B = (M1 * 0.5 + M2 * 0.5)*A
B = M*A
and invert M
A = inv(M)*B = M\B
For example
M1 = | 1 0 | M2 = | 1/√2 -1/√2 |
| 0 1 | | 1/√2 1/√2 |
makes
M = | √2/4+1/2 -√2/4 |
| √2/4 √2/4+1/2 |
and the inverse
inv(M) = | 1 √2-1 |
| 1-√2 1 |
you will find that
inv(M)*| 0.8535 | = | 0.999999 |
| 0.3535 | | -3e-5 |
The above process is part of linear algebra, exactly because you can use the associative & distributive properties with non-scalar quantities.

A = (M1 * 0.5 + M2 * 0.5)^-1 * B

Related

How to simplify material implication?

you know that the truth table for material implication is:
A | C | Y = A --> C
0 | 0 | 1
0 | 1 | 1
1 | 0 | 0
1 | 1 | 1
From this table we can deduce
A --> C = Y = ~A~C + ~AC + AC (where ~X stands for NOT X)
But it is also well known that
A --> C = ~(A~C)
I can't reduce the 1st expression (~A~C + ~AC + AC) to the 2nd ( ~(A~C) ), can you show me through which steps can you obtain the 2nd from the 1st?
Thank you.
(~A~C + ~AC + AC)
(~A~C + ~AC) + AC
~A(~C + C) + AC
~A(T) + AC
~A + AC
~~(~A + AC)
~((~~A)~(AC))
~(A~(AC))
~(A(~A + ~C))
~(A~A + A~C)
~(F + A~C)
~(A~C)

Print lower triangular packed matrix in Fortran 77

I have a matrix of real*8 numbers saved in an array as a packed lower triangular matrix:
|1 * * * * *|
|2 7 * * * *|
|3 8 12 * * *|
|4 9 13 16 * *| => [1,2,3,4,5,6,7,8,9...21]
|5 10 14 17 19 *|
|6 11 15 18 20 21|
I want to be able to print it in the following format:
[row|col] 1 2 3 4 5
1 1 * * * *
2 2 7 * * *
3 3 8 12 * *
4 4 9 13 16 *
5 5 10 14 17 19
6 6 11 15 18 20
[row|col] 6
1 *
2 *
3 *
4 *
5 *
6 6
The problem I have is that I don't know how to loop into the matrix elements without the need to creating extra arrays to save the elements by columns and then print them. this is what I have tried so far
Implicit Real*8 (A-H,O-Z)
INTEGER ARRAY(21)
10 Format(5X,'[Row|Col]',5(8X,I6))
Icol=6
Num1=1
C Test array
DO K=1,21
Array(K)=K*1.0d0
ENDO
C Print the elements row-by-row.
44 Num2=Icol-Num1
Num=Num2
If ((Num2).gt.5) Num=5
Write(*,10) (I,I=Num1,Num1+Num)
INum1=INum1+Num
if (Inum1.ne.Icol) goto 44
STOP
END
The key is to identify the formula designating the non-zero elements of the lower triangular matrix. If i and j are running indices for rows and columns respectively, then (j < i) elements will be in the lower triangular part. Here is the code:
PROGRAM print_low_tri_matrix
IMPLICIT NONE
INTEGER :: i,j,n,m,p
REAL ,ALLOCATABLE, DIMENSION(:,:) :: a
REAL, ALLOCATABLE, DIMENSION(:) :: r
n = 6
p = n*(n+1)/2 ! Number of non-zero elements in a lower triangular matrix of size n by n
ALLOCATE(a(n,n),r(p))
DO i = 1, p
r(i) = i*1.0 ! Array containing the non-zero elements
END DO
m = 1 ! Index tracking the non-zero elements array r
DO j = 1,n
DO i = 1,n
IF(j .LE. i) THEN ! Non-zero indices of the matrix
a(i,j) = r(m)
m = m + 1
ELSE
a(i,j) = 0.
END IF
END DO
END DO
! Printing the full matrix a - uncomment for checking
! DO i = 1,n
! WRITE(*,*) (a(i,j),j=1,n)
! END DO
! Printing the matrix a as needed
DO i = 1,n
DO j = 1,n
IF(j .LE. i) THEN
WRITE(*,'(F12.4)',ADVANCE="NO") a(i,j)
ELSE
WRITE(*,'(A12)',ADVANCE="NO") '*'
END IF
IF (j .EQ. n ) WRITE(*,*)
END DO
END DO
END PROGRAM print_low_tri_matrix
And the output will be:
1.0000 * * * * *
2.0000 7.0000 * * * *
3.0000 8.0000 12.0000 * * *
4.0000 9.0000 13.0000 16.0000 * *
5.0000 10.0000 14.0000 17.0000 19.0000 *
6.0000 11.0000 15.0000 18.0000 20.0000 21.0000
Of course, you can change the matrix into an integer array if you wanted.
P.S :- To make this work in f77, you can change the format specifiers as WRITE(*,'(F12.4)',ADVANCE="NO") a(i,j) to WRITE(*,'(F12.4,$)') a(i,j). The allocations of the arrays should be removed and defined directly.

Continous Parent and Discrete Child Conditional with Observed Data in PyMC

I am new to PyMC and trying to set up the simple conditional probability model: P(has_diabetes|bmi, race). Race can take on 5 discrete values encoded as 0-4 and BMI can take on a non-zero positive real number. So far I have the parent variables setup like this:
p_race = [0.009149232914923292,
0.15656903765690378,
0.019637377963737795,
0.013947001394700141,
0.800697350069735]
race = pymc.Categorical('race', p_race)
bmi_alpha = pymc.Exponential('bmi_alpha', 1)
bmi_beta = pymc.Exponential('bmi_beta', 1)
bmi = pymc.Gamma('bmi', bmi_alpha, bmi_beta, value=bmis, observed=True)
I have observed data that looks like:
| bmi | race | has_diabetes |
| 21.7 | 1 | 0 |
| 45.3 | 4 | 1 |
| 18.9 | 2 | 0 |
| 26.6 | 0 | 0 |
| 35.1 | 4 | 0 |
I am attempting to model has_diabetes as:
has_diabetes = pymc.Bernoulli('has_diabetes', p_diabetes, value=data, observed=True)
My problem is that I am not sure how to construct the p_diabetes function since it is dependent on the values of race and and the continuous value of bmi.
You need to construct a deterministic function that generates p_diabetes as a function of your predictors. The safest way to do this is via a logit-linear transformation. For example:
intercept = pymc.Normal('intercept', 0, 0.01, value=0)
beta_race = pymc.Normal('beta_race', 0, 0.01, value=np.zeros(4))
beta_bmi = pymc.Normal('beta_bmi', 0, 0.01, value=0)
#pymc.deterministic
def p_diabetes(b0=intercept, b1=beta_race, b2=beta_bmi):
# Prepend a zero for baseline
b1 = np.append(0, b1)
# Logit-linear model
return pymc.invlogit(b0 + b1[race] + b2*bmi)
I would make the baseline race be the largest group (it is assumed to be index 0 in this example).
Actually, its not clear what the first part of the model above is for, specifically, why you are building models for the predictors, but perhaps I am missing something.

Elastic Search, is it possible to get the index of a specific document in a search result set?

I know the id of a specific document I want to show in my result set, but I don't know which page of the results it will be on. Is it possible with elastic search to tell it to return the page that a specific document is on?
My guess is this is not possible. My current approach is to run the query once loading just the documents ids and return a very large (all) result set for the query. I find the id in this list, then I run the query again loading all of the data I want to show on the page. I'd prefer not to run the query twice if I can avoid it.
I am using JAVA API and I am getting the index, type, id and source as follows.
SearchResponse response = client.prepareSearch().execute().actionGet();
SearchHit[] documents = response.getHits().getHits();
for(SearchHit document : documents)
{
System.out.println("document index :: " + document.getIndex());
System.out.println("document type :: " + document.getType());
System.out.println("document id :: " + document.getId());
System.out.println("document source JSON :: " + document.getSourceAsString());
}
I've faced quite similar challenge.
We have to figure out what's the clip position in some list.
It works with assumption that we sort by score and by document id.
Without sorting by id or anything else as second sorting rule it would not work this way.
Here's a PHP code overview of our's solution:
$clipScore = $this->getScore($clip);
$lowestPossibleHigherScore = $this->getLowestPossibleHigherScore($clipScore);
$countHigherScore = $this->countClipsWithMinScore($lowestPossibleHigherScore);
$countSameScoreAndHigherId = $this->countClipsWithMinScore($clipScore, $clip->getId());
$countHigherScoreAndHigherId = $countHigherScore;
if ($countHigherScore > 0) {
$countHigherScoreAndHigherId = $this->countClipsWithMinScore($lowestPossibleHigherScore, $clip->getId());
}
$position = $countHigherScore + ($countSameScoreAndHigherId - $countHigherScoreAndHigherId);
return $position;
And here are some notes why this works ;)
/**
* Considered test cases =D
*
* | Position | Score | ID |
* | 0 | 4.0 | 3 |
* | 1 | 3.2 | 1 |
* | 2 | 3.1 | 6 |
* | 3 | 2.5 | 5 |
* | 4 | 2.5 | 4 |
* | 5 | 2.5 | 2 |
* | 6 | 1.2 | 7 |
*
* findPosition(ID = 4)
*
* countAllMinScore(2.501) = 3 (A) // so it's best position is 3 (4th place)
* countAllMinScoreAndBiggerId(2.5, 4) = 2 (C) // "two" of same score and higher ID
* countAllMinScoreAndBiggerId(2.501, 4) = 1 (D) // "one" of higher score and higher ID
*
* $position (how to get "4"?) = 3 (A) + 1 (C - D) ??? YES !!!!
*
* // next case
* findPosition(ID = 5)
*
* countAllMinScore(2.501) = 3 (A)
* countAllMinScoreAndBiggerId(2.5, 5) = 1 (C)
* countAllMinScoreAndBiggerId(2.501, 5) = 1 (D)
*
* $position (how to get "3"?) = 3 (A) + 0 (C - D) = 3 ??? YES !!!!
*
* // next case
* findPosition(ID = 2)
*
* countAllMinScore(2.501) = 3 (A)
* countAllMinScoreAndBiggerId(2.5, 2) = 5 (C)
* countAllMinScoreAndBiggerId(2.501, 2) = 3 (D)
*
* $position (how to get "5"?) = 3 (A) + 2 (C - D) = 5 ??? YES !!!!
*
* /// next case
* findPosition(ID = 3)
*
* countAllMinScore(4.001) = 0 (A)
* countAllMinScoreAndBiggerId(4.0, 3) = 0 (C)
* countAllMinScoreAndBiggerId(4.001, 3) = 0 (D)
*
* $position (how to get "0"?) = 0 (A) + 0 (C - D) = 0 ??? YES !!!!
*
* /// next case
* findPosition(ID = 7)
*
* countAllMinScore(1.201) = 6 (A)
* countAllMinScoreAndBiggerId(1.2, 7) = 0 (C)
* countAllMinScoreAndBiggerId(1.201, 7) = 0 (D)
*
* $position (how to get "6"?) = 6 (A) + 0 (C - D) = 6 ??? YES !!!!
*
*
* /// next case
*
* | Position | Score | ID |
* | 0 | 4.0 | 3 |
* | 1 | 4.0 | 1 |
* | 2 | 3.1 | 6 |
* | 3 | 2.5 | 5 |
* | 4 | 2.5 | 4 |
* | 5 | 2.5 | 2 |
* | 6 | 1.2 | 7 |
*
* findPosition(ID = 3)
*
* countAllMinScore(4.001) = 0 (A)
* countAllMinScoreAndBiggerId(4.0, 3) = 0 (C)
* countAllMinScoreAndBiggerId(4.001, 3) = 0 (D)
*
* $position (how to get "0"?) = 0 (A) + 0 (C - D) = 0 ??? YES !!!!
*
* /// next case
* findPosition(ID = 1)
*
* countAllMinScore(4.001) = 0 (A)
* countAllMinScoreAndBiggerId(4.0, 1) = 1 (C)
* countAllMinScoreAndBiggerId(4.001, 1) = 0 (D)
*
* $position (how to get "1"?) = 0 (A) + 1 (C - D) = 1 ??? YES !!!!
*/

how to calculate svg transform matrix from rotation with pivot point

If there is a svg rotation( a deg) with default pivot point(0,0), then I can calculate the rotation transform matrix as
_ _
| cos a -sin a 0 |
| sin a cos a 0 |
| 0 0 1 |
- -
But if pivot point is not (0,0), Lets say (px,py) then how do I calculate the rotation transform matrix?
I got the ans,
Lets the pivot point is (px ,py) and rotation is a degree
then net transform matrix will be
_ _ _ _
| 1 0 px | | cos a -sin a 0 |
net_matrix = | 0 1 py | X | sin a cos a 0 |
| 0 0 1 | | 0 0 1 |
- - - -
_ _
| 1 0 -px |
rotate_transform_matrix = net_matrix X | 0 1 -py |
| 0 0 1 |
- -
You can use javascript to apply the rotation transform on an svg element:
var rect = document.createElementNS("http://www.w3.org/2000/svg", "rect");
rect.setAttribute('transform', 'rotate(-30 50 50)');
rect.getCTM();
to get The TransformMatrix.
Just multiplying out (and tidying up the result to use the same variable names as the W3C) in case anyone else reading this wants something explicit.
rotate(a, cx, cy)
is equivalent to
matrix(cos(a), sin(a), -sin(a), cos(a), cx(1 - cos(a)) + cy(sin(a)), cy(1 - cos(a)) - cx(sin(a)))
Using mathematical notation assuming rotate and matrix are functions.
For anyone who is interested in Swarnendu Paul rotate_transform_matrix above, one would get:
_ _
| cos a -sin a px * (1 - cos a) + py * sin a |
| sin a cos a py * (1 - cos a) - px * sin a |
| 0 0 1 |
¯ ¯
I used it for SVG matrix transforms.

Resources