system of linear equation with additional constraint of minimal variables sum - algorithm

I have a system of linear equations that I already reduced to a row echelon matrix using Gauss-Jordan elimination. My system with n variables Xn (where Xn is in N0 (=positive integers)) has multiple solutions and I want to find the solution for witch the Sum of all Xn is minimal.
How could I do that programmatically?
For example consider this system of linear equations:
x1 + + x5 + x6 = 2
x2 + x5 = 1
x3 + x6 = 1
x4 + x5 + x6 = 1
one of the minimal solution I want to obtain is:
x3 = x4 = x5 = 0
x1 = x2 = x6 = 1
another one would be
x2 = x4 = x6 = 0
x1 = x3 = x5 = 1
But I don't want
x1 = 2
x2 = x3 = x4 = 1
x5 = x6 = 0
which is also a solution of this system, but not a minimal one according to my criteria as x1 + x2 + x3 + x4 + x5 + x6 = 5 (whereas it is only 3 for the 2 first solutions)
In case of multiple minimal solutions (like here, where solutions 1 and 2 are both minimal), I don't care about the minimal solution that is returned as long as it is one of the minimal ones

Since the variables are all nonnegative, this problem is essentially equivalent to integer programming. Use an off-the-shelf integer program solver and formulate like
minimize x1 + x2 + x3 + x4 + x5 + x6
subject to
x1 + x5 + x6 = 2
x2 + x5 = 1
x3 + x6 = 1
x4 + x5 + x6 = 1
integers
x1, x2, x3, x4, x5, x6 >= 0
(exact syntax depends on the tool).

Related

Counting number of quadruples of integers

I saw this question today where we need to count the number of
quadruples of integers
(X1, X2, X3, X4), such that Li ≤ Xi ≤ Ri for i
= 1, 2, 3, 4 and X1 ≠ X2, X2 ≠ X3, X3 ≠ X4, X4 ≠ X1.
input:
Li Ri
1 4
1 3
1 2
4 4
output:
8
1 2 1 4
1 3 1 4
1 3 2 4
2 1 2 4
2 3 1 4
2 3 2 4
3 1 2 4
3 2 1 4
My initial thoughts were using
Principle of Inclusion Exclusion
I was able to find number if unrestricted quadruples but I am not able to figure out how can we find the remaining conditions to reach the final solution. Also I came to know this question can be done using DFS .
How can we do this question with Inclusion Exclusion/ DFS
Inclusion/Exclusion will give you the number of quadruples, but won't give you the quadruples themselves.
Let Ai be the set of quadruples satisfying Lj<=Xj<=Rj for all j, with Xi=X(i+1) (where the indices are cyclic, so X5 means X1). In the example you provided,
A1 = { (1114), (1124), (2214), (2224), (3314), (3324) }
A2 = { (1114), (2114), (3114), (4114), (1224), (2224), (3224), (4224) }
A3 = { } (empty set)
A4 = { (4114), (4214), (4314), (4124), (4224), (4324) }
We also need the intersections of pairs of sets:
A1 cap A2 = { (1114), (2224) } (note first three numbers identical)
A1 cap A3 = { }
A1 cap A4 = { } (can't have X4=X1=X2)
A2 cap A3 = { }
A2 cap A4 = { (4114), (4224) }
A3 cap A4 = { }
Intersections of triples of sets:
A1 cap A2 cap A3 = { }
A1 cap A2 cap A4 = { }
A1 cap A3 cap A4 = { }
A2 cap A3 cap A4 = { }
And the intersection of all the sets:
A1 cap A2 cap A3 cap A4 = { }
Inclusion/exclusion in its complementary form tells us that
|intersection of complements of Ai| = |unrestricted quadruples|
- sum of |Ai| + sum of |Ai cap Aj| - sum of |Ai cap Aj cap Ak|
+ sum of |Ai cap Aj cap Ak cap Al|
where none of the indices i,j,k,l are equal. In your example,
|intersection of complements of Ai| = 4x3x2x1 - (6+8+0+6) + (2+0+0+0+2+0) - (0+0+0+0) + 0
= 24 - 20 + 4 - 0 + 0 = 8
In order to find the |Ai| and their intersections, you have to find intersections of intervals [Li,Ri] and multiply the lengths of intersections by the lengths of unrestricted intervals. For example,
|A1| = |[1234] cap [123]| x |[12]| x |[4]| = 3 x 2 x 1 = 6
|A2 cap A4| = |[123] cap [12]| x |[4] cap [1234]| = |[12]| x |[4]| = 2 x 1 = 2
I don't see what depth first search has to do with it in this approach.
It depends if the sets are disjoint or share elements. For n = 4, meaning quadruples, as you asked about, I think I got it down to between 1 and 4 iterations if we commit the ends to four types describing if x_1 is a member of X2 and x_4 a member of X3.
Example with three iterations:
input = {1,2,3}{1,2}{1,2,3}{3,4}
2 * (1)(12)(123)(3) = (1)(2)(1)(3) = 2 * 1 // x_1 ∈ X2, x_4 ∈ X3
2 * (1)(12)(123)(4) = (1)(2)(13)(4) = 2 * 2 // x_1 ∈ X2, x_4 ∉ X3
1 * (3)(12)(123)(4) = (3)(12)(12,3)(4) = 1 * (2 + 2) // x_1 ∉ X2, x_4 ∉ X3
Total = 10
Example with one iteration:
input = {1,2,3,4}{1,2,3,4}{1,2,3,4}{1,2,3,4} // x_1 ∈ X2, x_4 ∈ X3
12 * (1)(1234)(1234)(2) = (1)(2,34)(134)(2) = 12 * (3 + 4)
Total = 84

SPSS Return Highest Variable Name

1) I need SPSS to return the variable name of the highest variable in a series of subscales. Basically I have ten subscales with mean scores ranging from 0 to 5, I don't want to know the highest score for each case, but rather which subscale is highest.
When I use this syntax, I just get the score, which doesn't tell me which category it belongs to.
COMPUTE Motivation_Highest2 = MAX(Stress_Mgmt, Revitalisation, Enjoyment, Challenge, SocialRecog, Affiliation, Competition, HealthPress, IllHealth, PosHealth,
WtMgmt, Appearance, StrengthEnd, Nimbleness, MotivationHighest).
VARIABLE LABELS Motivation_Highest2 'Motivation Intensity: Highest Score on any Motivation Subscale'.
EXECUTE.
Can I ask SPSS to return the variable name instead of the score?
2) There may be two scores that are both equally high. In this case, I would like SPSS to give me both variable names.
Thanks!
This is an ok job for a macro to do.
DEFINE !MaxVars (OutN = !TOKENS(1)
/OutV = !CHAREND("/")
/Var = !CMDEND)
NUMERIC !OutN.
!DO !I !IN (!Var)
COMPUTE !OutN = MAX(!OutN,!I).
!DOEND
STRING !OutV (!CONCAT("A",!LENGTH(!Var))).
!DO !I !IN (!Var)
IF !I = !OutN !OutV = LTRIM(CONCAT(RTRIM(!OutV)," ",!QUOTE(!I))).
!DOEND
!ENDDEFINE.
And here is an example of using it on a set of data.
DATA LIST FREE / X1 X2 X3.
BEGIN DATA
1 2 3
3 2 1
4 4 0
0 4 4
1 1 1
END DATA.
!MaxVars OutN = Max OutV = Vars /Var = X1 X2 X3.
If you then run LIST Max Vars. it will return in the output:
Max Vars
3 X3
3 X1
4 X1 X2
4 X2 X3
1 X1 X2 X3

In Mathematica, how can I cut off the high-order terms in a polynomial?

For example, I have a polynomial y=a_0+a_1 x + ..... + a_50 x^50. Since I know that the high-order terms are imposing negligible effects on the evaluation of y, I want to cut off them and have something like y=a_0+a_1 x + ..... + a_10 x^10, the first eleven terms. How can I realize this?
I thank you all in advance.
In[1]:= y = a0 + a1*x + a2*x^2 + a3*x^3 + a4*x^4;
y /. x^b_ /; b >= 3 -> 0
Out[2]= a0 + a1 x + a2 x^2
The mathematically proper approach..
Series[ a0 + a1*x + a2*x^2 + a3*x^3 + a4*x^4, {x, 0, 2}] // Normal
-> a0 + a1 x + a2 x^2
If your polynomial is actually as simple as shown, with a term for every power of x and none others, you can simply use Take or Part to extract only those terms that you want because of the automatic ordering (in Plus) that Mathematica uses. For example:
exp1 = Expand[(1 + x)^9]
Take[exp1, 5]
1 + 9 x + 36 x^2 + 84 x^3 + 126 x^4 + 126 x^5 + 84 x^6 + 36 x^7 + 9 x^8 + x^9
1 + 9 x + 36 x^2 + 84 x^3 + 126 x^4
If it is not you will need something else. Bill's replacement rule is one concise and efficient method. For more complex manipulations you may wish to decompose the polynomial using CoefficientArrays, CoefficientRules, or CoefficientList.
There is a shortcut to the previous answers which is even more symbolic. You write, say,
y[x_] = a0 + a1 x + a2 x^2 + a3 x^3 + a4 x^4 + a5 x^5;
y[x] + O[x]^3
which gives you,
a0 + a1 x + a2 x^2 + O[x]^3

Converting a single col into mutiple rows (GNUPLOT file)

I'm trying to make a gnuplot picture from a file, have a problem with the distribution of 'data.txt' file. The actual distribution is :
1 4 x1
1 16 x2
4 4 x3
4 16 x4
8 4 x5
8 16 x6
The first line makes reference to the number of lines that i want, and the other colums make reference to the x and y axis.
I'm trying two approximations to make the picture without success:
Use some gnuplot function to sketch the picture using the actual distribution that my file have. I have not found this command.
Make a bash script to convert the actual file into another with the correct distribution
4 x1 x3 x5
16 x2 x4 x6
Addressing #2
awk '{a[$2] = a[$2] $3 " "} END {for (i in a) print i, a[i]}' file
4 x1 x3 x5
16 x2 x4 x6

Determine elements of a matrix when sum of rows and columns are given

There is 4x4 matrix with all 4 diagonal elements zero. All other elements are non negative integers. Sum of all 4 rows and 4 columns are known individually. Is it possible to determine the remaining 12 elements of the matrix? Eg
0 1 1 0 sum=2
2 0 0 1 sum=3
4 1 0 0 sum=5
0 1 6 0 sum=7
sum=6 sum=3 sum=7 sum=1
Any guidance will be very helpful.
Thanks
The matrix is
0 a12 a13 a14
a21 0 a23 a24
a31 a32 0 a34
a41 a42 a43 0
The problem is to solve a set of linear equations:
a12 + a13 + a14 = c1
a21 + a23 + a24 = c2
and so on. We have 12 variables and 8 equations (4 for the rows and 4 for the columns). To solve a linear equation system in 12 variables, we generally need 12 equations. Since the number of equations is lesser, the system will not have a unique solution. It may have infinitely many solutions.
The matrix is
0 a12 a13 a14
a21 0 a23 a24
a31 a32 0 a34
a41 a42 a43 0
The problem is to solve a set of linear equations:
a12 + a13 + a14 = r1
a21 + a23 + a24 = r2
a31 + a32 + a34 = r3
a41 + a43 + a44 = r4
a21 + a31 + a41 = c1
a12 + a32 + a42 = c2
a13 + a23 + a43 = c3
a14 + a34 + a44 = c4
Thus you need to solve an equation of the form Ax = b with A consisting of only 0 and 1 coefficients. Use Gauss Elimination and Euclidian Algorithm to find integer Matrices S, D, T such that D is in Diagonal form and SDT = A. If you do not know how to do this search the web for Smith normal form algorithm.
Then
SDTx = Ax = b
Thus
DTx = S-1Ax = S-1b
Since D is in diagonal form you can check if you can solve
Dy = S-1b
for y. You also find a base for the (Homogenous) solution space. This in turn can then be used to cut down the complexity in the search for the positive solutions of the original equation.

Resources