1 /*
2 *+
3 *  Name:
4 *     palInvf
5 
6 *  Purpose:
7 *     Invert a linear model of the type produced by the palFitxy routine.
8 
9 *  Language:
10 *     Starlink ANSI C
11 
12 *  Type of Module:
13 *     Library routine
14 
15 *  Invocation:
16 *     palInvf ( double fwds[6], double bkwds[6], int *j )
17 
18 *  Arguments:
19 *     fwds = double[6] (Given)
20 *        model coefficients
21 *     bkwds = double[6] (Returned)
22 *        inverse model
23 *     j = int (Returned)
24 *        status: 0 = OK, -1 = no inverse
25 
26 *  Description:
27 *     The models relate two sets of [x,y] coordinates as follows.
28 *     Naming the elements of fwds:
29 *     ---
30 *        fwds[0] = A
31 *        fwds[1] = B
32 *        fwds[2] = C
33 *        fwds[3] = D
34 *        fwds[4] = E
35 *        fwds[5] = F
36 *     ---
37 *     where two sets of coordinates [x1,y1] and [x2,y2] are related
38 *     thus:
39 *     ---
40 *        x2 = A + B * x1 + C * y1
41 *        y2 = D + E * x1 + F * y1
42 *     ---
43 *     the present routine generates a new set of coefficients:
44 *     ---
45 *        bkwds[0] = P
46 *        bkwds[1] = Q
47 *        bkwds[2] = R
48 *        bkwds[3] = S
49 *        bkwds[4] = T
50 *        bkwds[5] = U
51 *     ---
52 *     such that:
53 *     ---
54 *        x1 = P + Q * x2 + R * y2
55 *        y1 = S + T * x2 + U * y2
56 *     ---
57 *     Two successive calls to palInvf will thus deliver a set
58 *     of coefficients equal to the starting values.
59 
60 *  See also:
61 *     palFitxy, palPxy, palXy2xy and palDcmpf
62 
63 *  Authors:
64 *     PTW: Pat Wallace (STFC)
65 *     GSB: Graham Bell (EAO)
66 
67 *  History:
68 *     1990-04-11 (PTW):
69 *        SLALIB implementation.
70 *     2004-12-26 (PTW):
71 *        Documentation updated.
72 *     2018-10-23 (GSB):
73 *        Initial version in C.
74 
75 *  Copyright:
76 *     Copyright P.T.Wallace.  All rights reserved.
77 *     Copyright (C) 2018 East Asian Observatory.
78 
79 *  Licence:
80 *     This program is free software; you can redistribute it and/or modify
81 *     it under the terms of the GNU General Public License as published by
82 *     the Free Software Foundation; either version 2 of the License, or
83 *     (at your option) any later version.
84 *
85 *     This program is distributed in the hope that it will be useful,
86 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
87 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
88 *     GNU General Public License for more details.
89 *
90 *     You should have received a copy of the GNU General Public License
91 *     along with this program (see SLA_CONDITIONS); if not, write to the
92 *     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
93 *     Boston, MA  02110-1301  USA
94 *-
95 */
96 
palInvf(double fwds[6],double bkwds[6],int * j)97 void palInvf ( double fwds[6], double bkwds[6], int *j ) {
98   double a, b, c, d, e, f, det;
99 
100   a = fwds[0];
101   b = fwds[1];
102   c = fwds[2];
103   d = fwds[3];
104   e = fwds[4];
105   f = fwds[5];
106   det = b * f - c * e;
107 
108   if ( det != 0.0 ) {
109     bkwds[0] = (c * d - a * f) / det;
110     bkwds[1] = f / det;
111     bkwds[2] = -c / det;
112     bkwds[3] = (a * e - b * d) / det;
113     bkwds[4] = -e / det;
114     bkwds[5] = b / det;
115     *j = 0;
116   } else {
117     *j = -1;
118   }
119 }
120