From e55140e707fc8f9c10f4393d5467bf6bc6ad7e69 Mon Sep 17 00:00:00 2001 From: Michael Gage Date: Sat, 7 Oct 2017 19:29:45 -0400 Subject: [PATCH 01/30] Clean up and augment pod documentation on Matrix related files. --- lib/Matrix.pm | 73 +++- lib/Value/Matrix.pm | 109 ++++- macros/MatrixCheckers.pl | 30 +- macros/MatrixReduce.pl | 144 ++++--- macros/PGmatrixmacros.pl | 379 +++++++++-------- macros/PGmorematrixmacros.pl | 83 +++- macros/tableau.pl | 535 ++++++++++++++++++++++++ t/matrix_tableau_tests/matrix_test1.pg | 111 +++++ t/matrix_tableau_tests/tableau_test1.pg | 104 +++++ 9 files changed, 1275 insertions(+), 293 deletions(-) create mode 100755 macros/tableau.pl create mode 100644 t/matrix_tableau_tests/matrix_test1.pg create mode 100644 t/matrix_tableau_tests/tableau_test1.pg diff --git a/lib/Matrix.pm b/lib/Matrix.pm index 18c8093ff9..b2f83ce052 100644 --- a/lib/Matrix.pm +++ b/lib/Matrix.pm @@ -3,6 +3,12 @@ Matrix - Matrix of Reals Implements overrides for MatrixReal.pm for WeBWorK +In general it is better to use MathObjects Matrices (Value::Matrix) +in writing PG problem. The answer checking is much superior with better +error messages for syntax errors in student entries. Some of the +subroutines in this file are still used behind the scenes +by Value::Matrix to perform calculations, +such as decompose_LR(). =head1 DESCRIPTION @@ -68,6 +74,17 @@ sub _stringify { return($s); } +=head3 Accessor functions + + (these are deprecated for direct use. Use the covering Methods + provided by MathObject Matrices instead.) + + L($matrix) - return matrix L of the LR decomposition + R($matrix) - return matrix R of the LR decomposition + PL($matrix) return + PR($matrix + +Original matrix is P_L * L * R *P_R # obtain the Left Right matrices of the decomposition and the two pivot permutation matrices # the original is M = PL*L*R*PR sub L { @@ -119,10 +136,14 @@ sub PR { # use this permuation on the right PL*L*R*PR =M } # obtain the Left Right matrices of the decomposition and the two pivot permutation matrices # the original is M = PL*L*R*PR + + =head4 Method $matrix->rh_options +Meant for internal use when dealing with MatrixReal1 + =cut sub rh_options { @@ -137,9 +158,13 @@ sub rh_options { Method $matrix->trace Returns: scalar which is the trace of the matrix. + + Used by MathObject Matrices for calculating the trace. + Deprecated for direct use in PG questions. =cut + sub trace { my $self = shift; my $rows = $self->[1]; @@ -152,9 +177,12 @@ sub trace { $sum; } + =head4 - Method $matrix->new_from_array_ref + Method $new_matrix = $matrix->new_from_array_ref ([[a,b,c],[d,e,f]]) + + Deprecated in favor of using creation tools for MathObject Matrices =cut @@ -172,6 +200,8 @@ sub new_from_array_ref { # this will build a matrix or a row vector from [a, b Method $matrix->array_ref +Converts Matrix from an ARRAY to an ARRAY reference. + =cut sub array_ref { @@ -183,6 +213,8 @@ sub array_ref { Method $matrix->list +Converts a Matrix column vector to an ARRAY (list). + =cut sub list { # this is used only for column vectors @@ -196,29 +228,14 @@ sub list { # this is used only for column vectors @list; } -=head4 - - Method $matrix->new_from_list - -=cut - -sub new_from_list { # this builds a row vector from an array - my $class = shift; - my @list = @_; - my $cols = @list; - my $rows = 1; - my $matrix = new Matrix($rows, $cols); - my $i=1; - while(@list) { - my $elem = shift(@list); - $matrix->assign($i++,1, $elem); - } - $matrix; -} =head4 Method $matrix->new_row_matrix + + Deprecated -- there are better tools for MathObject Matrices. + +Create a row 1 by n matrix from a list. This subroutine appears to be broken =cut @@ -239,7 +256,9 @@ sub new_row_matrix { # this builds a row vector from an array =head4 Method $matrix->proj - + Provides behind the scenes calculations for MathObject Matrix->proj + Deprecated for direct use in favor of methods of MathObject matrix + =cut sub proj{ @@ -251,6 +270,8 @@ sub proj{ =head4 Method $matrix->proj_coeff + Provides behind the scenes calculations for MathObject Matrix->proj_coeff + Deprecated for direct use in favor of methods of MathObject matrix =cut @@ -271,6 +292,8 @@ sub proj_coeff{ Method $matrix->new_column_matrix + Create column matrix from an ARRAY reference (list reference) + =cut sub new_column_matrix { @@ -293,6 +316,8 @@ sub new_column_matrix { vectors. Method $matrix->new_from_col_vecs + + Deprecated: The tools for creating MathObjects Matrices are simpler =cut @@ -343,8 +368,8 @@ sub new_from_col_vecs =head4 - Method $matrix->new_from_col_vecs - + Function: cp() + Provides ability to use complex numbers. =cut sub cp { # MEG makes new copies of complex number @@ -462,6 +487,8 @@ sub transpose Method $matrix->decompose_LR + Used by MathObjects Matrix for LR decomposition + Deprecated for direct use in PG problems. =cut sub decompose_LR diff --git a/lib/Value/Matrix.pm b/lib/Value/Matrix.pm index f230931256..f7c4df3368 100644 --- a/lib/Value/Matrix.pm +++ b/lib/Value/Matrix.pm @@ -3,6 +3,109 @@ # Implements the Matrix class. # # @@@ Still needs lots of work @@@ + +=head1 Value::Matrix class + + +References: + +MathObject Matrix methods: L +MathObject Contexts: L +CPAN RealMatrix docs: L + +Allowing Matrices in Fractions: +L + + Context()->parens->set("[" => {formMatrix => 1}); + +Files interacting with Matrices: + +L L +L + +L -- checking whether vectors form a basis +L -- tools for row reduction via elementary matrices +L -- Generates unimodular matrices with real entries +L +L +L +L +L +L +Contexts + + Matrix -- allows students to enter [[3,4],[3,6]] + -- formMatrix =>1 also allows this? + Complex-Matrix -- allows complex entries + +Creation methods + + $M1 = Matrix([1,2],[3,4]); + $M2 = Matrix([5,6],[7,8]); + $v = Vector(9,10); + $w = ColumnVector(9,10); # differs in how it is printed + +Commands added in Value::matrix + + Conversion + $matrix->values produces [[3,4,5],[1,3,4]] recursive array references of numbers (not MathObjects) + $matrix->wwMatrix produces CPAN MatrixReal1 matrix, used for computation subroutines + + Information + $matrix->dimension: ARRAY + + Access values + + row : MathObjectMatrix + column : MathObjectMatrix + element : Real or Complex value + + Assign values + + these need to be added: + +see C in MatrixReduce and L + + Advanced + $matrix->data: ARRAY reference (internal data) of MathObjects (Real,Complex, Fractions) + stored at each location. + + +Passthrough methods covering subroutines in Matrix.pm which overrides or +augment CPAN's MatrixReal1.pm. Matrix is a specialized subclass of MatrixReal1.pm + +The actual calculations are done in Matrix.pm + + trace + proj + proj_coeff + L + R + PL + PR + +Passthrough methods covering subroutines in MatrixReal1.pm +(this has been modified to handle complex numbers) +The actual calculations are done in MatrixReal1.pm subroutines + + condition + det + inverse + is_symmetric + decompose_LR + dim + norm_one + norm_max + kleene + normalize + solve_LR (also solve()) + order_LR (also order() + solve_GSM + solve_SSM + solve_RM + +=cut + # package Value::Matrix; my $pkg = 'Value::Matrix'; @@ -17,7 +120,7 @@ our @ISA = qw(Value); # a point, vector or matrix object, a matrix-valued formula, or a string # that evaluates to a matrix # -sub new { +sub new { #internal my $self = shift; my $class = ref($self) || $self; my $context = (Value::isContext($_[0]) ? shift : $self->context); my $M = shift; $M = [] unless defined $M; $M = [$M,@_] if scalar(@_) > 0; @@ -38,7 +141,7 @@ sub new { # (Recursively) make a matrix from a list of array refs # and report errors about the entry types # -sub matrixMatrix { +sub matrixMatrix { #internal my $self = shift; my $class = ref($self) || $self; my $context = shift; my ($x,$m); my @M = (); my $isFormula = 0; @@ -62,7 +165,7 @@ sub matrixMatrix { # Form a 1 x n matrix from a list of numbers # (could become a row of an m x n matrix) # -sub numberMatrix { +sub numberMatrix { #internal my $self = shift; my $class = ref($self) || $self; my $context = shift; my @M = (); my $isFormula = 0; diff --git a/macros/MatrixCheckers.pl b/macros/MatrixCheckers.pl index 6f4c963173..ec13808365 100644 --- a/macros/MatrixCheckers.pl +++ b/macros/MatrixCheckers.pl @@ -87,23 +87,23 @@ =head1 DESCRIPTION are produced by C<\(\Bigg\lbrace\)> and C<\(\Bigg\rbrace\)>, are a matter of personal preference (since a basis is an ordered set, I like to include braces). -=over 12 -Context()->texStrings; -BEGIN_TEXT -Find an orthonormal basis for... -$BR -$BR -$BCENTER -\(\Bigg\lbrace\) -\{ $multians->ans_array(15) \}, -\{ $multians->ans_array(15) \} -\(\Bigg\rbrace.\) -$ECENTER -END_TEXT -Context()->normalStrings; -=back + Context()->texStrings; + BEGIN_TEXT + Find an orthonormal basis for... + $BR + $BR + $BCENTER + \(\Bigg\lbrace\) + \{ $multians->ans_array(15) \}, + \{ $multians->ans_array(15) \} + \(\Bigg\rbrace.\) + $ECENTER + END_TEXT + Context()->normalStrings; + + The answer evaluation section of the PG file is totally standard. diff --git a/macros/MatrixReduce.pl b/macros/MatrixReduce.pl index 010d732269..a77925dc76 100644 --- a/macros/MatrixReduce.pl +++ b/macros/MatrixReduce.pl @@ -14,27 +14,45 @@ =head1 SYNOPSIS =over 12 -=item Get the reduced row echelon form: C<$Areduced = rref($A);> Should be used in the fraction context with all entries of $A made into fractions. +=item Get the reduced row echelon form: C<$Areduced = rref($A);> + +Should be used in the fraction context with all entries of $A made into fractions. -=item Make matrix entries do fraction arithmetic (rather than decimal arithmetic): After selecting the Fraction context using Context('Fraction')->parens->set("[" => {formMatrix => 1}), C<$A = apply_fraction_to_matrix_entries($A);> applies Fraction() to all of the entries of $A, which makes subsequent matrix algebra computations with $A use fraction arithmetic. +=item Make matrix entries do fraction arithmetic (rather than decimal arithmetic): + +After selecting the Fraction context using Context('Fraction')->parens->set("[" => {formMatrix => 1}), C<$A = apply_fraction_to_matrix_entries($A);> applies Fraction() to all of the entries of $A, which makes subsequent matrix algebra computations with $A use fraction arithmetic. =item Get the reduced column echelon form: C<$Areduced = rcef($A);> -=item Change the value of a matrix entry: C changes the [2,3] entry to the value 50. +=item Change the value of a matrix entry: C + +changes the [2,3] entry to the value 50. =item Construct an n x n identity matrix: C<$E = identity_matrix(5);> -=item Construct an n x n elementary matrix that will permute rows i and j: C<$E = elem_matrix_row_switch(5,2,4);> creates a 5 x 5 identity matrix and swaps rows 2 and 4. +=item Construct an n x n elementary matrix that will permute rows i and j: + +C<$E = elem_matrix_row_switch(5,2,4);> creates a 5 x 5 identity matrix and swaps rows 2 and 4. + +=item Construct an n x n elementary matrix that will multiply row i by s: C<$E = elem_matrix_row_mult(5,2,4);> + +creates a 5 x 5 identity matrix and swaps puts 4 in the second spot on the diagonal. -=item Construct an n x n elementary matrix that will multiply row i by s: C<$E = elem_matrix_row_mult(5,2,4);> creates a 5 x 5 identity matrix and swaps puts 4 in the second spot on the diagonal. +=item Construct an n x n elementary matrix that will multiply row i by s: C<$E3 = elem_matrix_row_add(5,3,1,35);> -=item Construct an n x n elementary matrix that will multiply row i by s: C<$E3 = elem_matrix_row_add(5,3,1,35);> creates a 5 x 5 identity matrix and swaps puts 35 in the (3,1) position. +creates a 5 x 5 identity matrix and swaps puts 35 in the (3,1) position. -=item Perform the row switch transform that swaps (row i) with (row j): C<$Areduced = row_switch($A,2,4);> swaps rows 2 and 4 in matrix $A. +=item Perform the row switch transform that swaps (row i) with (row j): C<$Areduced = row_switch($A,2,4);> -=item Perform the row multiplication transform s * (row i) placed into (row i): C<$Areduced = row_mult(A,2,10);> multiplies every entry in row 2 of $A by 10. +swaps rows 2 and 4 in matrix $A. -=item Perform the row addition transform (row i) + s * (row j) placed into (row i): C<$Areduced = row_add($A,2,1,10);> adds 10 times row 1 to row 2 and places the result in row 2. (Same as constructing $E to be the identity with 10 placed in entry (2,1), then multiplying $E * $A.) +=item Perform the row multiplication transform s * (row i) placed into (row i): C<$Areduced = row_mult(A,2,10);> + +multiplies every entry in row 2 of $A by 10. + +=item Perform the row addition transform (row i) + s * (row j) placed into (row i): C<$Areduced = row_add($A,2,1,10);> + +adds 10 times row 1 to row 2 and places the result in row 2. (Same as constructing $E to be the identity with 10 placed in entry (2,1), then multiplying $E * $A.) =back @@ -42,61 +60,59 @@ =head1 DESCRIPTION Usage: -=over 12 - -DOCUMENT(); -loadMacros( -"PGstandard.pl", -"MathObjects.pl", -"MatrixReduce.pl", # automatically loads contextFraction.pl and MathObjects.pl -"PGcourse.pl", -); -$showPartialCorrectAnswers = 0; -TEXT(beginproblem()); - -# Context('Matrix'); # for decimal arithmetic -Context('Fraction'); # for fraction arithmetic - -$A = Matrix([ -[random(-5,5,1),random(-5,5,1),random(-5,5,1),3], -[random(-5,5,1),random(-5,5,1),random(-5,5,1),0.75], -[random(-5,5,1),random(-5,5,1),random(-5,5,1),9/4], -]); - -$A = apply_fraction_to_matrix_entries($A); # try commenting this line out for different results - -$Arref = rref($A); - -$Aswitch = row_switch($A, 2, 3); - -$Amult = row_mult($A, 2, 4); - -$Aadd = row_add($A, 2, 1, 10); - -$E = elem_matrix_row_add(3,2,1,10); -$EA = $E * $A; - -$E1 = elem_matrix_row_switch(5,2,4); -$E2 = elem_matrix_row_mult(5,4,Fraction(1/10)); -$E3 = elem_matrix_row_add(5,3,1,35); -$E4 = identity_matrix(4); -change_matrix_entry($E4,[3,2],10); - -Context()->texStrings; -BEGIN_TEXT -The original matrix and its row reduced echelon form: -\[ $A \sim $Arref. \] -$BR -The original matrix with rows switched, multiplied, or added together: -\[ $Aswitch, $Amult, $Aadd. \] -$BR -Some elementary matrices. -\[$E1, $E2, $E3, $E4\] -END_TEXT -Context()->normalStrings; - -COMMENT('MathObject version.'); -ENDDOCUMENT(); + DOCUMENT(); + loadMacros( + "PGstandard.pl", + "MathObjects.pl", + "MatrixReduce.pl", # automatically loads contextFraction.pl and MathObjects.pl + "PGcourse.pl", + ); + $showPartialCorrectAnswers = 0; + TEXT(beginproblem()); + + # Context('Matrix'); # for decimal arithmetic + Context('Fraction'); # for fraction arithmetic + + $A = Matrix([ + [random(-5,5,1),random(-5,5,1),random(-5,5,1),3], + [random(-5,5,1),random(-5,5,1),random(-5,5,1),0.75], + [random(-5,5,1),random(-5,5,1),random(-5,5,1),9/4], + ]); + + $A = apply_fraction_to_matrix_entries($A); # try commenting this line out for different results + + $Arref = rref($A); + + $Aswitch = row_switch($A, 2, 3); + + $Amult = row_mult($A, 2, 4); + + $Aadd = row_add($A, 2, 1, 10); + + $E = elem_matrix_row_add(3,2,1,10); + $EA = $E * $A; + + $E1 = elem_matrix_row_switch(5,2,4); + $E2 = elem_matrix_row_mult(5,4,Fraction(1/10)); + $E3 = elem_matrix_row_add(5,3,1,35); + $E4 = identity_matrix(4); + change_matrix_entry($E4,[3,2],10); + + Context()->texStrings; + BEGIN_TEXT + The original matrix and its row reduced echelon form: + \[ $A \sim $Arref. \] + $BR + The original matrix with rows switched, multiplied, or added together: + \[ $Aswitch, $Amult, $Aadd. \] + $BR + Some elementary matrices. + \[$E1, $E2, $E3, $E4\] + END_TEXT + Context()->normalStrings; + + COMMENT('MathObject version.'); + ENDDOCUMENT(); =back diff --git a/macros/PGmatrixmacros.pl b/macros/PGmatrixmacros.pl index 070646c63a..c40d183b74 100644 --- a/macros/PGmatrixmacros.pl +++ b/macros/PGmatrixmacros.pl @@ -12,11 +12,20 @@ =head1 SYNPOSIS =head1 DESCRIPTION -Almost all of the macros in the file are very rough at best. The most useful is display_matrix. -Many of the other macros work with vectors and matrices stored as anonymous arrays. +These macros are fairly old. The most useful is display_matrix and +its variants. -Frequently it may be -more useful to use the Matrix objects defined RealMatrix.pm and Matrix.pm and the constructs listed there. +Frequently it will be +most useful to use the MathObjects Matrix (defined in Value::Matrix.pm) +and Vector types which +have more capabilities and more error checking than the subroutines in +this file. These macros have no object orientation and +work with vectors and matrices +stored as perl anonymous arrays. + +There are also Matrix objects defined in +RealMatrix.pm and Matrix.pm but in almost all cases the +MathObjects Matrix types are preferable. =cut @@ -28,132 +37,57 @@ BEGIN sub _PGmatrixmacros_init { } -# this subroutine zero_check is not very well designed below -- if it is used much it should receive -# more work -- particularly for checking relative tolerance. More work needs to be done if this is -# actually used. - -sub zero_check{ - my $array = shift; - my %options = @_; - my $num = @$array; - my $i; - my $max = 0; my $mm; - for ($i=0; $i< $num; $i++) { - $mm = $array->[$i] ; - $max = abs($mm) if abs($mm) > $max; - } - my $tol = $options{tol}; - $tol = 0.01*$options{reltol}*$options{avg} if defined($options{reltol}) and defined $options{avg}; - $tol = .000001 unless defined($tol); - ($max <$tol) ? 1: 0; # 1 if the array is close to zero; -} -sub vec_dot{ - my $vec1 = shift; - my $vec2 = shift; - warn "vectors must have the same length" unless @$vec1 == @$vec2; # the vectors must have the same length. - my @vec1=@$vec1; - my @vec2=@$vec2; - my $sum = 0; - - while(@vec1) { - $sum += shift(@vec1)*shift(@vec2); - } - $sum; -} -sub proj_vec { - my $vec = shift; - warn "First input must be a column matrix" unless ref($vec) eq 'Matrix' and ${$vec->dim()}[1] == 1; - my $matrix = shift; # the matrix represents a set of vectors spanning the linear space - # onto which we want to project the vector. - warn "Second input must be a matrix" unless ref($matrix) eq 'Matrix' and ${$matrix->dim()}[1] == ${$vec->dim()}[0]; - $matrix * transpose($matrix) * $vec; -} - -sub vec_cmp{ #check to see that the submitted vector is a non-zero multiple of the correct vector - my $correct_vector = shift; - my %options = @_; - my $ans_eval = sub { - my $in = shift @_; - - my $ans_hash = new AnswerHash; - my @in = split("\0",$in); - my @correct_vector=@$correct_vector; - $ans_hash->{student_ans} = "( " . join(", ", @in ) . " )"; - $ans_hash->{correct_ans} = "( " . join(", ", @correct_vector ) . " )"; - - return($ans_hash) unless @$correct_vector == @in; # make sure the vectors are the same dimension - - my $correct_length = vec_dot($correct_vector,$correct_vector); - my $in_length = vec_dot(\@in,\@in); - return($ans_hash) if $in_length == 0; - - if (defined($correct_length) and $correct_length != 0) { - my $constant = vec_dot($correct_vector,\@in)/$correct_length; - my @difference = (); - for(my $i=0; $i < @correct_vector; $i++ ) { - $difference[$i]=$constant*$correct_vector[$i] - $in[$i]; - } - $ans_hash->{score} = zero_check(\@difference); - - } else { - $ans_hash->{score} = 1 if vec_dot(\@in,\@in) == 0; - } - $ans_hash; - - }; - - $ans_eval; -} ############ =head4 display_matrix - Usage \{ display_matrix( [ [1, '\(\sin x\)'], [ans_rule(5), 6] ]) \} - \{ display_matrix($A, align=>'crvl') \} - \[ \{ display_matrix_mm($A) \} \] - \[ \{ display_matrix_mm([ [1, 3], [4, 6] ]) \} \] - - display_matrix produces a matrix for display purposes. It checks whether - it is producing LaTeX output, or if it is displaying on a web page in one - of the various modes. The input can either be of type Matrix, Value::Matrix (mathobject) - or a reference to an array. - - Entries can be numbers, Fraction objects, bits of math mode, or answer - boxes. An entire row can be replaced by the string 'hline' to produce - a horizontal line in the matrix. - - display_matrix_mm functions similarly, except that it should be inside - math mode. display_matrix_mm cannot contain answer boxes in its entries. - Entries to display_matrix_mm should assume that they are already in - math mode. - - Both functions take an optional alignment string, similar to ones in - LaTeX tabulars and arrays. Here c for centered columns, l for left - flushed columns, and r for right flushed columns. - - The alignment string can also specify vertical rules to be placed in the - matrix. Here s or | denote a solid line, d is a dashed line, and v - requests the default vertical line. This can be set on a system-wide - or course-wide basis via the variable $defaultDisplayMatrixStyle, and - it can default to solid, dashed, or no vertical line (n for none). - - The matrix has left and right delimiters also specified by - $defaultDisplayMatrixStyle. They can be parentheses, square brackets, - braces, vertical bars, or none. The default can be overridden in - an individual problem with optional arguments such as left=>"|", or - right=>"]". - - You can specify an optional argument of 'top_labels'=> ['a', 'b', 'c']. - These are placed above the columns of the matrix (as is typical for - linear programming tableau, for example). The entries will be typeset - in math mode. - - Top labels require a bit of care. For image modes, they look better - with display_matrix_mm where it is all one big image, but they work with - display_matrix. With tth, you pretty much have to use display_matrix - since tth can't handle the TeX tricks used to get the column headers - up there if it gets the whole matrix at once. + Usage + \{ display_matrix( [ [1, '\(\sin x\)'], [ans_rule(5), 6] ]) \} + \{ display_matrix($A, align=>'crvl') \} + \[ \{ display_matrix_mm($A) \} \] + \[ \{ display_matrix_mm([ [1, 3], [4, 6] ]) \} \] + +display_matrix produces a matrix for display purposes. It checks whether +it is producing LaTeX output, or if it is displaying on a web page in one +of the various modes. The input can either be of type Matrix, Value::Matrix (mathobject) +or a reference to an array. + +Entries can be numbers, Fraction objects, bits of math mode, or answer +boxes. An entire row can be replaced by the string 'hline' to produce +a horizontal line in the matrix. + +display_matrix_mm functions similarly, except that it should be inside +math mode. display_matrix_mm cannot contain answer boxes in its entries. +Entries to display_matrix_mm should assume that they are already in +math mode. + +Both functions take an optional alignment string, similar to ones in +LaTeX tabulars and arrays. Here c for centered columns, l for left +flushed columns, and r for right flushed columns. + +The alignment string can also specify vertical rules to be placed in the +matrix. Here s or | denote a solid line, d is a dashed line, and v +requests the default vertical line. This can be set on a system-wide +or course-wide basis via the variable $defaultDisplayMatrixStyle, and +it can default to solid, dashed, or no vertical line (n for none). + +The matrix has left and right delimiters also specified by +$defaultDisplayMatrixStyle. They can be parentheses, square brackets, +braces, vertical bars, or none. The default can be overridden in +an individual problem with optional arguments such as left=>"|", or +right=>"]". + +You can specify an optional argument of 'top_labels'=> ['a', 'b', 'c']. +These are placed above the columns of the matrix (as is typical for +linear programming tableau, for example). The entries will be typeset +in math mode. + +Top labels require a bit of care. For image modes, they look better +with display_matrix_mm where it is all one big image, but they work with +display_matrix. With tth, you pretty much have to use display_matrix +since tth can't handle the TeX tricks used to get the column headers +up there if it gets the whole matrix at once. =cut @@ -692,6 +626,7 @@ sub mbox { =head4 ra_flatten_matrix Usage: ra_flatten_matrix($A) + returns: [a11, a12,a21,a22] where $A is a matrix object The output is a reference to an array. The matrix is placed in the array by iterating @@ -715,9 +650,16 @@ sub ra_flatten_matrix{ \@array; } -# This subroutine is probably obsolete and not generally useful. It was patterned after the APL -# constructs for multiplying matrices. It might come in handy for non-standard multiplication of -# of matrices (e.g. mod 2) for indice matrices. += head4 apl_matrix_mult() + + # This subroutine is probably obsolete and not generally useful. + # It was patterned after the APL + # constructs for multiplying matrices. It might come in handy + # for non-standard multiplication of + # of matrices (e.g. mod 2) for indice matrices. + +=cut + sub apl_matrix_mult{ my $ra_a= shift; my $ra_b= shift; @@ -763,9 +705,11 @@ sub make_matrix{ =head4 create2d_matrix -This can be a useful method for quickly entering small matrices by hand. --MEG +This can be a useful method for quickly entering small matrices by hand. + --MEG - create2d_matrix("1 2 4, 5 6 8"); + create2d_matrix("1 2 4, 5 6 8"); or + create2d_matrix("1 2 4; 5 6 8"); produces the anonymous array [[1,2,4],[5,6,8] ] @@ -775,11 +719,42 @@ =head4 create2d_matrix sub create2d_matrix { my $string = shift; - my @rows = split("\\s*,\\s*",$string); + my @rows = split("\\s*[,;]\\s*",$string); @rows = map {[split("\\s", $_ )]} @rows; [@rows]; } + +=head2 convert_to_array_ref { + + $output_matrix = convert_to_array_ref($input_matrix) + +Converts a MathObject matrix (ref($input_matrix eq 'Value::Matrix') +or a MatrixReal1 matrix (ref($input_matrix eq 'Matrix')to +a reference to an array (e.g [[4,6],[3,2]]). +This adaptor allows all of the LinearProgramming.pl subroutines to be used with +MathObject arrays. + +$mathobject_matrix->value outputs an array (usually an array of array references) so placing it inside +square bracket produces and array reference (of array references) which is what lp_display_mm() is +seeking. + +=cut + +sub convert_to_array_ref { + my $input = shift; + if (ref($input) eq 'Value::Matrix' ) { + $input = [$input->value]; + } elsif (ref($input) eq 'Matrix' ) { + $input = $input->array_ref; + } elsif (ref($input) =~/ARRAY/) { + # no change to input value + } else { + WARN_MESSAGE("This does not appear to be a matrix "); + } + $input; +} + =head4 check_matrix_from_ans_box_cmp An answer checker factory built on create2d_matrix. This still needs @@ -796,7 +771,6 @@ sub check_matrix_from_ans_box_cmp{ my $string_matrix_cmp = sub { $string = shift @_; my $studentMatrix; - # eval { $studentMatrix = Matrix(create2d_matrix($string)); die "I give up";}; #caught by op_mask $studentMatrix = Matrix(create2d_matrix($string)); die "I give up"; # main::DEBUG_MESSAGE(ref($studentMatrix). "$studentMatrix with error "); # errors are returned as warnings. Can't seem to trap them. @@ -815,67 +789,100 @@ sub check_matrix_from_ans_box_cmp{ } -=head2 convert_to_array_ref { - $output_matrix = convert_to_array_ref($input_matrix) +=head4 zero_check (deprecated -- use MathObjects matrices and vectors) -Converts a MathObject matrix (ref($input_matrix eq 'Value::Matrix') -or a MatrixReal1 matrix (ref($input_matrix eq 'Matrix')to -a reference to an array (e.g [[4,6],[3,2]]). -This adaptor allows all of the Linear Programming subroutines to be used with -MathObject arrays. + # this subroutine zero_check is not very well designed below -- if it is used much it should receive + # more work -- particularly for checking relative tolerance. More work needs to be done if this is + # actually used. -$mathobject_matrix->value outputs an array (usually an array of array references) so placing it inside -square bracket produces and array reference (of array references) which is what lp_display_mm() is -seeking. +=cut + +sub zero_check{ + my $array = shift; + my %options = @_; + my $num = @$array; + my $i; + my $max = 0; my $mm; + for ($i=0; $i< $num; $i++) { + $mm = $array->[$i] ; + $max = abs($mm) if abs($mm) > $max; + } + my $tol = $options{tol}; + $tol = 0.01*$options{reltol}*$options{avg} if defined($options{reltol}) and defined $options{avg}; + $tol = .000001 unless defined($tol); + ($max <$tol) ? 1: 0; # 1 if the array is close to zero; +} + +=head4 vec_dot() (deprecated -- use MathObjects vectors and matrices) + +sub vec_dot{ + my $vec1 = shift; + my $vec2 = shift; + warn "vectors must have the same length" unless @$vec1 == @$vec2; # the vectors must have the same length. + my @vec1=@$vec1; + my @vec2=@$vec2; + my $sum = 0; + + while(@vec1) { + $sum += shift(@vec1)*shift(@vec2); + } + $sum; +} + +=head4 proj_vect (deprecated -- use MathObjects vectors and matrices) =cut -sub convert_to_array_ref { - my $input = shift; - if (ref($input) eq 'Value::Matrix' ) { - $input = [$input->value]; - } elsif (ref($input) eq 'Matrix' ) { - $input = $input->array_ref; - } elsif (ref($input) =~/ARRAY/) { - # no change to input value - } else { - WARN_MESSAGE("This does not appear to be a matrix "); - } - $input; +sub proj_vec { + my $vec = shift; + warn "First input must be a column matrix" unless ref($vec) eq 'Matrix' and ${$vec->dim()}[1] == 1; + my $matrix = shift; # the matrix represents a set of vectors spanning the linear space + # onto which we want to project the vector. + warn "Second input must be a matrix" unless ref($matrix) eq 'Matrix' and ${$matrix->dim()}[1] == ${$vec->dim()}[0]; + $matrix * transpose($matrix) * $vec; +} + +=head4 vec_cmp (deprecated -- use MathObjects vectors and matrices) + +=cut + + +sub vec_cmp{ #check to see that the submitted vector is a non-zero multiple of the correct vector + my $correct_vector = shift; + my %options = @_; + my $ans_eval = sub { + my $in = shift @_; + + my $ans_hash = new AnswerHash; + my @in = split("\0",$in); + my @correct_vector=@$correct_vector; + $ans_hash->{student_ans} = "( " . join(", ", @in ) . " )"; + $ans_hash->{correct_ans} = "( " . join(", ", @correct_vector ) . " )"; + + return($ans_hash) unless @$correct_vector == @in; # make sure the vectors are the same dimension + + my $correct_length = vec_dot($correct_vector,$correct_vector); + my $in_length = vec_dot(\@in,\@in); + return($ans_hash) if $in_length == 0; + + if (defined($correct_length) and $correct_length != 0) { + my $constant = vec_dot($correct_vector,\@in)/$correct_length; + my @difference = (); + for(my $i=0; $i < @correct_vector; $i++ ) { + $difference[$i]=$constant*$correct_vector[$i] - $in[$i]; + } + $ans_hash->{score} = zero_check(\@difference); + + } else { + $ans_hash->{score} = 1 if vec_dot(\@in,\@in) == 0; + } + $ans_hash; + + }; + + $ans_eval; } -# sub format_answer{ -# my $ra_eigenvalues = shift; -# my $ra_eigenvectors = shift; -# my $functionName = shift; -# my @eigenvalues=@$ra_eigenvalues; -# my $size= @eigenvalues; -# my $ra_eigen = make_matrix( sub {my ($i,$j) = @_; ($i==$j) ? "e^{$eigenvalues[$j] t}": 0 }, $size,$size); -# my $out = qq! -# $functionName(t) =! . -# displayMatrix(apl_matrix_mult($ra_eigenvectors,$ra_eigen, -# 'times'=>sub{($_[0] and $_[1]) ? "$_[0]$_[1]" : ''}, -# 'plus'=>sub{ my $out = join("",@_); ($out) ?$out : '0' } -# ) ) ; -# $out; -# } -# sub format_vector_answer{ -# my $ra_eigenvalues = shift; -# my $ra_eigenvectors = shift; -# my $functionName = shift; -# my @eigenvalues=@$ra_eigenvalues; -# my $size= @eigenvalues; -# my $ra_eigen = make_matrix( sub {my ($i,$j) = @_; ($i==$j) ? "e^{$eigenvalues[$j] t}": 0 }, $size,$size); -# my $out = qq! -# $functionName(t) =! . -# displayMatrix($ra_eigenvectors)."e^{$eigenvalues[0] t}" ; -# $out; -# } -# sub format_question{ -# my $ra_matrix = shift; -# my $out = qq! y'(t) = ! . displayMatrix($B). q! y(t)! -# -# } 1; diff --git a/macros/PGmorematrixmacros.pl b/macros/PGmorematrixmacros.pl index b2af761786..8ed22573a9 100644 --- a/macros/PGmorematrixmacros.pl +++ b/macros/PGmorematrixmacros.pl @@ -5,9 +5,24 @@ BEGIN # set the prefix used for arrays. our $ArRaY = $main::PG->{ARRAY_PREFIX}; +=head2 NAME + + macros/PGmorematrixmacros.pl + +=cut + + sub _PGmorematrixmacros_init{} -sub random_inv_matrix { ## Builds and returns a random invertible \$row by \$col matrix. +=head4 random_inv_matrix + +## Builds and returns a random invertible \$row by \$col matrix. + +=cut + + +sub random_inv_matrix { +## Builds and returns a random invertible \$row by \$col matrix. warn "Usage: \$new_matrix = random_inv_matrix(\$rows,\$cols)" if (@_ != 2); @@ -54,16 +69,29 @@ sub random_diag_matrix{ ## Builds and returns a random diagonal \$n by \$n matri return $D; } +=head4 swap_rows ($matrix, $row1, $row2) + + (deprecated use MathObject Matrix instead) + +$matrix is assumed to be a RealMatrix1 object. +It is better to use MathObject Matrices and row swap mechanisms +from MatrixReduce.pl instead. + +=cut + + sub swap_rows{ warn "Usage: \$new_matrix = swap_rows(\$matrix,\$row1,\$row2);" if (@_ != 3); my $matrix = $_[0]; my ($i,$j) = ($_[1],$_[2]); + warn "Error: Rows to be swapped must exist!" if ($i>@$matrix or $j >@$matrix); warn "Warning: Swapping the same row is pointless" - if ($i==$j); + if ($i==$j); + my $cols = @{$matrix->[0]}; my $B = new Matrix(@$matrix,$cols); foreach my $k (1..$cols){ @@ -73,6 +101,16 @@ sub swap_rows{ return $B; } +=head4 row_mult ($matrix, $scaler, $row) + + (deprecated use MathObject Matrix instead) + +$matrix is assumed to be a RealMatrix1 object. +It is better to use MathObject Matrices and row swap mechanisms +from MatrixReduce.pl instead. + +=cut + sub row_mult{ warn "Usage: \$new_matrix = row_mult(\$matrix,\$scalar,\$row);" @@ -88,6 +126,16 @@ sub row_mult{ return $B; } +sub linear_combo($matrix, $scalar, $row1, $row2) + + (deprecated use MathObject Matrix instead) + +Adds a multiple of row1 to row2. + +$matrix is assumed to be a RealMatrix1 object. +It is better to use MathObject Matrices and subroutines +from MatrixReduce.pl instead. + sub linear_combo{ warn "Usage: \$new_matrix = linear_combo(\$matrix,\$scalar,\$row1,\$row2);" @@ -106,6 +154,15 @@ sub linear_combo{ return $B; } + +=head2 + +These should be compared to similar subroutines made later in +MatrixCheckers.pl + + +=cut + =head3 basis_cmp() Compares a list of vectors by finding the change of coordinate matrix @@ -378,6 +435,8 @@ sub compare_basis { =head2 vec_list_string +(this is mostly obsolete. One should use MathObject Vectors instead. ) + This is a check_syntax type method (in fact I borrowed some of that method's code) for vector input. The student needs to enter vectors like: [1,0,0],[1,2,3],[0,9/sqrt(10),1/sqrt(10)] Each entry can contain functions and operations and the usual math constants (pi and e). @@ -503,8 +562,14 @@ sub vec_list_string{ $rh_ans; } + + + =head5 ans_array_filter + (this filter is not necessary when using MathObjects. It may someday be useful + again if the AnswerEvaluator pipeline is used to its fullest extent. ) + This filter was created to get, format, and evaluate each entry of the ans_array and ans_array_extension answer entry methods. Running this filter is necessary to get all the entries out of the answer hash. Each entry is evaluated and the resulting number is put in the display for student answer @@ -616,6 +681,20 @@ sub ans_array_filter{ } +=head3 + +The following subroutines, meant to be used with MatrixReal1 type matrices, are +deprecated. In general you should use the MathObject Matrix type and the +checking methods in MatrixCheckers.pl + + are_orthogonal_vecs($vec_ref, %opts) + is_diagonal($matrix, %opts) + are_unit_vecs($vec_ref, %opts) + display_correct_vecs($vec_ref, %opts) + vec_solution_cmp($vec,%opts) + filter: compare_vec_solution($rh_ans,%opts); + +=cut sub are_orthogonal_vecs{ my ($vec_ref , %opts) = @_; diff --git a/macros/tableau.pl b/macros/tableau.pl new file mode 100755 index 0000000000..bb4c7514d6 --- /dev/null +++ b/macros/tableau.pl @@ -0,0 +1,535 @@ +#!/usr/bin/perl -w + +# this file needs documentation and unit testing. +# where is it used? + +##### From gage_matrix_ops +# 2014_HKUST_demo/templates/setSequentialWordProblem/bill_and_steve.pg:"gage_matrix_ops.pl", + +=head1 Tableaus and matrices + + # We're going to have several types + # MathObject Matrices Value::Matrix + # tableaus form John Jones macros + # MathObject tableaus + # Containing an matrix $A coefficients for constraint + # A vertical vector $b for constants for constraints + # A horizontal vector $c for coefficients for objective function + # A vertical vector corresponding to the value $z of the objective function + # dimensions $n problem vectors, $m constraints = $m slack variables + # A basis Value::Set -- positions for columns which are independent and + # whose associated variables can be determined + # uniquely from the parameter variables. + # The non-basis (parameter) variables are set to zero. + # + # state variables (assuming parameter variables are zero or when given parameter variables) + # create the methods for updating the various containers + # create the method for printing the tableau with all its decorations + # possibly with switches to turn the decorations on and off. + + +The structure of the tableau is: + + ----------------------------------------------------------- + | | | | | + | A | S | 0 | b | + | | | | | + ---------------------------------------------- + | -c | 0 | 1 | 0 | + ---------------------------------------------- + Matrix A, the constraint matrix is n by m + Matrix S, the slack variables is m by m + Matrix b, the constraint constants is n by 1 + The next to the last column holds z or objective value + z(...x^i...) = c_i* x^i (Einstein summation convention) + + +=cut + +=head2 Package main + +=cut + +=item get_tableau_variable_values + + Parameters: ($MathObjectMatrix_tableau, $MathObjectSet_basis) + Returns: ARRAY or ARRAY_ref + +Returns the solution variables to the tableau assuming +that the parameter (non-basis) variables +have been set to zero. It returns a list in +array context and a reference to +an array in scalar context. + +=item lp_basis_pivot + + Parameters: ($old_tableau,$old_basis,$pivot) + Returns: ($new_tableau, Set($new_basis),\@statevars) + +=item linebreak_at_commas + + Parameters: () + Return: + + Useage: + $foochecker = $constraints->cmp()->withPostFilter( + linebreak_at_commas() + ); + +Replaces commas with line breaks in the latex presentations of the answer checker. +Used most often when $constraints is a LinearInequality math object. + + + +=head2 Package tableau + +=item Tableau->new(A=>Matrix, b=>Vector or Matrix, c=>Vector or Matrix) + + A => undef, # constraint matrix MathObjectMatrix + b => undef, # constraint constants Vector or MathObjectMatrix 1 by n + c => undef, # coefficients for objective function Vector or MathObjectMatrix 1 by n + obj_row => undef, # contains the negative of the coefficients of the objective function. + z => undef, # value for objective function + n => undef, # dimension of problem variables (columns in A) + m => undef, # dimension of slack variables (rows in A) + S => undef, # square m by m matrix for slack variables + basis => undef, # list describing the current basis columns corresponding to determined variables. + B => undef, # square invertible matrix corresponding to the current basis columns + M => undef, # matrix of consisting of all columns and all rows except for the objective function row + obj_col_num => undef, + # flag indicating the column (1 or n+m+1) for the objective value + constraint_labels => undef, + problem_var_labels => undef, + slack_var_labels => undef, + +=item $self->current_tableau + Parameters: () + Returns: A MathObjectMatrix_tableau + +This represents the current version of the tableau + +=item $self->objective_row + Parameters: () + Returns: + +=item $self->basis + Parameter: ARRAY or ARRAY_ref or () + Returns: MathObject_list + + FiXME -- this should accept a MathObject_List (or MO_Set?) + +=head3 Package Tableau (eventually package Matrix?) + +=item $self->row_slice + + Parameter: @slice or \@slice + Return: MathObject matrix + +=item $self->extract_rows + + Parameter: @slice or \@slice + Return: two dimensional array ref + +=item extract_rows_to_list + + Parameter: @slice or \@slice + Return: MathObject List of row references + +=item $self->extract_columns + + Parameter: @slice or \@slice + Return: two dimensional array ref + +=item $self->column_slice + + Parameter: @slice or \@slice + Return: MathObject Matrix + +=item $self->extract_columns_to_list + + Parameter: @slice or \@slice + Return: MathObject List of Matrix references ? + +=item $self->submatrix + + Parameter:(rows=>\@row_slice,columns=>\@column_slice) + Return: MathObject matrix + + +=cut + + +sub _tableau_init {}; # don't reload this file +package main; + +sub matrix_column_slice{ + matrix_from_matrix_cols(@_); +} +sub matrix_from_matrix_cols { + my $M = shift; # a MathObject matrix_columns + my($n,$m) = $M->dimensions; + my @slice = @_; + if (ref($slice[0]) =~ /ARRAY/) { # handle array reference + @slice = @{$slice[0]}; + } + @slice = @slice?@slice : (1..$m); + my @columns = map {$M->column($_)->transpose->value} @slice; + #create the chosen columns as rows + # then transform to array_refs. + Matrix(@columns)->transpose; #transpose and return an n by m matrix (2 dim) +} +sub matrix_row_slice{ + matrix_from_matrix_rows(@_); +} + +sub matrix_from_matrix_rows { + my $M = shift; # a MathObject matrix_columns + my($n,$m) = $M->dimensions; + my @slice = @_; + if (ref($slice[0]) =~ /ARRAY/) { # handle array reference + @slice = @{$slice[0]}; + } + @slice = @slice? @slice : (1..$n); # the default is the whole matrix. + # DEBUG_MESSAGE("row slice in matrix from rows is @slice"); + my @rows = map {[$M->row($_)->value]} @slice; + #create the chosen columns as rows + # then transform to array_refs. + Matrix([@rows]); # insure that it is still an n by m matrix (2 dim) +} + +sub matrix_extract_submatrix { + matrix_from_submatrix(@_); +} +sub matrix_from_submatrix { + my $M=shift; + return undef unless ref($M) =~ /Value::Matrix/; + my %options = @_; + my($n,$m) = $M->dimensions; + my $row_slice = ($options{rows})?$options{rows}:[1..$m]; + my $col_slice = ($options{columns})?$options{columns}:[1..$n]; + # DEBUG_MESSAGE("ROW SLICE", join(" ", @$row_slice)); + # DEBUG_MESSAGE("COL SLICE", join(" ", @$col_slice)); + my $M1 = matrix_from_matrix_rows($M,@$row_slice); + # DEBUG_MESSAGE("M1 - matrix from rows) $M1"); + return matrix_from_matrix_cols($M1, @$col_slice); +} +sub matrix_extract_rows { + my $M =shift; + my @slice = @_; + if (ref($slice[0]) =~ /ARRAY/) { # handle array reference + @slice = @{$slice[0]}; + } elsif (@slice == 0) { # export all rows to List + @slice = ( 1..(($M->dimensions)[0]) ); + } + return map {$M->row($_)} @slice ; +} + +sub matrix_rows_to_list { + List(matrix_extract_rows(@_)); +} +sub matrix_columns_to_list { + List(matrix_extract_columns(@_) ); +} +sub matrix_extract_columns { + my $M =shift; # Add error checking + my @slice = @_; + if (ref($slice[0]) =~ /ARRAY/) { # handle array reference + @slice = @{$slice[0]}; + } elsif (@slice == 0) { # export all columns to an array + @slice = 1..($M->dimensions->[1]); + } + return map {$M->column($_)} @slice; +} + + + +######################## +############## +# get_tableau_variable_values +# +# Calculates the values of the basis variables of the tableau, +# assuming the parameter variables are 0. +# +# Usage: ARRAY = get_tableau_variable_values($MathObjectMatrix_tableau, $MathObjectSet_basis) +# +# feature request -- for tableau object -- allow specification of non-zero parameter variables +sub get_tableau_variable_values { + my $mat = shift; # a MathObject matrix + my $basis =shift; # a MathObject set + # FIXME + # type check ref($mat)='Matrix'; ref($basis)='Set'; + # or check that $mat has dimensions, element methods; and $basis has a contains method + my ($n, $m) = $mat->dimensions; + @var = (); + #DEBUG_MESSAGE( "start new matrix"); + foreach my $j (1..$m-2) { # the last two columns of the tableau are object variable and constants + if (not $basis->contains($j)) { + DEBUG_MESSAGE( "j= $j not in basis"); # set the parameter values to zero + $var[$j-1]=0; next; # non-basis variables (parameters) are set to 0. + + } else { + foreach my $i (1..$n-1) { # the last row is the objective function + # if this is a basis column there should be only one non-zero element(the pivot) + if ( $mat->element($i,$j)->value != 0 ) { # should this have ->value????? + $var[$j-1] = ($mat->element($i,$m)/($mat->element($i,$j))->value); + DEBUG_MESSAGE("i=$i j=$j var = $var[$j-1] "); # calculate basis variable value + next; + } + + } + } + } # element($n, $m-1) is the coefficient of the objective value. + # this last variable is the value of the objective function + push @var , ($mat->element($n,$m)/$mat->element($n,$m-1))->value; + + return wantarray ? @var : \@var; +} +#### Test -- assume matrix is this +# 1 2 1 0 0 | 0 | 3 +# 4 5 0 1 0 | 0 | 6 +# 7 8 0 0 1 | 0 | 9 +# -1 -2 0 0 0 | 1 | 10 # objective row +# and basis is {3,4,5} (start columns with 1) +# $n= 4; $m = 7 +# $x1=0; $x2=0; $x3=s1=3; $x4=s2=6; $x5=s3=9; w=10=objective value +# +# + +#################################### +# +# Cover for lp_pivot which allows us to use a set object for the new and old basis + +sub lp_basis_pivot { + my ($old_tableau,$old_basis,$pivot) = @_; # $pivot is a Value::Point + my $new_tableau= lp_clone($old_tableau); + # lp_pivot has 0 based indices + main::lp_pivot($new_tableau, $pivot->extract(1)-1,$pivot->extract(2)-1); + # lp_pivot pivots in place + my $new_matrix = Matrix($new_tableau); + my ($n,$m) = $new_matrix->dimensions; + my $param_size = $m-$n -1; #n=constraints+1, #m = $param_size + $constraints +2 + my $new_basis = ( $old_basis - ($pivot->extract(1)+$param_size) + ($pivot->extract(2)) )->sort; + my @statevars = get_tableau_variable_values($new_matrix, $new_basis); + return ( $new_tableau, Set($new_basis),\@statevars); + # force to set (from type Union) to insure that ->data is an array and not a string. +} + + + +sub linebreak_at_commas { + return sub { + my $ans=shift; + my $foo = $ans->{correct_ans_latex_string}; + $foo =~ s/,/,\\\\\\\\/g; + ($ans->{correct_ans_latex_string})=~ s/,/,\\\\\\\\/g; + ($ans->{preview_latex_string})=~ s/,/,\\\\\\\\/g; + #DEBUG_MESSAGE("foo", $foo); + #DEBUG_MESSAGE( "correct", $ans->{correct_ans_latex_string} ); + #DEBUG_MESSAGE( "preview", $ans->{preview_latex_string} ); + #DEBUG_MESSAGE("section4ans1 ", pretty_print($ans, $displayMode)); + $ans; + }; +} +# Useage +# $foochecker = $constraints->cmp()->withPostFilter( +# linebreak_at_commas() +# ); + + +### End gage_matrix_ops include + + + +################################################## +package Tableau; +our @ISA = qw(Value::Matrix Value); + +sub _Matrix { # can we just import this? + # this is a function, not a method + Value::Matrix->new(@_); +} + +sub new { + my $self = shift; my $class = ref($self) || $self; + my $context = (Value::isContext($_[0]) ? shift : $self->context); + my $tableau = { + A => undef, # constraint matrix MathObjectMatrix + b => undef, # constraint constants Vector or MathObjectMatrix 1 by n + c => undef, # coefficients for objective function Vector or MathObjectMatrix 1 by n + obj_row => undef, # contains the negative of the coefficients of the objective function. + z => undef, # value for objective function + n => undef, # dimension of problem variables (columns in A) + m => undef, # dimension of slack variables (rows in A) + S => undef, # square m by m matrix for slack variables + basis => undef, # list describing the current basis columns corresponding to determined variables. + B => undef, # square invertible matrix corresponding to the current basis columns + M => undef, # matrix of consisting of all columns and all rows except for the objective function row and column + obj_col_num => undef, # flag indicating the column (1 or n+m+1) for the objective value + constraint_labels => undef, + problem_var_labels => undef, + slack_var_labels => undef, + @_, + }; + bless $tableau, $class; + $tableau->initialize(); + return $tableau; +} + +# the following are used to construct the tableau +# initialize +# assemble_matrix +# objective_row +sub initialize { + $self= shift; + unless (ref($self->{A}) =~ /Value::Matrix/ && + ref($self->{b}) =~ /Value::Vector|Value::Matrix/ && + ref($self->{c}) =~ /Value::Vector|Value::Matrix/){ + main::WARN_MESSAGE("Error: Required inputs: Tableau(A=> Matrix, b=>Vector, c=>Vector)"); + return; + } + my ($m, $n)=($self->{A}->dimensions); + $self->{n}=$self->{n}//$n; + $self->{m}=$self->{m}//$m; + # main::DEBUG_MESSAGE("m $m, n $n"); + $self->{S} = Value::Matrix->I($m); + $self->{basis} = [($n+1)...($n+$m)] unless ref($self->{basis})=~/ARRAY/; + my @rows = $self->assemble_matrix; + #main::DEBUG_MESSAGE("rows", @rows); + $self->{M} = _Matrix([@rows]); + $self->{B} = $self->{M}->submatrix(rows=>[1..($self->{m})],columns=>$self->{basis}); + $self->{obj_row} = _Matrix($self->objective_row()); + return(); +} + +sub assemble_matrix { + my $self = shift; + my @rows =(); + my $m = $self->{m}; + my $n = $self->{n}; + foreach my $i (1..$m) { + my @current_row=(); + foreach my $j (1..$n) { + push @current_row, $self->{A}->element($i, $j); + } + foreach my $j (1..$m) { + push @current_row, $self->{S}->element($i,$j); # slack variables + } + push @current_row, 0, $self->{b}->data->[$i-1]; # obj column and constant column + push @rows, [@current_row]; + } + + return @rows; # these are the matrices A | S | obj | b + # the final row describing the objective function is not in this +} + +sub objective_row { + my $self = shift; + my @last_row=(); + push @last_row, ( -($self->{c}) )->value; + foreach my $i (1..($self->{m})) { push @last_row, 0 }; + push @last_row, 1, 0; + return \@last_row; +} + +# return a matrix containing the entire tableau +sub current_tableau { + my $Badj = ($self->{B}->det) * ($self->{B}->inverse); + my $current_tableau = $Badj * $self->{M}; # the A | S | obj | b + $self->{current_tableau}=$current_tableau; + # find the coefficients associated with the basis columns + my $c_B = $self->{obj_row}->extract_columns($self->{basis} ); + my $c_B2 = Value::Vector->new([ map {$_->value} @$c_B]); + my $correction_coeff = ($c_B2*$current_tableau )->row(1); + # subtract the correction coefficients from the obj_row + # this essentially extends Gauss reduction applied to the obj_row + my $obj_row_normalized = ($self->{B}->det) *$self->{obj_row}; + my $current_coeff = $obj_row_normalized-$correction_coeff ; + $self->{current_coeff}= $current_coeff; + + #main::DEBUG_MESSAGE("subtract these two ", (($self->{B}->det) *$self->{obj_row}), " | ", ($c_B*$current_tableau)->dimensions); + #main::DEBUG_MESSAGE("all coefficients", join('|', $self->{obj_row}->value ) ); + #main::DEBUG_MESSAGE("current coefficients", join('|', @current_coeff) ); + #main::DEBUG_MESSAGE("type of $self->{basis}", ref($self->{basis}) ); + #main::DEBUG_MESSAGE("current basis",join("|", @{$self->{basis}})); + #main::DEBUG_MESSAGE("CURRENT STATE ", $current_tableau); + return _Matrix( @{$current_tableau->extract_rows},$self->{current_coeff} ); + #return( $self->{current_coeff} ); +} + +sub basis { + my $self = shift; #update basis + my @input = @_; + return Value::List->new($self->{basis}) unless @input; #return basis if no input + my $new_basis; + if (ref( $input[0]) =~/ARRAY/) { + $new_basis=$input[0]; + } else { + $new_basis = \@input; + } + $self->{basis}= $new_basis; + $self->{B} = $self->{M}->submatrix(rows=>[1..($self->{m})],columns=>$self->{basis}); + return Value::List->new($self->{basis}); +} + + + + +package Value::Matrix; + +sub _Matrix { + Value::Matrix->new(@_); +} + +sub row_slice { + $self = shift; + @slice = @_; + return _Matrix( $self->extract_rows(@slice) ); +} +sub extract_rows { + $self = shift; + my @slice = @_; + if (ref($slice[0]) =~ /ARRAY/) { # handle array reference + @slice = @{$slice[0]}; + } elsif (@slice == 0) { # export all rows to List + @slice = ( 1..(($self->dimensions)[0]) ); + } + return [map {$self->row($_)} @slice ]; #prefer to pass references when possible +} +sub column_slice { + $self = shift; + return _Matrix( $self->extract_columns(@_) )->transpose; # matrix is built as rows then transposed. +} +sub extract_columns { + $self = shift; + my @slice = @_; + if (ref($slice[0]) =~ /ARRAY/) { # handle array reference + @slice = @{$slice[0]}; + } elsif (@slice == 0) { # export all columns to an array + @slice = ( 1..(($self->dimensions)[1] ) ); + } + return [map { $self->transpose->row($_) } @slice] ; + # returns the columns as an array of 1 by n row matrices containing values + # if you pull columns directly you get an array of 1 by n column vectors. + # prefer to pass references when possible +} +sub extract_rows_to_list { + my $self = shift; + Value::List->new($self->extract_rows(@_)); +} +sub extract_columns_to_list { + my $self = shift; + Value::List->new($self->extract_columns(@_) ); +} + +sub submatrix { + my $self = shift; + my %options = @_; + my($m,$n) = $self->dimensions; + my $row_slice = ($options{rows})?$options{rows}:[1..$m]; + my $col_slice = ($options{columns})?$options{columns}:[1..$n]; + return $self->row_slice($row_slice)->column_slice($col_slice); +} + + + +1; diff --git a/t/matrix_tableau_tests/matrix_test1.pg b/t/matrix_tableau_tests/matrix_test1.pg new file mode 100644 index 0000000000..c9b2c5f3bc --- /dev/null +++ b/t/matrix_tableau_tests/matrix_test1.pg @@ -0,0 +1,111 @@ +############################################## +DOCUMENT(); + +loadMacros( + "PGstandard.pl", # Standard macros for PG language + "MathObjects.pl", + "parserLinearInequality.pl", + "PGML.pl", + "tableau.pl", + "PGmatrixmacros.pl", + "LinearProgramming.pl", + #"source.pl", # allows code to be displayed on certain sites. + "PGcourse.pl", +); + +############################################## + +Context("Matrix"); # need Matrix context to allow string input into Matrix. + +#Construct a small test matrix. +$m = Matrix("[[3,6,7],[2,1,8],[4,6,21],[-6,7,9]]"); + + + $m_rows = $m->extract_rows([4,1]); #outputs an array reference + $m_cols = $m->extract_columns([3,2,1]); #outputs the columns as rows + $m1list = List($m_rows); + $m2list = List($m_cols); + $list_cols = $m->extract_columns_to_list(1,2); + $list_rows = $m->extract_rows_to_list(2,3); + + $m1matrix = Matrix($m_rows); + $m2matrix = Matrix($m_cols)->transpose; #matrix is built with rows and then needs to be transposed + + + $m3 = $m->row_slice([4,1]); + $m4 = $m->column_slice([3,2,1]); + + $submatrix1 = $m->submatrix(rows=>[2,3],columns=>[1,2]); + $submatrix2 = $m->submatrix(rows=>[2,3]); + $submatrix3 = $m->submatrix(columns=>[1,2]); + $submatrix4 = $m->submatrix(); + + +BEGIN_PGML + +Create a matrix: matrix m = [`[$m]`] + +Matrix indices start at 1. + +Extracting rows and columns from a matrix. +These are best displayed in PGML within a list. +It is best to place them in the list outside of PGML and then display. + +You can also group the extracted rows into a matrix and display. + +Matrix m = [`[$m]`] + +List rows 4,1 of the matrix m :[` [@ List($m_rows) @] `], + +or create the list before hand [`[$m1list]`] + +or matrix version [`[$m1matrix]`] + +You can do the same for columns. Notice that this does not +do exactly what you expect when you extract columns + +matrix m = [`[$m]`] + +List columns 3,2,1 of the matrix m: [@ List($m_cols) @] + +create the list before hand [`[$m2list]`] + +or create matrix version [`[$m2matrix]`] + + +Using the \[@ ... @\] escape to apply List doesn't +always do the best job +of interacting with TeX output as you can see in the first entries for List above. + +The two entries below show what happens if you extract rows or +columns into lists using +the method $m->extract_rows_to_list(). +It is the same as applying List() to the method +$m->extract_rows outside of PGML. + +matrix: [`[$m]`] + +Find the list of columns 1,2 of the matrix [`[$list_cols]`] + +Find the list of rows 2,3 of the matrix [`[$list_rows]`] + +The next two entries illustrate the $m->row_slice, $m->column_slice methods for shuffling or +selecting rows or columns from a matrix. The return type is Value::Matrix. The column +selection is done by doing row selection on the transpose -- this does what you expect +when selecting columns. + +Row slice (4,1) [`[$m3]`] + +Column slice (3,2,1) [`[$m4]`] + +This final group selects a rectangular submatrix of a matrix. ->submatrix. + +Select \[2,3\]x\[1,2\] to get [`[$submatrix1]`] of [`[$m]`] + +Select \[2,3\]x all to get [`[$submatrix2]`] + +Select all x \[1,2\] to get [`[$submatrix3]`] + +END_PGML + +ENDDOCUMENT(); \ No newline at end of file diff --git a/t/matrix_tableau_tests/tableau_test1.pg b/t/matrix_tableau_tests/tableau_test1.pg new file mode 100644 index 0000000000..cc0a503fe7 --- /dev/null +++ b/t/matrix_tableau_tests/tableau_test1.pg @@ -0,0 +1,104 @@ + + + +############################################## +DOCUMENT(); + +loadMacros( + "PGstandard.pl", # Standard macros for PG language + "MathObjects.pl", + "parserLinearInequality.pl", + "PGML.pl", + "tableau.pl", + "PGmatrixmacros.pl", + "LinearProgramming.pl", + #"source.pl", # allows code to be displayed on certain sites. + "PGcourse.pl", +); + +############################################## + +Context("Matrix"); # need Matrix context to allow string input into Matrix. + +$m = Matrix("[[3,6,7],[2,1,8],[4,6,21],[-6,7,9]]"); +$constraint_matrix = Matrix(" +[[ 0, 0, -1, -1], + [-1, -1, 0, 0 ], + [1, 0 , 1 , 0], + [0, 1, 0, 1]] +"); + +#TEXT ("created ". ref($m)); +#what are the best ways to display a matrix? + +$m1 = $m->row_slice([4,1]); +$m2 = $m->column_slice([3,2,1]); + +$list = $m->extract_rows_to_list(2,3); + +$b = Matrix([1, 2, 3, 4]); +#TEXT($BR, "vector", $b->data->[1]); +$c = Matrix([5, 6, 7]); +$t = Tableau->new(A=>$m,b=>$b, c=>$c); + +$basis2 = $t->basis(1,3,4,6); +$t->current_tableau; + +$c_B = $t->{obj_row}->extract_columns($t->{basis} ); #basis coefficients +$c_B2 = Value::Vector->new(map {$_->value} @$c_B); +$c_4 = $t->current_tableau; + + +my $Badj = ($t->{B}->det) * ($t->{B}->inverse); +my $current_tableau = $Badj * $t->{M}; # the A | S | obj | b + +$correction_coeff = ($c_B2*$current_tableau)->row(1); +$obj_row_normalized = ($t->{B}->det) *$t->{obj_row}; +$current_coeff = $obj_row_normalized-$correction_coeff ; + +TEXT("obj_row ", $t->{obj_row}, $BR ); +TEXT("c_b is", @$c_B,$BR); +TEXT("c_b2 is", $c_B2,$BR); +TEXT("current coeff ", List($current_coeff),$BR); +BEGIN_PGML +matrix is [`[$m]`] + +b is [$b] + +and c is [$c] + +original tableau is [`[$t->{M}]`] + +and basis is [$t->basis] + +B is [`[$t->{B}]`] with determinant [$t->{B}->det] + +the objective row is [@ $t->{obj_row} @] + + +the coefficients associated with the basis are [@ List(@$c_B) @] + +the vector version of these coefficients is [$c_B2] + +the normalized objective row is [@ List($obj_row_normalized ) @] + +The correction coeff are [@ List($correction_coeff) @] + +The current coeff are [@ List($current_coeff) @] + +Print the current total tableau for the basis [@ Matrix($t->{basis}) @]: + +$t->current_tableau [`[$c_4]`] + +Here is the decorated version of the total tableau: + +[`[@ lp_display_mm($c_4, top_labels=>[qw(x1 x2 x3 x4 x5 x6 x7 w b)], +side_labels=>['\text{constraintA}', '', '\text{constraintC}', '\text{constraintD}', +'\text{objective_function}'])@]`] + + +END_PGML + +TEXT("array reference", pretty_print( convert_to_array_ref($c_4) )); +ENDDOCUMENT(); + From 66acc9ad421f4ade22f479bcb11f3acdd0f5efff Mon Sep 17 00:00:00 2001 From: Michael Gage Date: Sat, 7 Oct 2017 20:15:29 -0400 Subject: [PATCH 02/30] commit test files --- t/embedded_files/analytics.js | 45 + t/embedded_files/css | 24 + t/embedded_files/embed-light.css | 259 + t/embedded_files/embed.js | 184 + t/embedded_files/highlight.pack.js | 2 + t/embedded_files/jquery-1.9.1.js | 9597 ++++++++++++++++ t/embedded_files/jquery-ui.css | 474 + t/embedded_files/jquery-ui.js | 14912 +++++++++++++++++++++++++ t/embedded_files/result-light.css | 0 t/embedded_files/saved_resource.html | 170 + 10 files changed, 25667 insertions(+) create mode 100644 t/embedded_files/analytics.js create mode 100644 t/embedded_files/css create mode 100644 t/embedded_files/embed-light.css create mode 100644 t/embedded_files/embed.js create mode 100644 t/embedded_files/highlight.pack.js create mode 100644 t/embedded_files/jquery-1.9.1.js create mode 100644 t/embedded_files/jquery-ui.css create mode 100644 t/embedded_files/jquery-ui.js create mode 100644 t/embedded_files/result-light.css create mode 100644 t/embedded_files/saved_resource.html diff --git a/t/embedded_files/analytics.js b/t/embedded_files/analytics.js new file mode 100644 index 0000000000..92ec834ff2 --- /dev/null +++ b/t/embedded_files/analytics.js @@ -0,0 +1,45 @@ +(function(){var $c=function(a){this.w=a||[]};$c.prototype.set=function(a){this.w[a]=!0};$c.prototype.encode=function(){for(var a=[],b=0;b\x3c/script>')):(c=M.createElement("script"),c.type="text/javascript",c.async=!0,c.src=a,b&&(c.id=b),a=M.getElementsByTagName("script")[0],a.parentNode.insertBefore(c,a)))},Ud=function(){return"https:"==M.location.protocol},E=function(a,b){var c= +a.match("(?:&|#|\\?)"+K(b).replace(/([.*+?^=!:${}()|\[\]\/\\])/g,"\\$1")+"=([^&#]*)");return c&&2==c.length?c[1]:""},xa=function(){var a=""+M.location.hostname;return 0==a.indexOf("www.")?a.substring(4):a},ya=function(a){var b=M.referrer;if(/^https?:\/\//i.test(b)){if(a)return b;a="//"+M.location.hostname;var c=b.indexOf(a);if(5==c||6==c)if(a=b.charAt(c+a.length),"/"==a||"?"==a||""==a||":"==a)return;return b}},za=function(a,b){if(1==b.length&&null!=b[0]&&"object"===typeof b[0])return b[0];for(var c= +{},d=Math.min(a.length+1,b.length),e=0;e=b.length)wc(a,b,c);else if(8192>=b.length)x(a,b,c)||wd(a,b,c)||wc(a,b,c);else throw ge("len",b.length),new Da(b.length);},wc=function(a,b,c){var d=ta(a+"?"+b);d.onload=d.onerror=function(){d.onload=null;d.onerror=null;c()}},wd=function(a,b,c){var d=O.XMLHttpRequest;if(!d)return!1;var e=new d;if(!("withCredentials"in e))return!1; +e.open("POST",a,!0);e.withCredentials=!0;e.setRequestHeader("Content-Type","text/plain");e.onreadystatechange=function(){4==e.readyState&&(c(),e=null)};e.send(b);return!0},x=function(a,b,c){return O.navigator.sendBeacon?O.navigator.sendBeacon(a,b)?(c(),!0):!1:!1},ge=function(a,b,c){1<=100*Math.random()||G("?")||(a=["t=error","_e="+a,"_v=j47","sr=1"],b&&a.push("_f="+b),c&&a.push("_m="+K(c.substring(0,100))),a.push("aip=1"),a.push("z="+hd()),wc(oc()+"/collect",a.join("&"),ua))};var h=function(a){var b=O.gaData=O.gaData||{};return b[a]=b[a]||{}};var Ha=function(){this.M=[]};Ha.prototype.add=function(a){this.M.push(a)};Ha.prototype.D=function(a){try{for(var b=0;b=100*R(a,Ka))throw"abort";}function Ma(a){if(G(P(a,Na)))throw"abort";}function Oa(){var a=M.location.protocol;if("http:"!=a&&"https:"!=a)throw"abort";} +function Pa(a){try{O.navigator.sendBeacon?J(42):O.XMLHttpRequest&&"withCredentials"in new O.XMLHttpRequest&&J(40)}catch(c){}a.set(ld,Td(a),!0);a.set(Ac,R(a,Ac)+1);var b=[];Qa.map(function(c,d){if(d.F){var e=a.get(c);void 0!=e&&e!=d.defaultValue&&("boolean"==typeof e&&(e*=1),b.push(d.F+"="+K(""+e)))}});b.push("z="+Bd());a.set(Ra,b.join("&"),!0)} +function Sa(a){var b=P(a,gd)||oc()+"/collect",c=P(a,fa);!c&&a.get(Vd)&&(c="beacon");if(c){var d=P(a,Ra),e=a.get(Ia),e=e||ua;"image"==c?wc(b,d,e):"xhr"==c&&wd(b,d,e)||"beacon"==c&&x(b,d,e)||ba(b,d,e)}else ba(b,P(a,Ra),a.get(Ia));b=a.get(Na);b=h(b);c=b.hitcount;b.hitcount=c?c+1:1;b=a.get(Na);delete h(b).pending_experiments;a.set(Ia,ua,!0)} +function Hc(a){(O.gaData=O.gaData||{}).expId&&a.set(Nc,(O.gaData=O.gaData||{}).expId);(O.gaData=O.gaData||{}).expVar&&a.set(Oc,(O.gaData=O.gaData||{}).expVar);var b;var c=a.get(Na);if(c=h(c).pending_experiments){var d=[];for(b in c)c.hasOwnProperty(b)&&c[b]&&d.push(encodeURIComponent(b)+"."+encodeURIComponent(c[b]));b=d.join("!")}else b=void 0;b&&a.set(m,b,!0)}function cd(){if(O.navigator&&"preview"==O.navigator.loadPurpose)throw"abort";} +function yd(a){var b=O.gaDevIds;ka(b)&&0!=b.length&&a.set("&did",b.join(","),!0)}function vb(a){if(!a.get(Na))throw"abort";};var hd=function(){return Math.round(2147483647*Math.random())},Bd=function(){try{var a=new Uint32Array(1);O.crypto.getRandomValues(a);return a[0]&2147483647}catch(b){return hd()}};function Ta(a){var b=R(a,Ua);500<=b&&J(15);var c=P(a,Va);if("transaction"!=c&&"item"!=c){var c=R(a,Wa),d=(new Date).getTime(),e=R(a,Xa);0==e&&a.set(Xa,d);e=Math.round(2*(d-e)/1E3);0=c)throw"abort";a.set(Wa,--c)}a.set(Ua,++b)};var Ya=function(){this.data=new ee},Qa=new ee,Za=[];Ya.prototype.get=function(a){var b=$a(a),c=this.data.get(a);b&&void 0==c&&(c=ea(b.defaultValue)?b.defaultValue():b.defaultValue);return b&&b.Z?b.Z(this,a,c):c};var P=function(a,b){var c=a.get(b);return void 0==c?"":""+c},R=function(a,b){var c=a.get(b);return void 0==c||""===c?0:1*c};Ya.prototype.set=function(a,b,c){if(a)if("object"==typeof a)for(var d in a)a.hasOwnProperty(d)&&ab(this,d,a[d],c);else ab(this,a,b,c)}; +var ab=function(a,b,c,d){if(void 0!=c)switch(b){case Na:wb.test(c)}var e=$a(b);e&&e.o?e.o(a,b,c,d):a.data.set(b,c,d)},bb=function(a,b,c,d,e){this.name=a;this.F=b;this.Z=d;this.o=e;this.defaultValue=c},$a=function(a){var b=Qa.get(a);if(!b)for(var c=0;c=b?!1:!0},gc=function(a){var b={};if(Ec(b)||Fc(b)){var c=b[Eb];void 0==c||Infinity==c||isNaN(c)||(0c)a[b]=void 0},Fd=function(a){return function(b){if("pageview"==b.get(Va)&&!a.I){a.I=!0;var c= +aa(b);b=0=a&&d.push({hash:ca[0],R:e[g],O:ca})}if(0!=d.length)return 1==d.length?d[0]:Zc(b,d)||Zc(c,d)||Zc(null,d)||d[0]}function Zc(a,b){var c,d;null==a?c=d=1:(c=La(a),d=La(D(a,".")?a.substring(1):"."+a));for(var e=0;ed.length)){c=[];for(var e=0;e=ca[0]||0>=ca[1]?"":ca.join("x");a.set(rb,c);a.set(tb,fc());a.set(ob,M.characterSet||M.charset);a.set(sb,b&&"function"=== +typeof b.javaEnabled&&b.javaEnabled()||!1);a.set(nb,(b&&(b.language||b.browserLanguage)||"").toLowerCase());if(d&&a.get(cc)&&(b=M.location.hash)){b=b.split(/[?&#]+/);d=[];for(c=0;carguments.length)){var b,c;"string"===typeof arguments[0]?(b=arguments[0],c=[].slice.call(arguments,1)):(b=arguments[0]&&arguments[0][Va],c=arguments);b&&(c=za(qc[b]||[],c),c[Va]=b,this.b.set(c,void 0,!0),this.filters.D(this.b),this.b.data.m={})}}; +pc.prototype.ma=function(a,b){var c=this;u(a,c,b)||(v(a,function(){u(a,c,b)}),y(String(c.get(V)),a,void 0,b,!0))};var rc=function(a){if("prerender"==M.visibilityState)return!1;a();return!0},z=function(a){if(!rc(a)){J(16);var b=!1,c=function(){if(!b&&rc(a)){b=!0;var d=c,e=M;e.removeEventListener?e.removeEventListener("visibilitychange",d,!1):e.detachEvent&&e.detachEvent("onvisibilitychange",d)}};L(M,"visibilitychange",c)}};var td=/^(?:(\w+)\.)?(?:(\w+):)?(\w+)$/,sc=function(a){if(ea(a[0]))this.u=a[0];else{var b=td.exec(a[0]);null!=b&&4==b.length&&(this.c=b[1]||"t0",this.K=b[2]||"",this.C=b[3],this.a=[].slice.call(a,1),this.K||(this.A="create"==this.C,this.i="require"==this.C,this.g="provide"==this.C,this.ba="remove"==this.C),this.i&&(3<=this.a.length?(this.X=this.a[1],this.W=this.a[2]):this.a[1]&&(qa(this.a[1])?this.X=this.a[1]:this.W=this.a[1])));b=a[1];a=a[2];if(!this.C)throw"abort";if(this.i&&(!qa(b)||""==b))throw"abort"; +if(this.g&&(!qa(b)||""==b||!ea(a)))throw"abort";if(ud(this.c)||ud(this.K))throw"abort";if(this.g&&"t0"!=this.c)throw"abort";}};function ud(a){return 0<=a.indexOf(".")||0<=a.indexOf(":")};var Yd,Zd,$d,A;Yd=new ee;$d=new ee;A=new ee;Zd={ec:45,ecommerce:46,linkid:47}; +var u=function(a,b,c){b==N||b.get(V);var d=Yd.get(a);if(!ea(d))return!1;b.plugins_=b.plugins_||new ee;if(b.plugins_.get(a))return!0;b.plugins_.set(a,new d(b,c||{}));return!0},y=function(a,b,c,d,e){if(!ea(Yd.get(b))&&!$d.get(b)){Zd.hasOwnProperty(b)&&J(Zd[b]);if(p.test(b)){J(52);a=N.j(a);if(!a)return!0;c=d||{};d={id:b,B:c.dataLayer||"dataLayer",ia:!!a.get("anonymizeIp"),na:e,G:!1};a.get(">m")==b&&(d.G=!0);var g=String(a.get("name"));"t0"!=g&&(d.target=g);G(String(a.get("trackingId")))||(d.ja=String(a.get(Q)), +d.ka=Number(a.get(n)),a=c.palindrome?r:q,a=(a=M.cookie.replace(/^|(; +)/g,";").match(a))?a.sort().join("").substring(1):void 0,d.la=a);a=d.B;c=(new Date).getTime();O[a]=O[a]||[];c={"gtm.start":c};e||(c.event="gtm.js");O[a].push(c);c=t(d)}!c&&Zd.hasOwnProperty(b)?(J(39),c=b+".js"):J(43);c&&(c&&0<=c.indexOf("/")||(c=(Ba||Ud()?"https:":"http:")+"//www.google-analytics.com/plugins/ua/"+c),d=ae(c),a=d.protocol,c=M.location.protocol,("https:"==a||a==c||("http:"!=a?0:"http:"==c))&&B(d)&&(wa(d.url,void 0, +e),$d.set(b,!0)))}},v=function(a,b){var c=A.get(a)||[];c.push(b);A.set(a,c)},C=function(a,b){Yd.set(a,b);for(var c=A.get(a)||[],d=0;da.split("/")[0].indexOf(":")&&(a=ca+e[2].substring(0,e[2].lastIndexOf("/"))+"/"+ +a);c.href=a;d=b(c);return{protocol:(c.protocol||"").toLowerCase(),host:d[0],port:d[1],path:d[2],query:c.search||"",url:a||""}};var Z={ga:function(){Z.f=[]}};Z.ga();Z.D=function(a){var b=Z.J.apply(Z,arguments),b=Z.f.concat(b);for(Z.f=[];0c;c++){var d=b[c].src;if(d&&0==d.indexOf("https://www.google-analytics.com/analytics")){J(33); +b=!0;break a}}b=!1}b&&(Ba=!0)}Ud()||Ba||!Ed(new Od(1E4))||(J(36),Ba=!0);(O.gaplugins=O.gaplugins||{}).Linker=Dc;b=Dc.prototype;C("linker",Dc);X("decorate",b,b.ca,20);X("autoLink",b,b.S,25);C("displayfeatures",fd);C("adfeatures",fd);a=a&&a.q;ka(a)?Z.D.apply(N,a):J(50)}};N.da=function(){for(var a=N.getAll(),b=0;b>21:b;return b};})(window); diff --git a/t/embedded_files/css b/t/embedded_files/css new file mode 100644 index 0000000000..a946605790 --- /dev/null +++ b/t/embedded_files/css @@ -0,0 +1,24 @@ +/* vietnamese */ +@font-face { + font-family: 'Inconsolata'; + font-style: normal; + font-weight: 400; + src: local('Inconsolata Regular'), local('Inconsolata-Regular'), url(https://fonts.gstatic.com/s/inconsolata/v15/BjAYBlHtW3CJxDcjzrnZCNDiNsR5a-9Oe_Ivpu8XWlY.woff2) format('woff2'); + unicode-range: U+0102-0103, U+1EA0-1EF9, U+20AB; +} +/* latin-ext */ +@font-face { + font-family: 'Inconsolata'; + font-style: normal; + font-weight: 400; + src: local('Inconsolata Regular'), local('Inconsolata-Regular'), url(https://fonts.gstatic.com/s/inconsolata/v15/BjAYBlHtW3CJxDcjzrnZCKE8kM4xWR1_1bYURRojRGc.woff2) format('woff2'); + unicode-range: U+0100-024F, U+1E00-1EFF, U+20A0-20AB, U+20AD-20CF, U+2C60-2C7F, U+A720-A7FF; +} +/* latin */ +@font-face { + font-family: 'Inconsolata'; + font-style: normal; + font-weight: 400; + src: local('Inconsolata Regular'), local('Inconsolata-Regular'), url(https://fonts.gstatic.com/s/inconsolata/v15/BjAYBlHtW3CJxDcjzrnZCIgp9Q8gbYrhqGlRav_IXfk.woff2) format('woff2'); + unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2212, U+2215, U+E0FF, U+EFFD, U+F000; +} diff --git a/t/embedded_files/embed-light.css b/t/embedded_files/embed-light.css new file mode 100644 index 0000000000..4b28bdcf94 --- /dev/null +++ b/t/embedded_files/embed-light.css @@ -0,0 +1,259 @@ +@-webkit-keyframes rotate { + from { + -webkit-transform: rotate(0deg); + -moz-transform: rotate(0deg); + -ms-transform: rotate(0deg); + transform: rotate(0deg); } + to { + -webkit-transform: rotate(360deg); + -moz-transform: rotate(360deg); + -ms-transform: rotate(360deg); + transform: rotate(360deg); } } + +*, html, body, button, input, textarea, select { + -webkit-font-smoothing: antialiased; + -moz-osx-font-smoothing: grayscale; } + +.CodeMirror * { + -moz-osx-font-smoothing: auto; } + +a { + color: #39464E; + text-decoration: none; } + a:hover { + text-decoration: underline; } + +input, textarea, select { + font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Fira Sans", "Droid Sans", "Helvetica Neue", Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; } + +select { + cursor: pointer; } + +body, div, dl, dt, dd, ul, ol, li, h1, h2, h3, h4, h5, h6, pre, form, fieldset, input, textarea, p, blockquote, th, td { + margin: 0; + padding: 0; } + +table { + border-collapse: collapse; + border-spacing: 0; } + +fieldset, img { + border: 0; } + +address, caption, cite, code, dfn, em, strong, th, var { + font-style: normal; + font-weight: normal; } + +ol, ul { + list-style: none; } + +caption, th { + text-align: left; } + +h1, h2, h3, h4, h5, h6 { + font-size: 100%; + font-weight: normal; } + +abbr, acronym { + border: 0; } + +::-webkit-input-placeholder { + color: #abb9c2; } + +:-moz-placeholder { + /* Firefox 18- */ + color: #abb9c2; } + +::-moz-placeholder { + /* Firefox 19+ */ + color: #abb9c2; } + +:-ms-input-placeholder { + color: #abb9c2; } + +.errorlist { + font-size: .85em; } + .errorlist li { + margin-bottom: 5px; + color: #f36e65; } + +html { + height: 100vh; } + +body { + font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Fira Sans", "Droid Sans", "Helvetica Neue", Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; + overflow: hidden; + padding: 0; + margin: 0; + position: relative; + font-size: 14px; + color: #39464E; } + +a:hover { + text-decoration: none; } + +#wrapper { + position: relative; } + +header { + height: 50px; + box-shadow: 0 0 5px rgba(57, 70, 78, 0.2); + position: relative; + z-index: 100; + position: fixed; + top: 0; + left: 0; + right: 0; } + header h1 { + float: right; + height: 50px; + margin: 0 0 0 10px; } + header h1 a { + display: block; + height: 50px; + line-height: 50px; + color: #7f94a1; + font-size: 12px; + padding-right: 45px; + margin-right: 10px; + background: url(/img/embeddable/logo-dark.png) 100% 50% no-repeat; } + @media only screen and (-webkit-min-device-pixel-ratio: 2), only screen and (min--moz-device-pixel-ratio: 2), only screen and (-o-min-device-pixel-ratio: 2 / 1), only screen and (min-resolution: 192dpi), only screen and (min-resolution: 2dppx) { + header h1 a { + background: transparent url(/img/embeddable/logo-dark@2x.png) no-repeat 100% 50%; + background-size: 37px; } } + header h1 a:hover { + text-decoration: underline; } + +#actions li { + float: left; } + #actions li a { + display: block; + padding: 0 10px; + line-height: 50px; + height: 50px; } + +#actions .hl { + height: 3px; + width: 0; + background: #1C90F3; + position: absolute; + bottom: 0; + left: 0; } + #actions .hl.animated { + -moz-transition: all 0.15s; + /* FF3.6+ */ + -webkit-transition: all 0.15s; + /* Chrome, Safari */ + -o-transition: all 0.15s; + /* Opera */ + -ms-transition: all 0.15s; + /* IE 9 */ + transition: all 0.15s; } + +#tabs { + margin-top: 50px; + overflow: auto; + height: calc(100vh - 50px); } + #tabs .tCont { + padding: 10px; + display: none; } + #tabs .tCont.active { + display: block; } + #tabs pre { + background: transparent; + font-family: "Inconsolata", "Monaco", "Andale Mono", "Lucida Console", "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; + line-height: 1.55em; + font-size: 14px; } + #tabs #result { + padding: 0; + height: calc(100vh - 50px); } + #tabs #result iframe { + width: 100%; + height: 100%; + border: none; + display: block; + margin: 0; } + +#resources h3 { + font-size: 11px; + color: #7f94a1; + line-height: 23px; + text-transform: uppercase; + letter-spacing: 1px; } + +#resources ul { + border-top: solid 1px #cfd6d9; } + #resources ul li { + border-bottom: solid 1px #cfd6d9; + padding: 7px 0; } + #resources ul a { + color: #1C90F3; } + +/* + +JSFiddle Light (c) Oskar Krawczyk + +*/ +.hljs-comment, +.hljs-quote { + color: #abb8c6; } + +.hljs-variable, +.hljs-template-variable, +.hljs-regexp, +.hljs-deletion, +.hljs-keyword, +.hljs-selector-id, +.hljs-selector-class, +.hljs-selector-tag, +.hljs-attr { + color: #e38800; } + +.hljs-built_in, +.hljs-builtin-name, +.hljs-literal, +.hljs-type, +.hljs-params, +.hljs-meta, +.hljs-link { + color: #8d44eb; } + +.hljs-selector-id, +.hljs-selector-class, +.hljs-selector-tag, +.hljs-attr { + color: #e77600; } + +.hljs-attribute { + color: #108de8; } + +.hljs-string, +.hljs-symbol, +.hljs-bullet, +.hljs-addition { + color: #2AAB51; } + +.hljs-subst, +.hljs-number { + color: #ED6E55; } + +.hljs-title, +.hljs-section, +.hljs-name { + color: #fa3d58; } + +.hljs { + display: block; + overflow-x: auto; } + +.hljs-emphasis { + font-style: italic; } + +.hljs-strong { + font-weight: bold; } + +header { + background-color: #ffffff; } + +body { + background-color: #f3f5f6; } diff --git a/t/embedded_files/embed.js b/t/embedded_files/embed.js new file mode 100644 index 0000000000..776ee00229 --- /dev/null +++ b/t/embedded_files/embed.js @@ -0,0 +1,184 @@ +(function() { + var Embed, Utils, + bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, + extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, + hasProp = {}.hasOwnProperty; + + Utils = (function() { + function Utils() {} + + Utils.prototype.eachElement = function(array, callback, scope) { + var i, results; + if (scope == null) { + scope = this; + } + i = 0; + results = []; + while (i < array.length) { + callback.call(scope, array[i], i); + results.push(i++); + } + return results; + }; + + Utils.prototype.pushMessage = function(name, value) { + if (value == null) { + value = {}; + } + return window.parent.postMessage([name, value], "*"); + }; + + Utils.prototype.addEvent = function(element, event, fn, useCapture) { + if (useCapture == null) { + useCapture = false; + } + return element.addEventListener(event, fn, useCapture); + }; + + Utils.prototype.setStyles = function(element, styles) { + var key, results; + results = []; + for (key in styles) { + results.push(element.style[key] = styles[key]); + } + return results; + }; + + return Utils; + + })(); + + Embed = (function(superClass) { + extend(Embed, superClass); + + function Embed() { + this.switchTab = bind(this.switchTab, this); + this.loadResult = bind(this.loadResult, this); + this.repositionHighlight = bind(this.repositionHighlight, this); + this.predender = bind(this.predender, this); + this.setHeight = bind(this.setHeight, this); + this.setupEvents = bind(this.setupEvents, this); + this.setupDefaults = bind(this.setupDefaults, this); + this.elements = { + tabs: document.querySelectorAll("#tabs .tCont"), + actions: document.querySelectorAll("#actions a"), + hl: document.querySelector(".hl"), + pre: document.querySelectorAll("pre") + }; + this.setupDefaults(); + } + + Embed.prototype.setupDefaults = function() { + this.setupEvents(); + this.eachElement(this.elements.pre, function(element) { + return hljs.highlightBlock(element); + }); + this.repositionHighlight(this.elements.actions[0], false); + return this.setHeight(this.elements.tabs[0]); + }; + + Embed.prototype.setupEvents = function() { + this.eachElement(this.elements.actions, (function(_this) { + return function(action, index) { + return action.addEventListener("click", function(event) { + return _this.switchTab(event, action, index); + }); + }; + })(this)); + if (this.elements.actions[0].dataset.triggerType === "result") { + return this.loadResult(); + } + }; + + Embed.prototype.setHeight = function(element) { + var activeTab, height; + activeTab = element.getBoundingClientRect(); + height = activeTab.height; + return this.pushMessage("embed", { + slug: slug, + height: height + }); + }; + + Embed.prototype.predender = function() { + var head, prefetch, prerender; + head = document.getElementsByTagName("head")[0]; + prefetch = document.createElement("link"); + prefetch.setAttribute("rel", "prefetch"); + prefetch.setAttribute("href", show_src); + head.appendChild(prefetch); + prerender = document.createElement("link"); + prerender.setAttribute("rel", "prerender"); + prerender.setAttribute("href", show_src); + return head.appendChild(prerender); + }; + + Embed.prototype.repositionHighlight = function(action, animated) { + var position; + if (animated == null) { + animated = true; + } + position = action.getBoundingClientRect(); + if (animated) { + this.elements.hl.classList.add("animated"); + } + return this.setStyles(this.elements.hl, { + left: position.left + "px", + width: position.width + "px" + }); + }; + + Embed.prototype.loadResult = function(callback) { + var iframes, resultCont, resultsFrame; + iframes = document.querySelectorAll("#result iframe"); + resultsFrame = document.createElement("iframe"); + resultCont = document.querySelector("#result"); + this.eachElement(iframes, function(iframe) { + return iframe.parentNode.removeChild(iframe); + }); + resultsFrame.src = show_src; + resultsFrame.allowtransparency = true; + resultsFrame.allowfullscreen = true; + resultsFrame.frameBorder = "0"; + resultsFrame.sandbox = "allow-modals allow-forms allow-popups allow-scripts allow-same-origin"; + resultCont.appendChild(resultsFrame); + if (callback) { + return resultsFrame.addEventListener("load", (function(_this) { + return function() { + return callback.apply([_this]); + }; + })(this)); + } + }; + + Embed.prototype.switchTab = function(event, action, index) { + var actionParent; + event.preventDefault(); + event.stopPropagation(); + this.repositionHighlight(action); + actionParent = action.parentElement.parentElement.querySelectorAll("li"); + this.eachElement(this.elements.tabs, function(element) { + return element.classList.remove("active"); + }); + this.elements.tabs[index].classList.add("active"); + if (actionParent) { + this.eachElement(actionParent, function(element) { + return element.classList.remove("active"); + }); + action.parentElement.classList.add("active"); + } + this.setHeight(this.elements.tabs[index]); + if (action.dataset.triggerType === "result") { + return this.loadResult(); + } + }; + + return Embed; + + })(Utils); + + window.addEventListener("DOMContentLoaded", function(event) { + return this.EmbedManager = new Embed; + }); + +}).call(this); diff --git a/t/embedded_files/highlight.pack.js b/t/embedded_files/highlight.pack.js new file mode 100644 index 0000000000..b417075b44 --- /dev/null +++ b/t/embedded_files/highlight.pack.js @@ -0,0 +1,2 @@ +/*! highlight.js v9.0.0 | BSD3 License | git.io/hljslicense */ +!function(e){"undefined"!=typeof exports?e(exports):(self.hljs=e({}),"function"==typeof define&&define.amd&&define("hljs",[],function(){return self.hljs}))}(function(e){function n(e){return e.replace(/&/gm,"&").replace(//gm,">")}function t(e){return e.nodeName.toLowerCase()}function r(e,n){var t=e&&e.exec(n);return t&&0==t.index}function a(e){return/^(no-?highlight|plain|text)$/i.test(e)}function i(e){var n,t,r,i=e.className+" ";if(i+=e.parentNode?e.parentNode.className:"",t=/\blang(?:uage)?-([\w-]+)\b/i.exec(i))return E(t[1])?t[1]:"no-highlight";for(i=i.split(/\s+/),n=0,r=i.length;r>n;n++)if(E(i[n])||a(i[n]))return i[n]}function o(e,n){var t,r={};for(t in e)r[t]=e[t];if(n)for(t in n)r[t]=n[t];return r}function u(e){var n=[];return function r(e,a){for(var i=e.firstChild;i;i=i.nextSibling)3==i.nodeType?a+=i.nodeValue.length:1==i.nodeType&&(n.push({event:"start",offset:a,node:i}),a=r(i,a),t(i).match(/br|hr|img|input/)||n.push({event:"stop",offset:a,node:i}));return a}(e,0),n}function c(e,r,a){function i(){return e.length&&r.length?e[0].offset!=r[0].offset?e[0].offset"}function u(e){l+=""}function c(e){("start"==e.event?o:u)(e.node)}for(var s=0,l="",f=[];e.length||r.length;){var g=i();if(l+=n(a.substr(s,g[0].offset-s)),s=g[0].offset,g==e){f.reverse().forEach(u);do c(g.splice(0,1)[0]),g=i();while(g==e&&g.length&&g[0].offset==s);f.reverse().forEach(o)}else"start"==g[0].event?f.push(g[0].node):f.pop(),c(g.splice(0,1)[0])}return l+n(a.substr(s))}function s(e){function n(e){return e&&e.source||e}function t(t,r){return new RegExp(n(t),"m"+(e.cI?"i":"")+(r?"g":""))}function r(a,i){if(!a.compiled){if(a.compiled=!0,a.k=a.k||a.bK,a.k){var u={},c=function(n,t){e.cI&&(t=t.toLowerCase()),t.split(" ").forEach(function(e){var t=e.split("|");u[t[0]]=[n,t[1]?Number(t[1]):1]})};"string"==typeof a.k?c("keyword",a.k):Object.keys(a.k).forEach(function(e){c(e,a.k[e])}),a.k=u}a.lR=t(a.l||/\b\w+\b/,!0),i&&(a.bK&&(a.b="\\b("+a.bK.split(" ").join("|")+")\\b"),a.b||(a.b=/\B|\b/),a.bR=t(a.b),a.e||a.eW||(a.e=/\B|\b/),a.e&&(a.eR=t(a.e)),a.tE=n(a.e)||"",a.eW&&i.tE&&(a.tE+=(a.e?"|":"")+i.tE)),a.i&&(a.iR=t(a.i)),void 0===a.r&&(a.r=1),a.c||(a.c=[]);var s=[];a.c.forEach(function(e){e.v?e.v.forEach(function(n){s.push(o(e,n))}):s.push("self"==e?a:e)}),a.c=s,a.c.forEach(function(e){r(e,a)}),a.starts&&r(a.starts,i);var l=a.c.map(function(e){return e.bK?"\\.?("+e.b+")\\.?":e.b}).concat([a.tE,a.i]).map(n).filter(Boolean);a.t=l.length?t(l.join("|"),!0):{exec:function(){return null}}}}r(e)}function l(e,t,a,i){function o(e,n){for(var t=0;t";return i+=e+'">',i+n+o}function p(){if(!L.k)return n(M);var e="",t=0;L.lR.lastIndex=0;for(var r=L.lR.exec(M);r;){e+=n(M.substr(t,r.index-t));var a=g(L,r);a?(B+=a[1],e+=h(a[0],n(r[0]))):e+=n(r[0]),t=L.lR.lastIndex,r=L.lR.exec(M)}return e+n(M.substr(t))}function d(){var e="string"==typeof L.sL;if(e&&!R[L.sL])return n(M);var t=e?l(L.sL,M,!0,y[L.sL]):f(M,L.sL.length?L.sL:void 0);return L.r>0&&(B+=t.r),e&&(y[L.sL]=t.top),h(t.language,t.value,!1,!0)}function b(){return void 0!==L.sL?d():p()}function v(e,t){var r=e.cN?h(e.cN,"",!0):"";e.rB?(k+=r,M=""):e.eB?(k+=n(t)+r,M=""):(k+=r,M=t),L=Object.create(e,{parent:{value:L}})}function m(e,t){if(M+=e,void 0===t)return k+=b(),0;var r=o(t,L);if(r)return k+=b(),v(r,t),r.rB?0:t.length;var a=u(L,t);if(a){var i=L;i.rE||i.eE||(M+=t),k+=b();do L.cN&&(k+=""),B+=L.r,L=L.parent;while(L!=a.parent);return i.eE&&(k+=n(t)),M="",a.starts&&v(a.starts,""),i.rE?0:t.length}if(c(t,L))throw new Error('Illegal lexeme "'+t+'" for mode "'+(L.cN||"")+'"');return M+=t,t.length||1}var N=E(e);if(!N)throw new Error('Unknown language: "'+e+'"');s(N);var w,L=i||N,y={},k="";for(w=L;w!=N;w=w.parent)w.cN&&(k=h(w.cN,"",!0)+k);var M="",B=0;try{for(var C,j,I=0;;){if(L.t.lastIndex=I,C=L.t.exec(t),!C)break;j=m(t.substr(I,C.index-I),C[0]),I=C.index+j}for(m(t.substr(I)),w=L;w.parent;w=w.parent)w.cN&&(k+="");return{r:B,value:k,language:e,top:L}}catch(O){if(-1!=O.message.indexOf("Illegal"))return{r:0,value:n(t)};throw O}}function f(e,t){t=t||x.languages||Object.keys(R);var r={r:0,value:n(e)},a=r;return t.forEach(function(n){if(E(n)){var t=l(n,e,!1);t.language=n,t.r>a.r&&(a=t),t.r>r.r&&(a=r,r=t)}}),a.language&&(r.second_best=a),r}function g(e){return x.tabReplace&&(e=e.replace(/^((<[^>]+>|\t)+)/gm,function(e,n){return n.replace(/\t/g,x.tabReplace)})),x.useBR&&(e=e.replace(/\n/g,"
")),e}function h(e,n,t){var r=n?w[n]:t,a=[e.trim()];return e.match(/\bhljs\b/)||a.push("hljs"),-1===e.indexOf(r)&&a.push(r),a.join(" ").trim()}function p(e){var n=i(e);if(!a(n)){var t;x.useBR?(t=document.createElementNS("http://www.w3.org/1999/xhtml","div"),t.innerHTML=e.innerHTML.replace(/\n/g,"").replace(//g,"\n")):t=e;var r=t.textContent,o=n?l(n,r,!0):f(r),s=u(t);if(s.length){var p=document.createElementNS("http://www.w3.org/1999/xhtml","div");p.innerHTML=o.value,o.value=c(s,u(p),r)}o.value=g(o.value),e.innerHTML=o.value,e.className=h(e.className,n,o.language),e.result={language:o.language,re:o.r},o.second_best&&(e.second_best={language:o.second_best.language,re:o.second_best.r})}}function d(e){x=o(x,e)}function b(){if(!b.called){b.called=!0;var e=document.querySelectorAll("pre code");Array.prototype.forEach.call(e,p)}}function v(){addEventListener("DOMContentLoaded",b,!1),addEventListener("load",b,!1)}function m(n,t){var r=R[n]=t(e);r.aliases&&r.aliases.forEach(function(e){w[e]=n})}function N(){return Object.keys(R)}function E(e){return e=(e||"").toLowerCase(),R[e]||R[w[e]]}var x={classPrefix:"hljs-",tabReplace:null,useBR:!1,languages:void 0},R={},w={};return e.highlight=l,e.highlightAuto=f,e.fixMarkup=g,e.highlightBlock=p,e.configure=d,e.initHighlighting=b,e.initHighlightingOnLoad=v,e.registerLanguage=m,e.listLanguages=N,e.getLanguage=E,e.inherit=o,e.IR="[a-zA-Z]\\w*",e.UIR="[a-zA-Z_]\\w*",e.NR="\\b\\d+(\\.\\d+)?",e.CNR="(-?)(\\b0[xX][a-fA-F0-9]+|(\\b\\d+(\\.\\d*)?|\\.\\d+)([eE][-+]?\\d+)?)",e.BNR="\\b(0b[01]+)",e.RSR="!|!=|!==|%|%=|&|&&|&=|\\*|\\*=|\\+|\\+=|,|-|-=|/=|/|:|;|<<|<<=|<=|<|===|==|=|>>>=|>>=|>=|>>>|>>|>|\\?|\\[|\\{|\\(|\\^|\\^=|\\||\\|=|\\|\\||~",e.BE={b:"\\\\[\\s\\S]",r:0},e.ASM={cN:"string",b:"'",e:"'",i:"\\n",c:[e.BE]},e.QSM={cN:"string",b:'"',e:'"',i:"\\n",c:[e.BE]},e.PWM={b:/\b(a|an|the|are|I|I'm|isn't|don't|doesn't|won't|but|just|should|pretty|simply|enough|gonna|going|wtf|so|such|will|you|your|like)\b/},e.C=function(n,t,r){var a=e.inherit({cN:"comment",b:n,e:t,c:[]},r||{});return a.c.push(e.PWM),a.c.push({cN:"doctag",b:"(?:TODO|FIXME|NOTE|BUG|XXX):",r:0}),a},e.CLCM=e.C("//","$"),e.CBCM=e.C("/\\*","\\*/"),e.HCM=e.C("#","$"),e.NM={cN:"number",b:e.NR,r:0},e.CNM={cN:"number",b:e.CNR,r:0},e.BNM={cN:"number",b:e.BNR,r:0},e.CSSNM={cN:"number",b:e.NR+"(%|em|ex|ch|rem|vw|vh|vmin|vmax|cm|mm|in|pt|pc|px|deg|grad|rad|turn|s|ms|Hz|kHz|dpi|dpcm|dppx)?",r:0},e.RM={cN:"regexp",b:/\//,e:/\/[gimuy]*/,i:/\n/,c:[e.BE,{b:/\[/,e:/\]/,r:0,c:[e.BE]}]},e.TM={cN:"title",b:e.IR,r:0},e.UTM={cN:"title",b:e.UIR,r:0},e});hljs.registerLanguage("xml",function(s){var t="[A-Za-z0-9\\._:-]+",e={b:/<\?(php)?(?!\w)/,e:/\?>/,sL:"php"},r={eW:!0,i:/]+/}]}]}]};return{aliases:["html","xhtml","rss","atom","xsl","plist"],cI:!0,c:[{cN:"meta",b:"",r:10,c:[{b:"\\[",e:"\\]"}]},s.C("",{r:10}),{b:"<\\!\\[CDATA\\[",e:"\\]\\]>",r:10},{cN:"tag",b:"|$)",e:">",k:{name:"style"},c:[r],starts:{e:"",rE:!0,sL:["css","xml"]}},{cN:"tag",b:"|$)",e:">",k:{name:"script"},c:[r],starts:{e:"",rE:!0,sL:["actionscript","javascript","handlebars","xml"]}},e,{cN:"meta",b:/<\?\w+/,e:/\?>/,r:10},{cN:"tag",b:"",c:[{cN:"name",b:/[^\/><\s]+/,r:0},r]}]}});hljs.registerLanguage("coffeescript",function(e){var c={keyword:"in if for while finally new do return else break catch instanceof throw try this switch continue typeof delete debugger super then unless until loop of by when and or is isnt not",literal:"true false null undefined yes no on off",built_in:"npm require console print module global window document"},n="[A-Za-z$_][0-9A-Za-z$_]*",r={cN:"subst",b:/#\{/,e:/}/,k:c},s=[e.BNM,e.inherit(e.CNM,{starts:{e:"(\\s*/)?",r:0}}),{cN:"string",v:[{b:/'''/,e:/'''/,c:[e.BE]},{b:/'/,e:/'/,c:[e.BE]},{b:/"""/,e:/"""/,c:[e.BE,r]},{b:/"/,e:/"/,c:[e.BE,r]}]},{cN:"regexp",v:[{b:"///",e:"///",c:[r,e.HCM]},{b:"//[gim]*",r:0},{b:/\/(?![ *])(\\\/|.)*?\/[gim]*(?=\W|$)/}]},{b:"@"+n},{b:"`",e:"`",eB:!0,eE:!0,sL:"javascript"}];r.c=s;var i=e.inherit(e.TM,{b:n}),t="(\\(.*\\))?\\s*\\B[-=]>",o={cN:"params",b:"\\([^\\(]",rB:!0,c:[{b:/\(/,e:/\)/,k:c,c:["self"].concat(s)}]};return{aliases:["coffee","cson","iced"],k:c,i:/\/\*/,c:s.concat([e.C("###","###"),e.HCM,{cN:"function",b:"^\\s*"+n+"\\s*=\\s*"+t,e:"[-=]>",rB:!0,c:[i,o]},{b:/[:\(,=]\s*/,r:0,c:[{cN:"function",b:t,e:"[-=]>",rB:!0,c:[o]}]},{cN:"class",bK:"class",e:"$",i:/[:="\[\]]/,c:[{bK:"extends",eW:!0,i:/[:="\[\]]/,c:[i]},i]},{b:n+":",e:":",rB:!0,rE:!0,r:0}])}});hljs.registerLanguage("scss",function(e){var t="[a-zA-Z-][a-zA-Z0-9_-]*",i={cN:"variable",b:"(\\$"+t+")\\b"},r={cN:"number",b:"#[0-9A-Fa-f]+"};({cN:"attribute",b:"[A-Z\\_\\.\\-]+",e:":",eE:!0,i:"[^\\s]",starts:{eW:!0,eE:!0,c:[r,e.CSSNM,e.QSM,e.ASM,e.CBCM,{cN:"meta",b:"!important"}]}});return{cI:!0,i:"[=/|']",c:[e.CLCM,e.CBCM,{cN:"selector-id",b:"\\#[A-Za-z0-9_-]+",r:0},{cN:"selector-class",b:"\\.[A-Za-z0-9_-]+",r:0},{cN:"selector-attr",b:"\\[",e:"\\]",i:"$"},{cN:"selector-tag",b:"\\b(a|abbr|acronym|address|area|article|aside|audio|b|base|big|blockquote|body|br|button|canvas|caption|cite|code|col|colgroup|command|datalist|dd|del|details|dfn|div|dl|dt|em|embed|fieldset|figcaption|figure|footer|form|frame|frameset|(h[1-6])|head|header|hgroup|hr|html|i|iframe|img|input|ins|kbd|keygen|label|legend|li|link|map|mark|meta|meter|nav|noframes|noscript|object|ol|optgroup|option|output|p|param|pre|progress|q|rp|rt|ruby|samp|script|section|select|small|span|strike|strong|style|sub|sup|table|tbody|td|textarea|tfoot|th|thead|time|title|tr|tt|ul|var|video)\\b",r:0},{b:":(visited|valid|root|right|required|read-write|read-only|out-range|optional|only-of-type|only-child|nth-of-type|nth-last-of-type|nth-last-child|nth-child|not|link|left|last-of-type|last-child|lang|invalid|indeterminate|in-range|hover|focus|first-of-type|first-line|first-letter|first-child|first|enabled|empty|disabled|default|checked|before|after|active)"},{b:"::(after|before|choices|first-letter|first-line|repeat-index|repeat-item|selection|value)"},i,{cN:"attribute",b:"\\b(z-index|word-wrap|word-spacing|word-break|width|widows|white-space|visibility|vertical-align|unicode-bidi|transition-timing-function|transition-property|transition-duration|transition-delay|transition|transform-style|transform-origin|transform|top|text-underline-position|text-transform|text-shadow|text-rendering|text-overflow|text-indent|text-decoration-style|text-decoration-line|text-decoration-color|text-decoration|text-align-last|text-align|tab-size|table-layout|right|resize|quotes|position|pointer-events|perspective-origin|perspective|page-break-inside|page-break-before|page-break-after|padding-top|padding-right|padding-left|padding-bottom|padding|overflow-y|overflow-x|overflow-wrap|overflow|outline-width|outline-style|outline-offset|outline-color|outline|orphans|order|opacity|object-position|object-fit|normal|none|nav-up|nav-right|nav-left|nav-index|nav-down|min-width|min-height|max-width|max-height|mask|marks|margin-top|margin-right|margin-left|margin-bottom|margin|list-style-type|list-style-position|list-style-image|list-style|line-height|letter-spacing|left|justify-content|initial|inherit|ime-mode|image-orientation|image-resolution|image-rendering|icon|hyphens|height|font-weight|font-variant-ligatures|font-variant|font-style|font-stretch|font-size-adjust|font-size|font-language-override|font-kerning|font-feature-settings|font-family|font|float|flex-wrap|flex-shrink|flex-grow|flex-flow|flex-direction|flex-basis|flex|filter|empty-cells|display|direction|cursor|counter-reset|counter-increment|content|column-width|column-span|column-rule-width|column-rule-style|column-rule-color|column-rule|column-gap|column-fill|column-count|columns|color|clip-path|clip|clear|caption-side|break-inside|break-before|break-after|box-sizing|box-shadow|box-decoration-break|bottom|border-width|border-top-width|border-top-style|border-top-right-radius|border-top-left-radius|border-top-color|border-top|border-style|border-spacing|border-right-width|border-right-style|border-right-color|border-right|border-radius|border-left-width|border-left-style|border-left-color|border-left|border-image-width|border-image-source|border-image-slice|border-image-repeat|border-image-outset|border-image|border-color|border-collapse|border-bottom-width|border-bottom-style|border-bottom-right-radius|border-bottom-left-radius|border-bottom-color|border-bottom|border|background-size|background-repeat|background-position|background-origin|background-image|background-color|background-clip|background-attachment|background-blend-mode|background|backface-visibility|auto|animation-timing-function|animation-play-state|animation-name|animation-iteration-count|animation-fill-mode|animation-duration|animation-direction|animation-delay|animation|align-self|align-items|align-content)\\b",i:"[^\\s]"},{b:"\\b(whitespace|wait|w-resize|visible|vertical-text|vertical-ideographic|uppercase|upper-roman|upper-alpha|underline|transparent|top|thin|thick|text|text-top|text-bottom|tb-rl|table-header-group|table-footer-group|sw-resize|super|strict|static|square|solid|small-caps|separate|se-resize|scroll|s-resize|rtl|row-resize|ridge|right|repeat|repeat-y|repeat-x|relative|progress|pointer|overline|outside|outset|oblique|nowrap|not-allowed|normal|none|nw-resize|no-repeat|no-drop|newspaper|ne-resize|n-resize|move|middle|medium|ltr|lr-tb|lowercase|lower-roman|lower-alpha|loose|list-item|line|line-through|line-edge|lighter|left|keep-all|justify|italic|inter-word|inter-ideograph|inside|inset|inline|inline-block|inherit|inactive|ideograph-space|ideograph-parenthesis|ideograph-numeric|ideograph-alpha|horizontal|hidden|help|hand|groove|fixed|ellipsis|e-resize|double|dotted|distribute|distribute-space|distribute-letter|distribute-all-lines|disc|disabled|default|decimal|dashed|crosshair|collapse|col-resize|circle|char|center|capitalize|break-word|break-all|bottom|both|bolder|bold|block|bidi-override|below|baseline|auto|always|all-scroll|absolute|table|table-cell)\\b"},{b:":",e:";",c:[i,r,e.CSSNM,e.QSM,e.ASM,{cN:"meta",b:"!important"}]},{b:"@",e:"[{;]",k:"mixin include extend for if else each while charset import debug media page content font-face namespace warn",c:[i,e.QSM,e.ASM,r,e.CSSNM,{b:"\\s[A-Za-z0-9_.-]+",r:0}]}]}});hljs.registerLanguage("typescript",function(e){var r={keyword:"in if for while finally var new function do return void else break catch instanceof with throw case default try this switch continue typeof delete let yield const class public private protected get set super static implements enum export import declare type namespace abstract",literal:"true false null undefined NaN Infinity",built_in:"eval isFinite isNaN parseFloat parseInt decodeURI decodeURIComponent encodeURI encodeURIComponent escape unescape Object Function Boolean Error EvalError InternalError RangeError ReferenceError StopIteration SyntaxError TypeError URIError Number Math Date String RegExp Array Float32Array Float64Array Int16Array Int32Array Int8Array Uint16Array Uint32Array Uint8Array Uint8ClampedArray ArrayBuffer DataView JSON Intl arguments require module console window document any number boolean string void"};return{aliases:["ts"],k:r,c:[{cN:"meta",b:/^\s*['"]use strict['"]/},e.ASM,e.QSM,{cN:"string",b:"`",e:"`",c:[e.BE,{cN:"subst",b:"\\$\\{",e:"\\}"}]},e.CLCM,e.CBCM,{cN:"number",v:[{b:"\\b(0[bB][01]+)"},{b:"\\b(0[oO][0-7]+)"},{b:e.CNR}],r:0},{b:"("+e.RSR+"|\\b(case|return|throw)\\b)\\s*",k:"return throw case",c:[e.CLCM,e.CBCM,e.RM],r:0},{cN:"function",b:"function",e:/[\{;]/,eE:!0,k:r,c:["self",e.inherit(e.TM,{b:/[A-Za-z$_][0-9A-Za-z$_]*/}),{cN:"params",b:/\(/,e:/\)/,eB:!0,eE:!0,k:r,c:[e.CLCM,e.CBCM],i:/["'\(]/}],i:/\[|%/,r:0},{bK:"constructor",e:/\{/,eE:!0},{bK:"module",e:/\{/,eE:!0},{bK:"interface",e:/\{/,eE:!0,k:"interface extends"},{b:/\$[(.]/},{b:"\\."+e.IR,r:0}]}});hljs.registerLanguage("css",function(e){var c="[a-zA-Z-][a-zA-Z0-9_-]*",t={b:/[A-Z\_\.\-]+\s*:/,rB:!0,e:";",eW:!0,c:[{cN:"attribute",b:/\S/,e:":",eE:!0,starts:{eW:!0,eE:!0,c:[{b:/[\w-]+\s*\(/,rB:!0,c:[{cN:"built_in",b:/[\w-]+/}]},e.CSSNM,e.QSM,e.ASM,e.CBCM,{cN:"number",b:"#[0-9A-Fa-f]+"},{cN:"meta",b:"!important"}]}}]};return{cI:!0,i:/[=\/|'\$]/,c:[e.CBCM,{cN:"selector-id",b:/#[A-Za-z0-9_-]+/},{cN:"selector-class",b:/\.[A-Za-z0-9_-]+/},{cN:"selector-attr",b:/\[/,e:/\]/,i:"$"},{cN:"selector-pseudo",b:/:(:)?[a-zA-Z0-9\_\-\+\(\)"']+/},{b:"@(font-face|page)",l:"[a-z-]+",k:"font-face page"},{b:"@",e:"[{;]",c:[{cN:"keyword",b:/\S+/},{b:/\s/,eW:!0,eE:!0,r:0,c:[e.ASM,e.QSM,e.CSSNM]}]},{cN:"selector-tag",b:c,r:0},{b:"{",e:"}",i:/\S/,c:[e.CBCM,t]}]}});hljs.registerLanguage("javascript",function(e){return{aliases:["js"],k:{keyword:"in of if for while finally var new function do return void else break catch instanceof with throw case default try this switch continue typeof delete let yield const export super debugger as async await import from as",literal:"true false null undefined NaN Infinity",built_in:"eval isFinite isNaN parseFloat parseInt decodeURI decodeURIComponent encodeURI encodeURIComponent escape unescape Object Function Boolean Error EvalError InternalError RangeError ReferenceError StopIteration SyntaxError TypeError URIError Number Math Date String RegExp Array Float32Array Float64Array Int16Array Int32Array Int8Array Uint16Array Uint32Array Uint8Array Uint8ClampedArray ArrayBuffer DataView JSON Intl arguments require module console window document Symbol Set Map WeakSet WeakMap Proxy Reflect Promise"},c:[{cN:"meta",r:10,b:/^\s*['"]use (strict|asm)['"]/},e.ASM,e.QSM,{cN:"string",b:"`",e:"`",c:[e.BE,{cN:"subst",b:"\\$\\{",e:"\\}"}]},e.CLCM,e.CBCM,{cN:"number",v:[{b:"\\b(0[bB][01]+)"},{b:"\\b(0[oO][0-7]+)"},{b:e.CNR}],r:0},{b:"("+e.RSR+"|\\b(case|return|throw)\\b)\\s*",k:"return throw case",c:[e.CLCM,e.CBCM,e.RM,{b:/\s*[);\]]/,r:0,sL:"xml"}],r:0},{cN:"function",bK:"function",e:/\{/,eE:!0,c:[e.inherit(e.TM,{b:/[A-Za-z$_][0-9A-Za-z$_]*/}),{cN:"params",b:/\(/,e:/\)/,eB:!0,eE:!0,c:[e.CLCM,e.CBCM]}],i:/\[|%/},{b:/\$[(.]/},{b:"\\."+e.IR,r:0},{cN:"class",bK:"class",e:/[{;=]/,eE:!0,i:/[:"\[\]]/,c:[{bK:"extends"},e.UTM]},{bK:"constructor",e:/\{/,eE:!0}],i:/#/}}); \ No newline at end of file diff --git a/t/embedded_files/jquery-1.9.1.js b/t/embedded_files/jquery-1.9.1.js new file mode 100644 index 0000000000..e2c203fe97 --- /dev/null +++ b/t/embedded_files/jquery-1.9.1.js @@ -0,0 +1,9597 @@ +/*! + * jQuery JavaScript Library v1.9.1 + * http://jquery.com/ + * + * Includes Sizzle.js + * http://sizzlejs.com/ + * + * Copyright 2005, 2012 jQuery Foundation, Inc. and other contributors + * Released under the MIT license + * http://jquery.org/license + * + * Date: 2013-2-4 + */ +(function( window, undefined ) { + +// Can't do this because several apps including ASP.NET trace +// the stack via arguments.caller.callee and Firefox dies if +// you try to trace through "use strict" call chains. (#13335) +// Support: Firefox 18+ +//"use strict"; +var + // The deferred used on DOM ready + readyList, + + // A central reference to the root jQuery(document) + rootjQuery, + + // Support: IE<9 + // For `typeof node.method` instead of `node.method !== undefined` + core_strundefined = typeof undefined, + + // Use the correct document accordingly with window argument (sandbox) + document = window.document, + location = window.location, + + // Map over jQuery in case of overwrite + _jQuery = window.jQuery, + + // Map over the $ in case of overwrite + _$ = window.$, + + // [[Class]] -> type pairs + class2type = {}, + + // List of deleted data cache ids, so we can reuse them + core_deletedIds = [], + + core_version = "1.9.1", + + // Save a reference to some core methods + core_concat = core_deletedIds.concat, + core_push = core_deletedIds.push, + core_slice = core_deletedIds.slice, + core_indexOf = core_deletedIds.indexOf, + core_toString = class2type.toString, + core_hasOwn = class2type.hasOwnProperty, + core_trim = core_version.trim, + + // Define a local copy of jQuery + jQuery = function( selector, context ) { + // The jQuery object is actually just the init constructor 'enhanced' + return new jQuery.fn.init( selector, context, rootjQuery ); + }, + + // Used for matching numbers + core_pnum = /[+-]?(?:\d*\.|)\d+(?:[eE][+-]?\d+|)/.source, + + // Used for splitting on whitespace + core_rnotwhite = /\S+/g, + + // Make sure we trim BOM and NBSP (here's looking at you, Safari 5.0 and IE) + rtrim = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, + + // A simple way to check for HTML strings + // Prioritize #id over to avoid XSS via location.hash (#9521) + // Strict HTML recognition (#11290: must start with <) + rquickExpr = /^(?:(<[\w\W]+>)[^>]*|#([\w-]*))$/, + + // Match a standalone tag + rsingleTag = /^<(\w+)\s*\/?>(?:<\/\1>|)$/, + + // JSON RegExp + rvalidchars = /^[\],:{}\s]*$/, + rvalidbraces = /(?:^|:|,)(?:\s*\[)+/g, + rvalidescape = /\\(?:["\\\/bfnrt]|u[\da-fA-F]{4})/g, + rvalidtokens = /"[^"\\\r\n]*"|true|false|null|-?(?:\d+\.|)\d+(?:[eE][+-]?\d+|)/g, + + // Matches dashed string for camelizing + rmsPrefix = /^-ms-/, + rdashAlpha = /-([\da-z])/gi, + + // Used by jQuery.camelCase as callback to replace() + fcamelCase = function( all, letter ) { + return letter.toUpperCase(); + }, + + // The ready event handler + completed = function( event ) { + + // readyState === "complete" is good enough for us to call the dom ready in oldIE + if ( document.addEventListener || event.type === "load" || document.readyState === "complete" ) { + detach(); + jQuery.ready(); + } + }, + // Clean-up method for dom ready events + detach = function() { + if ( document.addEventListener ) { + document.removeEventListener( "DOMContentLoaded", completed, false ); + window.removeEventListener( "load", completed, false ); + + } else { + document.detachEvent( "onreadystatechange", completed ); + window.detachEvent( "onload", completed ); + } + }; + +jQuery.fn = jQuery.prototype = { + // The current version of jQuery being used + jquery: core_version, + + constructor: jQuery, + init: function( selector, context, rootjQuery ) { + var match, elem; + + // HANDLE: $(""), $(null), $(undefined), $(false) + if ( !selector ) { + return this; + } + + // Handle HTML strings + if ( typeof selector === "string" ) { + if ( selector.charAt(0) === "<" && selector.charAt( selector.length - 1 ) === ">" && selector.length >= 3 ) { + // Assume that strings that start and end with <> are HTML and skip the regex check + match = [ null, selector, null ]; + + } else { + match = rquickExpr.exec( selector ); + } + + // Match html or make sure no context is specified for #id + if ( match && (match[1] || !context) ) { + + // HANDLE: $(html) -> $(array) + if ( match[1] ) { + context = context instanceof jQuery ? context[0] : context; + + // scripts is true for back-compat + jQuery.merge( this, jQuery.parseHTML( + match[1], + context && context.nodeType ? context.ownerDocument || context : document, + true + ) ); + + // HANDLE: $(html, props) + if ( rsingleTag.test( match[1] ) && jQuery.isPlainObject( context ) ) { + for ( match in context ) { + // Properties of context are called as methods if possible + if ( jQuery.isFunction( this[ match ] ) ) { + this[ match ]( context[ match ] ); + + // ...and otherwise set as attributes + } else { + this.attr( match, context[ match ] ); + } + } + } + + return this; + + // HANDLE: $(#id) + } else { + elem = document.getElementById( match[2] ); + + // Check parentNode to catch when Blackberry 4.6 returns + // nodes that are no longer in the document #6963 + if ( elem && elem.parentNode ) { + // Handle the case where IE and Opera return items + // by name instead of ID + if ( elem.id !== match[2] ) { + return rootjQuery.find( selector ); + } + + // Otherwise, we inject the element directly into the jQuery object + this.length = 1; + this[0] = elem; + } + + this.context = document; + this.selector = selector; + return this; + } + + // HANDLE: $(expr, $(...)) + } else if ( !context || context.jquery ) { + return ( context || rootjQuery ).find( selector ); + + // HANDLE: $(expr, context) + // (which is just equivalent to: $(context).find(expr) + } else { + return this.constructor( context ).find( selector ); + } + + // HANDLE: $(DOMElement) + } else if ( selector.nodeType ) { + this.context = this[0] = selector; + this.length = 1; + return this; + + // HANDLE: $(function) + // Shortcut for document ready + } else if ( jQuery.isFunction( selector ) ) { + return rootjQuery.ready( selector ); + } + + if ( selector.selector !== undefined ) { + this.selector = selector.selector; + this.context = selector.context; + } + + return jQuery.makeArray( selector, this ); + }, + + // Start with an empty selector + selector: "", + + // The default length of a jQuery object is 0 + length: 0, + + // The number of elements contained in the matched element set + size: function() { + return this.length; + }, + + toArray: function() { + return core_slice.call( this ); + }, + + // Get the Nth element in the matched element set OR + // Get the whole matched element set as a clean array + get: function( num ) { + return num == null ? + + // Return a 'clean' array + this.toArray() : + + // Return just the object + ( num < 0 ? this[ this.length + num ] : this[ num ] ); + }, + + // Take an array of elements and push it onto the stack + // (returning the new matched element set) + pushStack: function( elems ) { + + // Build a new jQuery matched element set + var ret = jQuery.merge( this.constructor(), elems ); + + // Add the old object onto the stack (as a reference) + ret.prevObject = this; + ret.context = this.context; + + // Return the newly-formed element set + return ret; + }, + + // Execute a callback for every element in the matched set. + // (You can seed the arguments with an array of args, but this is + // only used internally.) + each: function( callback, args ) { + return jQuery.each( this, callback, args ); + }, + + ready: function( fn ) { + // Add the callback + jQuery.ready.promise().done( fn ); + + return this; + }, + + slice: function() { + return this.pushStack( core_slice.apply( this, arguments ) ); + }, + + first: function() { + return this.eq( 0 ); + }, + + last: function() { + return this.eq( -1 ); + }, + + eq: function( i ) { + var len = this.length, + j = +i + ( i < 0 ? len : 0 ); + return this.pushStack( j >= 0 && j < len ? [ this[j] ] : [] ); + }, + + map: function( callback ) { + return this.pushStack( jQuery.map(this, function( elem, i ) { + return callback.call( elem, i, elem ); + })); + }, + + end: function() { + return this.prevObject || this.constructor(null); + }, + + // For internal use only. + // Behaves like an Array's method, not like a jQuery method. + push: core_push, + sort: [].sort, + splice: [].splice +}; + +// Give the init function the jQuery prototype for later instantiation +jQuery.fn.init.prototype = jQuery.fn; + +jQuery.extend = jQuery.fn.extend = function() { + var src, copyIsArray, copy, name, options, clone, + target = arguments[0] || {}, + i = 1, + length = arguments.length, + deep = false; + + // Handle a deep copy situation + if ( typeof target === "boolean" ) { + deep = target; + target = arguments[1] || {}; + // skip the boolean and the target + i = 2; + } + + // Handle case when target is a string or something (possible in deep copy) + if ( typeof target !== "object" && !jQuery.isFunction(target) ) { + target = {}; + } + + // extend jQuery itself if only one argument is passed + if ( length === i ) { + target = this; + --i; + } + + for ( ; i < length; i++ ) { + // Only deal with non-null/undefined values + if ( (options = arguments[ i ]) != null ) { + // Extend the base object + for ( name in options ) { + src = target[ name ]; + copy = options[ name ]; + + // Prevent never-ending loop + if ( target === copy ) { + continue; + } + + // Recurse if we're merging plain objects or arrays + if ( deep && copy && ( jQuery.isPlainObject(copy) || (copyIsArray = jQuery.isArray(copy)) ) ) { + if ( copyIsArray ) { + copyIsArray = false; + clone = src && jQuery.isArray(src) ? src : []; + + } else { + clone = src && jQuery.isPlainObject(src) ? src : {}; + } + + // Never move original objects, clone them + target[ name ] = jQuery.extend( deep, clone, copy ); + + // Don't bring in undefined values + } else if ( copy !== undefined ) { + target[ name ] = copy; + } + } + } + } + + // Return the modified object + return target; +}; + +jQuery.extend({ + noConflict: function( deep ) { + if ( window.$ === jQuery ) { + window.$ = _$; + } + + if ( deep && window.jQuery === jQuery ) { + window.jQuery = _jQuery; + } + + return jQuery; + }, + + // Is the DOM ready to be used? Set to true once it occurs. + isReady: false, + + // A counter to track how many items to wait for before + // the ready event fires. See #6781 + readyWait: 1, + + // Hold (or release) the ready event + holdReady: function( hold ) { + if ( hold ) { + jQuery.readyWait++; + } else { + jQuery.ready( true ); + } + }, + + // Handle when the DOM is ready + ready: function( wait ) { + + // Abort if there are pending holds or we're already ready + if ( wait === true ? --jQuery.readyWait : jQuery.isReady ) { + return; + } + + // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443). + if ( !document.body ) { + return setTimeout( jQuery.ready ); + } + + // Remember that the DOM is ready + jQuery.isReady = true; + + // If a normal DOM Ready event fired, decrement, and wait if need be + if ( wait !== true && --jQuery.readyWait > 0 ) { + return; + } + + // If there are functions bound, to execute + readyList.resolveWith( document, [ jQuery ] ); + + // Trigger any bound ready events + if ( jQuery.fn.trigger ) { + jQuery( document ).trigger("ready").off("ready"); + } + }, + + // See test/unit/core.js for details concerning isFunction. + // Since version 1.3, DOM methods and functions like alert + // aren't supported. They return false on IE (#2968). + isFunction: function( obj ) { + return jQuery.type(obj) === "function"; + }, + + isArray: Array.isArray || function( obj ) { + return jQuery.type(obj) === "array"; + }, + + isWindow: function( obj ) { + return obj != null && obj == obj.window; + }, + + isNumeric: function( obj ) { + return !isNaN( parseFloat(obj) ) && isFinite( obj ); + }, + + type: function( obj ) { + if ( obj == null ) { + return String( obj ); + } + return typeof obj === "object" || typeof obj === "function" ? + class2type[ core_toString.call(obj) ] || "object" : + typeof obj; + }, + + isPlainObject: function( obj ) { + // Must be an Object. + // Because of IE, we also have to check the presence of the constructor property. + // Make sure that DOM nodes and window objects don't pass through, as well + if ( !obj || jQuery.type(obj) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) { + return false; + } + + try { + // Not own constructor property must be Object + if ( obj.constructor && + !core_hasOwn.call(obj, "constructor") && + !core_hasOwn.call(obj.constructor.prototype, "isPrototypeOf") ) { + return false; + } + } catch ( e ) { + // IE8,9 Will throw exceptions on certain host objects #9897 + return false; + } + + // Own properties are enumerated firstly, so to speed up, + // if last one is own, then all properties are own. + + var key; + for ( key in obj ) {} + + return key === undefined || core_hasOwn.call( obj, key ); + }, + + isEmptyObject: function( obj ) { + var name; + for ( name in obj ) { + return false; + } + return true; + }, + + error: function( msg ) { + throw new Error( msg ); + }, + + // data: string of html + // context (optional): If specified, the fragment will be created in this context, defaults to document + // keepScripts (optional): If true, will include scripts passed in the html string + parseHTML: function( data, context, keepScripts ) { + if ( !data || typeof data !== "string" ) { + return null; + } + if ( typeof context === "boolean" ) { + keepScripts = context; + context = false; + } + context = context || document; + + var parsed = rsingleTag.exec( data ), + scripts = !keepScripts && []; + + // Single tag + if ( parsed ) { + return [ context.createElement( parsed[1] ) ]; + } + + parsed = jQuery.buildFragment( [ data ], context, scripts ); + if ( scripts ) { + jQuery( scripts ).remove(); + } + return jQuery.merge( [], parsed.childNodes ); + }, + + parseJSON: function( data ) { + // Attempt to parse using the native JSON parser first + if ( window.JSON && window.JSON.parse ) { + return window.JSON.parse( data ); + } + + if ( data === null ) { + return data; + } + + if ( typeof data === "string" ) { + + // Make sure leading/trailing whitespace is removed (IE can't handle it) + data = jQuery.trim( data ); + + if ( data ) { + // Make sure the incoming data is actual JSON + // Logic borrowed from http://json.org/json2.js + if ( rvalidchars.test( data.replace( rvalidescape, "@" ) + .replace( rvalidtokens, "]" ) + .replace( rvalidbraces, "")) ) { + + return ( new Function( "return " + data ) )(); + } + } + } + + jQuery.error( "Invalid JSON: " + data ); + }, + + // Cross-browser xml parsing + parseXML: function( data ) { + var xml, tmp; + if ( !data || typeof data !== "string" ) { + return null; + } + try { + if ( window.DOMParser ) { // Standard + tmp = new DOMParser(); + xml = tmp.parseFromString( data , "text/xml" ); + } else { // IE + xml = new ActiveXObject( "Microsoft.XMLDOM" ); + xml.async = "false"; + xml.loadXML( data ); + } + } catch( e ) { + xml = undefined; + } + if ( !xml || !xml.documentElement || xml.getElementsByTagName( "parsererror" ).length ) { + jQuery.error( "Invalid XML: " + data ); + } + return xml; + }, + + noop: function() {}, + + // Evaluates a script in a global context + // Workarounds based on findings by Jim Driscoll + // http://weblogs.java.net/blog/driscoll/archive/2009/09/08/eval-javascript-global-context + globalEval: function( data ) { + if ( data && jQuery.trim( data ) ) { + // We use execScript on Internet Explorer + // We use an anonymous function so that context is window + // rather than jQuery in Firefox + ( window.execScript || function( data ) { + window[ "eval" ].call( window, data ); + } )( data ); + } + }, + + // Convert dashed to camelCase; used by the css and data modules + // Microsoft forgot to hump their vendor prefix (#9572) + camelCase: function( string ) { + return string.replace( rmsPrefix, "ms-" ).replace( rdashAlpha, fcamelCase ); + }, + + nodeName: function( elem, name ) { + return elem.nodeName && elem.nodeName.toLowerCase() === name.toLowerCase(); + }, + + // args is for internal usage only + each: function( obj, callback, args ) { + var value, + i = 0, + length = obj.length, + isArray = isArraylike( obj ); + + if ( args ) { + if ( isArray ) { + for ( ; i < length; i++ ) { + value = callback.apply( obj[ i ], args ); + + if ( value === false ) { + break; + } + } + } else { + for ( i in obj ) { + value = callback.apply( obj[ i ], args ); + + if ( value === false ) { + break; + } + } + } + + // A special, fast, case for the most common use of each + } else { + if ( isArray ) { + for ( ; i < length; i++ ) { + value = callback.call( obj[ i ], i, obj[ i ] ); + + if ( value === false ) { + break; + } + } + } else { + for ( i in obj ) { + value = callback.call( obj[ i ], i, obj[ i ] ); + + if ( value === false ) { + break; + } + } + } + } + + return obj; + }, + + // Use native String.trim function wherever possible + trim: core_trim && !core_trim.call("\uFEFF\xA0") ? + function( text ) { + return text == null ? + "" : + core_trim.call( text ); + } : + + // Otherwise use our own trimming functionality + function( text ) { + return text == null ? + "" : + ( text + "" ).replace( rtrim, "" ); + }, + + // results is for internal usage only + makeArray: function( arr, results ) { + var ret = results || []; + + if ( arr != null ) { + if ( isArraylike( Object(arr) ) ) { + jQuery.merge( ret, + typeof arr === "string" ? + [ arr ] : arr + ); + } else { + core_push.call( ret, arr ); + } + } + + return ret; + }, + + inArray: function( elem, arr, i ) { + var len; + + if ( arr ) { + if ( core_indexOf ) { + return core_indexOf.call( arr, elem, i ); + } + + len = arr.length; + i = i ? i < 0 ? Math.max( 0, len + i ) : i : 0; + + for ( ; i < len; i++ ) { + // Skip accessing in sparse arrays + if ( i in arr && arr[ i ] === elem ) { + return i; + } + } + } + + return -1; + }, + + merge: function( first, second ) { + var l = second.length, + i = first.length, + j = 0; + + if ( typeof l === "number" ) { + for ( ; j < l; j++ ) { + first[ i++ ] = second[ j ]; + } + } else { + while ( second[j] !== undefined ) { + first[ i++ ] = second[ j++ ]; + } + } + + first.length = i; + + return first; + }, + + grep: function( elems, callback, inv ) { + var retVal, + ret = [], + i = 0, + length = elems.length; + inv = !!inv; + + // Go through the array, only saving the items + // that pass the validator function + for ( ; i < length; i++ ) { + retVal = !!callback( elems[ i ], i ); + if ( inv !== retVal ) { + ret.push( elems[ i ] ); + } + } + + return ret; + }, + + // arg is for internal usage only + map: function( elems, callback, arg ) { + var value, + i = 0, + length = elems.length, + isArray = isArraylike( elems ), + ret = []; + + // Go through the array, translating each of the items to their + if ( isArray ) { + for ( ; i < length; i++ ) { + value = callback( elems[ i ], i, arg ); + + if ( value != null ) { + ret[ ret.length ] = value; + } + } + + // Go through every key on the object, + } else { + for ( i in elems ) { + value = callback( elems[ i ], i, arg ); + + if ( value != null ) { + ret[ ret.length ] = value; + } + } + } + + // Flatten any nested arrays + return core_concat.apply( [], ret ); + }, + + // A global GUID counter for objects + guid: 1, + + // Bind a function to a context, optionally partially applying any + // arguments. + proxy: function( fn, context ) { + var args, proxy, tmp; + + if ( typeof context === "string" ) { + tmp = fn[ context ]; + context = fn; + fn = tmp; + } + + // Quick check to determine if target is callable, in the spec + // this throws a TypeError, but we will just return undefined. + if ( !jQuery.isFunction( fn ) ) { + return undefined; + } + + // Simulated bind + args = core_slice.call( arguments, 2 ); + proxy = function() { + return fn.apply( context || this, args.concat( core_slice.call( arguments ) ) ); + }; + + // Set the guid of unique handler to the same of original handler, so it can be removed + proxy.guid = fn.guid = fn.guid || jQuery.guid++; + + return proxy; + }, + + // Multifunctional method to get and set values of a collection + // The value/s can optionally be executed if it's a function + access: function( elems, fn, key, value, chainable, emptyGet, raw ) { + var i = 0, + length = elems.length, + bulk = key == null; + + // Sets many values + if ( jQuery.type( key ) === "object" ) { + chainable = true; + for ( i in key ) { + jQuery.access( elems, fn, i, key[i], true, emptyGet, raw ); + } + + // Sets one value + } else if ( value !== undefined ) { + chainable = true; + + if ( !jQuery.isFunction( value ) ) { + raw = true; + } + + if ( bulk ) { + // Bulk operations run against the entire set + if ( raw ) { + fn.call( elems, value ); + fn = null; + + // ...except when executing function values + } else { + bulk = fn; + fn = function( elem, key, value ) { + return bulk.call( jQuery( elem ), value ); + }; + } + } + + if ( fn ) { + for ( ; i < length; i++ ) { + fn( elems[i], key, raw ? value : value.call( elems[i], i, fn( elems[i], key ) ) ); + } + } + } + + return chainable ? + elems : + + // Gets + bulk ? + fn.call( elems ) : + length ? fn( elems[0], key ) : emptyGet; + }, + + now: function() { + return ( new Date() ).getTime(); + } +}); + +jQuery.ready.promise = function( obj ) { + if ( !readyList ) { + + readyList = jQuery.Deferred(); + + // Catch cases where $(document).ready() is called after the browser event has already occurred. + // we once tried to use readyState "interactive" here, but it caused issues like the one + // discovered by ChrisS here: http://bugs.jquery.com/ticket/12282#comment:15 + if ( document.readyState === "complete" ) { + // Handle it asynchronously to allow scripts the opportunity to delay ready + setTimeout( jQuery.ready ); + + // Standards-based browsers support DOMContentLoaded + } else if ( document.addEventListener ) { + // Use the handy event callback + document.addEventListener( "DOMContentLoaded", completed, false ); + + // A fallback to window.onload, that will always work + window.addEventListener( "load", completed, false ); + + // If IE event model is used + } else { + // Ensure firing before onload, maybe late but safe also for iframes + document.attachEvent( "onreadystatechange", completed ); + + // A fallback to window.onload, that will always work + window.attachEvent( "onload", completed ); + + // If IE and not a frame + // continually check to see if the document is ready + var top = false; + + try { + top = window.frameElement == null && document.documentElement; + } catch(e) {} + + if ( top && top.doScroll ) { + (function doScrollCheck() { + if ( !jQuery.isReady ) { + + try { + // Use the trick by Diego Perini + // http://javascript.nwbox.com/IEContentLoaded/ + top.doScroll("left"); + } catch(e) { + return setTimeout( doScrollCheck, 50 ); + } + + // detach all dom ready events + detach(); + + // and execute any waiting functions + jQuery.ready(); + } + })(); + } + } + } + return readyList.promise( obj ); +}; + +// Populate the class2type map +jQuery.each("Boolean Number String Function Array Date RegExp Object Error".split(" "), function(i, name) { + class2type[ "[object " + name + "]" ] = name.toLowerCase(); +}); + +function isArraylike( obj ) { + var length = obj.length, + type = jQuery.type( obj ); + + if ( jQuery.isWindow( obj ) ) { + return false; + } + + if ( obj.nodeType === 1 && length ) { + return true; + } + + return type === "array" || type !== "function" && + ( length === 0 || + typeof length === "number" && length > 0 && ( length - 1 ) in obj ); +} + +// All jQuery objects should point back to these +rootjQuery = jQuery(document); +// String to Object options format cache +var optionsCache = {}; + +// Convert String-formatted options into Object-formatted ones and store in cache +function createOptions( options ) { + var object = optionsCache[ options ] = {}; + jQuery.each( options.match( core_rnotwhite ) || [], function( _, flag ) { + object[ flag ] = true; + }); + return object; +} + +/* + * Create a callback list using the following parameters: + * + * options: an optional list of space-separated options that will change how + * the callback list behaves or a more traditional option object + * + * By default a callback list will act like an event callback list and can be + * "fired" multiple times. + * + * Possible options: + * + * once: will ensure the callback list can only be fired once (like a Deferred) + * + * memory: will keep track of previous values and will call any callback added + * after the list has been fired right away with the latest "memorized" + * values (like a Deferred) + * + * unique: will ensure a callback can only be added once (no duplicate in the list) + * + * stopOnFalse: interrupt callings when a callback returns false + * + */ +jQuery.Callbacks = function( options ) { + + // Convert options from String-formatted to Object-formatted if needed + // (we check in cache first) + options = typeof options === "string" ? + ( optionsCache[ options ] || createOptions( options ) ) : + jQuery.extend( {}, options ); + + var // Flag to know if list is currently firing + firing, + // Last fire value (for non-forgettable lists) + memory, + // Flag to know if list was already fired + fired, + // End of the loop when firing + firingLength, + // Index of currently firing callback (modified by remove if needed) + firingIndex, + // First callback to fire (used internally by add and fireWith) + firingStart, + // Actual callback list + list = [], + // Stack of fire calls for repeatable lists + stack = !options.once && [], + // Fire callbacks + fire = function( data ) { + memory = options.memory && data; + fired = true; + firingIndex = firingStart || 0; + firingStart = 0; + firingLength = list.length; + firing = true; + for ( ; list && firingIndex < firingLength; firingIndex++ ) { + if ( list[ firingIndex ].apply( data[ 0 ], data[ 1 ] ) === false && options.stopOnFalse ) { + memory = false; // To prevent further calls using add + break; + } + } + firing = false; + if ( list ) { + if ( stack ) { + if ( stack.length ) { + fire( stack.shift() ); + } + } else if ( memory ) { + list = []; + } else { + self.disable(); + } + } + }, + // Actual Callbacks object + self = { + // Add a callback or a collection of callbacks to the list + add: function() { + if ( list ) { + // First, we save the current length + var start = list.length; + (function add( args ) { + jQuery.each( args, function( _, arg ) { + var type = jQuery.type( arg ); + if ( type === "function" ) { + if ( !options.unique || !self.has( arg ) ) { + list.push( arg ); + } + } else if ( arg && arg.length && type !== "string" ) { + // Inspect recursively + add( arg ); + } + }); + })( arguments ); + // Do we need to add the callbacks to the + // current firing batch? + if ( firing ) { + firingLength = list.length; + // With memory, if we're not firing then + // we should call right away + } else if ( memory ) { + firingStart = start; + fire( memory ); + } + } + return this; + }, + // Remove a callback from the list + remove: function() { + if ( list ) { + jQuery.each( arguments, function( _, arg ) { + var index; + while( ( index = jQuery.inArray( arg, list, index ) ) > -1 ) { + list.splice( index, 1 ); + // Handle firing indexes + if ( firing ) { + if ( index <= firingLength ) { + firingLength--; + } + if ( index <= firingIndex ) { + firingIndex--; + } + } + } + }); + } + return this; + }, + // Check if a given callback is in the list. + // If no argument is given, return whether or not list has callbacks attached. + has: function( fn ) { + return fn ? jQuery.inArray( fn, list ) > -1 : !!( list && list.length ); + }, + // Remove all callbacks from the list + empty: function() { + list = []; + return this; + }, + // Have the list do nothing anymore + disable: function() { + list = stack = memory = undefined; + return this; + }, + // Is it disabled? + disabled: function() { + return !list; + }, + // Lock the list in its current state + lock: function() { + stack = undefined; + if ( !memory ) { + self.disable(); + } + return this; + }, + // Is it locked? + locked: function() { + return !stack; + }, + // Call all callbacks with the given context and arguments + fireWith: function( context, args ) { + args = args || []; + args = [ context, args.slice ? args.slice() : args ]; + if ( list && ( !fired || stack ) ) { + if ( firing ) { + stack.push( args ); + } else { + fire( args ); + } + } + return this; + }, + // Call all the callbacks with the given arguments + fire: function() { + self.fireWith( this, arguments ); + return this; + }, + // To know if the callbacks have already been called at least once + fired: function() { + return !!fired; + } + }; + + return self; +}; +jQuery.extend({ + + Deferred: function( func ) { + var tuples = [ + // action, add listener, listener list, final state + [ "resolve", "done", jQuery.Callbacks("once memory"), "resolved" ], + [ "reject", "fail", jQuery.Callbacks("once memory"), "rejected" ], + [ "notify", "progress", jQuery.Callbacks("memory") ] + ], + state = "pending", + promise = { + state: function() { + return state; + }, + always: function() { + deferred.done( arguments ).fail( arguments ); + return this; + }, + then: function( /* fnDone, fnFail, fnProgress */ ) { + var fns = arguments; + return jQuery.Deferred(function( newDefer ) { + jQuery.each( tuples, function( i, tuple ) { + var action = tuple[ 0 ], + fn = jQuery.isFunction( fns[ i ] ) && fns[ i ]; + // deferred[ done | fail | progress ] for forwarding actions to newDefer + deferred[ tuple[1] ](function() { + var returned = fn && fn.apply( this, arguments ); + if ( returned && jQuery.isFunction( returned.promise ) ) { + returned.promise() + .done( newDefer.resolve ) + .fail( newDefer.reject ) + .progress( newDefer.notify ); + } else { + newDefer[ action + "With" ]( this === promise ? newDefer.promise() : this, fn ? [ returned ] : arguments ); + } + }); + }); + fns = null; + }).promise(); + }, + // Get a promise for this deferred + // If obj is provided, the promise aspect is added to the object + promise: function( obj ) { + return obj != null ? jQuery.extend( obj, promise ) : promise; + } + }, + deferred = {}; + + // Keep pipe for back-compat + promise.pipe = promise.then; + + // Add list-specific methods + jQuery.each( tuples, function( i, tuple ) { + var list = tuple[ 2 ], + stateString = tuple[ 3 ]; + + // promise[ done | fail | progress ] = list.add + promise[ tuple[1] ] = list.add; + + // Handle state + if ( stateString ) { + list.add(function() { + // state = [ resolved | rejected ] + state = stateString; + + // [ reject_list | resolve_list ].disable; progress_list.lock + }, tuples[ i ^ 1 ][ 2 ].disable, tuples[ 2 ][ 2 ].lock ); + } + + // deferred[ resolve | reject | notify ] + deferred[ tuple[0] ] = function() { + deferred[ tuple[0] + "With" ]( this === deferred ? promise : this, arguments ); + return this; + }; + deferred[ tuple[0] + "With" ] = list.fireWith; + }); + + // Make the deferred a promise + promise.promise( deferred ); + + // Call given func if any + if ( func ) { + func.call( deferred, deferred ); + } + + // All done! + return deferred; + }, + + // Deferred helper + when: function( subordinate /* , ..., subordinateN */ ) { + var i = 0, + resolveValues = core_slice.call( arguments ), + length = resolveValues.length, + + // the count of uncompleted subordinates + remaining = length !== 1 || ( subordinate && jQuery.isFunction( subordinate.promise ) ) ? length : 0, + + // the master Deferred. If resolveValues consist of only a single Deferred, just use that. + deferred = remaining === 1 ? subordinate : jQuery.Deferred(), + + // Update function for both resolve and progress values + updateFunc = function( i, contexts, values ) { + return function( value ) { + contexts[ i ] = this; + values[ i ] = arguments.length > 1 ? core_slice.call( arguments ) : value; + if( values === progressValues ) { + deferred.notifyWith( contexts, values ); + } else if ( !( --remaining ) ) { + deferred.resolveWith( contexts, values ); + } + }; + }, + + progressValues, progressContexts, resolveContexts; + + // add listeners to Deferred subordinates; treat others as resolved + if ( length > 1 ) { + progressValues = new Array( length ); + progressContexts = new Array( length ); + resolveContexts = new Array( length ); + for ( ; i < length; i++ ) { + if ( resolveValues[ i ] && jQuery.isFunction( resolveValues[ i ].promise ) ) { + resolveValues[ i ].promise() + .done( updateFunc( i, resolveContexts, resolveValues ) ) + .fail( deferred.reject ) + .progress( updateFunc( i, progressContexts, progressValues ) ); + } else { + --remaining; + } + } + } + + // if we're not waiting on anything, resolve the master + if ( !remaining ) { + deferred.resolveWith( resolveContexts, resolveValues ); + } + + return deferred.promise(); + } +}); +jQuery.support = (function() { + + var support, all, a, + input, select, fragment, + opt, eventName, isSupported, i, + div = document.createElement("div"); + + // Setup + div.setAttribute( "className", "t" ); + div.innerHTML = "
a"; + + // Support tests won't run in some limited or non-browser environments + all = div.getElementsByTagName("*"); + a = div.getElementsByTagName("a")[ 0 ]; + if ( !all || !a || !all.length ) { + return {}; + } + + // First batch of tests + select = document.createElement("select"); + opt = select.appendChild( document.createElement("option") ); + input = div.getElementsByTagName("input")[ 0 ]; + + a.style.cssText = "top:1px;float:left;opacity:.5"; + support = { + // Test setAttribute on camelCase class. If it works, we need attrFixes when doing get/setAttribute (ie6/7) + getSetAttribute: div.className !== "t", + + // IE strips leading whitespace when .innerHTML is used + leadingWhitespace: div.firstChild.nodeType === 3, + + // Make sure that tbody elements aren't automatically inserted + // IE will insert them into empty tables + tbody: !div.getElementsByTagName("tbody").length, + + // Make sure that link elements get serialized correctly by innerHTML + // This requires a wrapper element in IE + htmlSerialize: !!div.getElementsByTagName("link").length, + + // Get the style information from getAttribute + // (IE uses .cssText instead) + style: /top/.test( a.getAttribute("style") ), + + // Make sure that URLs aren't manipulated + // (IE normalizes it by default) + hrefNormalized: a.getAttribute("href") === "/a", + + // Make sure that element opacity exists + // (IE uses filter instead) + // Use a regex to work around a WebKit issue. See #5145 + opacity: /^0.5/.test( a.style.opacity ), + + // Verify style float existence + // (IE uses styleFloat instead of cssFloat) + cssFloat: !!a.style.cssFloat, + + // Check the default checkbox/radio value ("" on WebKit; "on" elsewhere) + checkOn: !!input.value, + + // Make sure that a selected-by-default option has a working selected property. + // (WebKit defaults to false instead of true, IE too, if it's in an optgroup) + optSelected: opt.selected, + + // Tests for enctype support on a form (#6743) + enctype: !!document.createElement("form").enctype, + + // Makes sure cloning an html5 element does not cause problems + // Where outerHTML is undefined, this still works + html5Clone: document.createElement("nav").cloneNode( true ).outerHTML !== "<:nav>", + + // jQuery.support.boxModel DEPRECATED in 1.8 since we don't support Quirks Mode + boxModel: document.compatMode === "CSS1Compat", + + // Will be defined later + deleteExpando: true, + noCloneEvent: true, + inlineBlockNeedsLayout: false, + shrinkWrapBlocks: false, + reliableMarginRight: true, + boxSizingReliable: true, + pixelPosition: false + }; + + // Make sure checked status is properly cloned + input.checked = true; + support.noCloneChecked = input.cloneNode( true ).checked; + + // Make sure that the options inside disabled selects aren't marked as disabled + // (WebKit marks them as disabled) + select.disabled = true; + support.optDisabled = !opt.disabled; + + // Support: IE<9 + try { + delete div.test; + } catch( e ) { + support.deleteExpando = false; + } + + // Check if we can trust getAttribute("value") + input = document.createElement("input"); + input.setAttribute( "value", "" ); + support.input = input.getAttribute( "value" ) === ""; + + // Check if an input maintains its value after becoming a radio + input.value = "t"; + input.setAttribute( "type", "radio" ); + support.radioValue = input.value === "t"; + + // #11217 - WebKit loses check when the name is after the checked attribute + input.setAttribute( "checked", "t" ); + input.setAttribute( "name", "t" ); + + fragment = document.createDocumentFragment(); + fragment.appendChild( input ); + + // Check if a disconnected checkbox will retain its checked + // value of true after appended to the DOM (IE6/7) + support.appendChecked = input.checked; + + // WebKit doesn't clone checked state correctly in fragments + support.checkClone = fragment.cloneNode( true ).cloneNode( true ).lastChild.checked; + + // Support: IE<9 + // Opera does not clone events (and typeof div.attachEvent === undefined). + // IE9-10 clones events bound via attachEvent, but they don't trigger with .click() + if ( div.attachEvent ) { + div.attachEvent( "onclick", function() { + support.noCloneEvent = false; + }); + + div.cloneNode( true ).click(); + } + + // Support: IE<9 (lack submit/change bubble), Firefox 17+ (lack focusin event) + // Beware of CSP restrictions (https://developer.mozilla.org/en/Security/CSP), test/csp.php + for ( i in { submit: true, change: true, focusin: true }) { + div.setAttribute( eventName = "on" + i, "t" ); + + support[ i + "Bubbles" ] = eventName in window || div.attributes[ eventName ].expando === false; + } + + div.style.backgroundClip = "content-box"; + div.cloneNode( true ).style.backgroundClip = ""; + support.clearCloneStyle = div.style.backgroundClip === "content-box"; + + // Run tests that need a body at doc ready + jQuery(function() { + var container, marginDiv, tds, + divReset = "padding:0;margin:0;border:0;display:block;box-sizing:content-box;-moz-box-sizing:content-box;-webkit-box-sizing:content-box;", + body = document.getElementsByTagName("body")[0]; + + if ( !body ) { + // Return for frameset docs that don't have a body + return; + } + + container = document.createElement("div"); + container.style.cssText = "border:0;width:0;height:0;position:absolute;top:0;left:-9999px;margin-top:1px"; + + body.appendChild( container ).appendChild( div ); + + // Support: IE8 + // Check if table cells still have offsetWidth/Height when they are set + // to display:none and there are still other visible table cells in a + // table row; if so, offsetWidth/Height are not reliable for use when + // determining if an element has been hidden directly using + // display:none (it is still safe to use offsets if a parent element is + // hidden; don safety goggles and see bug #4512 for more information). + div.innerHTML = "
t
"; + tds = div.getElementsByTagName("td"); + tds[ 0 ].style.cssText = "padding:0;margin:0;border:0;display:none"; + isSupported = ( tds[ 0 ].offsetHeight === 0 ); + + tds[ 0 ].style.display = ""; + tds[ 1 ].style.display = "none"; + + // Support: IE8 + // Check if empty table cells still have offsetWidth/Height + support.reliableHiddenOffsets = isSupported && ( tds[ 0 ].offsetHeight === 0 ); + + // Check box-sizing and margin behavior + div.innerHTML = ""; + div.style.cssText = "box-sizing:border-box;-moz-box-sizing:border-box;-webkit-box-sizing:border-box;padding:1px;border:1px;display:block;width:4px;margin-top:1%;position:absolute;top:1%;"; + support.boxSizing = ( div.offsetWidth === 4 ); + support.doesNotIncludeMarginInBodyOffset = ( body.offsetTop !== 1 ); + + // Use window.getComputedStyle because jsdom on node.js will break without it. + if ( window.getComputedStyle ) { + support.pixelPosition = ( window.getComputedStyle( div, null ) || {} ).top !== "1%"; + support.boxSizingReliable = ( window.getComputedStyle( div, null ) || { width: "4px" } ).width === "4px"; + + // Check if div with explicit width and no margin-right incorrectly + // gets computed margin-right based on width of container. (#3333) + // Fails in WebKit before Feb 2011 nightlies + // WebKit Bug 13343 - getComputedStyle returns wrong value for margin-right + marginDiv = div.appendChild( document.createElement("div") ); + marginDiv.style.cssText = div.style.cssText = divReset; + marginDiv.style.marginRight = marginDiv.style.width = "0"; + div.style.width = "1px"; + + support.reliableMarginRight = + !parseFloat( ( window.getComputedStyle( marginDiv, null ) || {} ).marginRight ); + } + + if ( typeof div.style.zoom !== core_strundefined ) { + // Support: IE<8 + // Check if natively block-level elements act like inline-block + // elements when setting their display to 'inline' and giving + // them layout + div.innerHTML = ""; + div.style.cssText = divReset + "width:1px;padding:1px;display:inline;zoom:1"; + support.inlineBlockNeedsLayout = ( div.offsetWidth === 3 ); + + // Support: IE6 + // Check if elements with layout shrink-wrap their children + div.style.display = "block"; + div.innerHTML = "
"; + div.firstChild.style.width = "5px"; + support.shrinkWrapBlocks = ( div.offsetWidth !== 3 ); + + if ( support.inlineBlockNeedsLayout ) { + // Prevent IE 6 from affecting layout for positioned elements #11048 + // Prevent IE from shrinking the body in IE 7 mode #12869 + // Support: IE<8 + body.style.zoom = 1; + } + } + + body.removeChild( container ); + + // Null elements to avoid leaks in IE + container = div = tds = marginDiv = null; + }); + + // Null elements to avoid leaks in IE + all = select = fragment = opt = a = input = null; + + return support; +})(); + +var rbrace = /(?:\{[\s\S]*\}|\[[\s\S]*\])$/, + rmultiDash = /([A-Z])/g; + +function internalData( elem, name, data, pvt /* Internal Use Only */ ){ + if ( !jQuery.acceptData( elem ) ) { + return; + } + + var thisCache, ret, + internalKey = jQuery.expando, + getByName = typeof name === "string", + + // We have to handle DOM nodes and JS objects differently because IE6-7 + // can't GC object references properly across the DOM-JS boundary + isNode = elem.nodeType, + + // Only DOM nodes need the global jQuery cache; JS object data is + // attached directly to the object so GC can occur automatically + cache = isNode ? jQuery.cache : elem, + + // Only defining an ID for JS objects if its cache already exists allows + // the code to shortcut on the same path as a DOM node with no cache + id = isNode ? elem[ internalKey ] : elem[ internalKey ] && internalKey; + + // Avoid doing any more work than we need to when trying to get data on an + // object that has no data at all + if ( (!id || !cache[id] || (!pvt && !cache[id].data)) && getByName && data === undefined ) { + return; + } + + if ( !id ) { + // Only DOM nodes need a new unique ID for each element since their data + // ends up in the global cache + if ( isNode ) { + elem[ internalKey ] = id = core_deletedIds.pop() || jQuery.guid++; + } else { + id = internalKey; + } + } + + if ( !cache[ id ] ) { + cache[ id ] = {}; + + // Avoids exposing jQuery metadata on plain JS objects when the object + // is serialized using JSON.stringify + if ( !isNode ) { + cache[ id ].toJSON = jQuery.noop; + } + } + + // An object can be passed to jQuery.data instead of a key/value pair; this gets + // shallow copied over onto the existing cache + if ( typeof name === "object" || typeof name === "function" ) { + if ( pvt ) { + cache[ id ] = jQuery.extend( cache[ id ], name ); + } else { + cache[ id ].data = jQuery.extend( cache[ id ].data, name ); + } + } + + thisCache = cache[ id ]; + + // jQuery data() is stored in a separate object inside the object's internal data + // cache in order to avoid key collisions between internal data and user-defined + // data. + if ( !pvt ) { + if ( !thisCache.data ) { + thisCache.data = {}; + } + + thisCache = thisCache.data; + } + + if ( data !== undefined ) { + thisCache[ jQuery.camelCase( name ) ] = data; + } + + // Check for both converted-to-camel and non-converted data property names + // If a data property was specified + if ( getByName ) { + + // First Try to find as-is property data + ret = thisCache[ name ]; + + // Test for null|undefined property data + if ( ret == null ) { + + // Try to find the camelCased property + ret = thisCache[ jQuery.camelCase( name ) ]; + } + } else { + ret = thisCache; + } + + return ret; +} + +function internalRemoveData( elem, name, pvt ) { + if ( !jQuery.acceptData( elem ) ) { + return; + } + + var i, l, thisCache, + isNode = elem.nodeType, + + // See jQuery.data for more information + cache = isNode ? jQuery.cache : elem, + id = isNode ? elem[ jQuery.expando ] : jQuery.expando; + + // If there is already no cache entry for this object, there is no + // purpose in continuing + if ( !cache[ id ] ) { + return; + } + + if ( name ) { + + thisCache = pvt ? cache[ id ] : cache[ id ].data; + + if ( thisCache ) { + + // Support array or space separated string names for data keys + if ( !jQuery.isArray( name ) ) { + + // try the string as a key before any manipulation + if ( name in thisCache ) { + name = [ name ]; + } else { + + // split the camel cased version by spaces unless a key with the spaces exists + name = jQuery.camelCase( name ); + if ( name in thisCache ) { + name = [ name ]; + } else { + name = name.split(" "); + } + } + } else { + // If "name" is an array of keys... + // When data is initially created, via ("key", "val") signature, + // keys will be converted to camelCase. + // Since there is no way to tell _how_ a key was added, remove + // both plain key and camelCase key. #12786 + // This will only penalize the array argument path. + name = name.concat( jQuery.map( name, jQuery.camelCase ) ); + } + + for ( i = 0, l = name.length; i < l; i++ ) { + delete thisCache[ name[i] ]; + } + + // If there is no data left in the cache, we want to continue + // and let the cache object itself get destroyed + if ( !( pvt ? isEmptyDataObject : jQuery.isEmptyObject )( thisCache ) ) { + return; + } + } + } + + // See jQuery.data for more information + if ( !pvt ) { + delete cache[ id ].data; + + // Don't destroy the parent cache unless the internal data object + // had been the only thing left in it + if ( !isEmptyDataObject( cache[ id ] ) ) { + return; + } + } + + // Destroy the cache + if ( isNode ) { + jQuery.cleanData( [ elem ], true ); + + // Use delete when supported for expandos or `cache` is not a window per isWindow (#10080) + } else if ( jQuery.support.deleteExpando || cache != cache.window ) { + delete cache[ id ]; + + // When all else fails, null + } else { + cache[ id ] = null; + } +} + +jQuery.extend({ + cache: {}, + + // Unique for each copy of jQuery on the page + // Non-digits removed to match rinlinejQuery + expando: "jQuery" + ( core_version + Math.random() ).replace( /\D/g, "" ), + + // The following elements throw uncatchable exceptions if you + // attempt to add expando properties to them. + noData: { + "embed": true, + // Ban all objects except for Flash (which handle expandos) + "object": "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000", + "applet": true + }, + + hasData: function( elem ) { + elem = elem.nodeType ? jQuery.cache[ elem[jQuery.expando] ] : elem[ jQuery.expando ]; + return !!elem && !isEmptyDataObject( elem ); + }, + + data: function( elem, name, data ) { + return internalData( elem, name, data ); + }, + + removeData: function( elem, name ) { + return internalRemoveData( elem, name ); + }, + + // For internal use only. + _data: function( elem, name, data ) { + return internalData( elem, name, data, true ); + }, + + _removeData: function( elem, name ) { + return internalRemoveData( elem, name, true ); + }, + + // A method for determining if a DOM node can handle the data expando + acceptData: function( elem ) { + // Do not set data on non-element because it will not be cleared (#8335). + if ( elem.nodeType && elem.nodeType !== 1 && elem.nodeType !== 9 ) { + return false; + } + + var noData = elem.nodeName && jQuery.noData[ elem.nodeName.toLowerCase() ]; + + // nodes accept data unless otherwise specified; rejection can be conditional + return !noData || noData !== true && elem.getAttribute("classid") === noData; + } +}); + +jQuery.fn.extend({ + data: function( key, value ) { + var attrs, name, + elem = this[0], + i = 0, + data = null; + + // Gets all values + if ( key === undefined ) { + if ( this.length ) { + data = jQuery.data( elem ); + + if ( elem.nodeType === 1 && !jQuery._data( elem, "parsedAttrs" ) ) { + attrs = elem.attributes; + for ( ; i < attrs.length; i++ ) { + name = attrs[i].name; + + if ( !name.indexOf( "data-" ) ) { + name = jQuery.camelCase( name.slice(5) ); + + dataAttr( elem, name, data[ name ] ); + } + } + jQuery._data( elem, "parsedAttrs", true ); + } + } + + return data; + } + + // Sets multiple values + if ( typeof key === "object" ) { + return this.each(function() { + jQuery.data( this, key ); + }); + } + + return jQuery.access( this, function( value ) { + + if ( value === undefined ) { + // Try to fetch any internally stored data first + return elem ? dataAttr( elem, key, jQuery.data( elem, key ) ) : null; + } + + this.each(function() { + jQuery.data( this, key, value ); + }); + }, null, value, arguments.length > 1, null, true ); + }, + + removeData: function( key ) { + return this.each(function() { + jQuery.removeData( this, key ); + }); + } +}); + +function dataAttr( elem, key, data ) { + // If nothing was found internally, try to fetch any + // data from the HTML5 data-* attribute + if ( data === undefined && elem.nodeType === 1 ) { + + var name = "data-" + key.replace( rmultiDash, "-$1" ).toLowerCase(); + + data = elem.getAttribute( name ); + + if ( typeof data === "string" ) { + try { + data = data === "true" ? true : + data === "false" ? false : + data === "null" ? null : + // Only convert to a number if it doesn't change the string + +data + "" === data ? +data : + rbrace.test( data ) ? jQuery.parseJSON( data ) : + data; + } catch( e ) {} + + // Make sure we set the data so it isn't changed later + jQuery.data( elem, key, data ); + + } else { + data = undefined; + } + } + + return data; +} + +// checks a cache object for emptiness +function isEmptyDataObject( obj ) { + var name; + for ( name in obj ) { + + // if the public data object is empty, the private is still empty + if ( name === "data" && jQuery.isEmptyObject( obj[name] ) ) { + continue; + } + if ( name !== "toJSON" ) { + return false; + } + } + + return true; +} +jQuery.extend({ + queue: function( elem, type, data ) { + var queue; + + if ( elem ) { + type = ( type || "fx" ) + "queue"; + queue = jQuery._data( elem, type ); + + // Speed up dequeue by getting out quickly if this is just a lookup + if ( data ) { + if ( !queue || jQuery.isArray(data) ) { + queue = jQuery._data( elem, type, jQuery.makeArray(data) ); + } else { + queue.push( data ); + } + } + return queue || []; + } + }, + + dequeue: function( elem, type ) { + type = type || "fx"; + + var queue = jQuery.queue( elem, type ), + startLength = queue.length, + fn = queue.shift(), + hooks = jQuery._queueHooks( elem, type ), + next = function() { + jQuery.dequeue( elem, type ); + }; + + // If the fx queue is dequeued, always remove the progress sentinel + if ( fn === "inprogress" ) { + fn = queue.shift(); + startLength--; + } + + hooks.cur = fn; + if ( fn ) { + + // Add a progress sentinel to prevent the fx queue from being + // automatically dequeued + if ( type === "fx" ) { + queue.unshift( "inprogress" ); + } + + // clear up the last queue stop function + delete hooks.stop; + fn.call( elem, next, hooks ); + } + + if ( !startLength && hooks ) { + hooks.empty.fire(); + } + }, + + // not intended for public consumption - generates a queueHooks object, or returns the current one + _queueHooks: function( elem, type ) { + var key = type + "queueHooks"; + return jQuery._data( elem, key ) || jQuery._data( elem, key, { + empty: jQuery.Callbacks("once memory").add(function() { + jQuery._removeData( elem, type + "queue" ); + jQuery._removeData( elem, key ); + }) + }); + } +}); + +jQuery.fn.extend({ + queue: function( type, data ) { + var setter = 2; + + if ( typeof type !== "string" ) { + data = type; + type = "fx"; + setter--; + } + + if ( arguments.length < setter ) { + return jQuery.queue( this[0], type ); + } + + return data === undefined ? + this : + this.each(function() { + var queue = jQuery.queue( this, type, data ); + + // ensure a hooks for this queue + jQuery._queueHooks( this, type ); + + if ( type === "fx" && queue[0] !== "inprogress" ) { + jQuery.dequeue( this, type ); + } + }); + }, + dequeue: function( type ) { + return this.each(function() { + jQuery.dequeue( this, type ); + }); + }, + // Based off of the plugin by Clint Helfers, with permission. + // http://blindsignals.com/index.php/2009/07/jquery-delay/ + delay: function( time, type ) { + time = jQuery.fx ? jQuery.fx.speeds[ time ] || time : time; + type = type || "fx"; + + return this.queue( type, function( next, hooks ) { + var timeout = setTimeout( next, time ); + hooks.stop = function() { + clearTimeout( timeout ); + }; + }); + }, + clearQueue: function( type ) { + return this.queue( type || "fx", [] ); + }, + // Get a promise resolved when queues of a certain type + // are emptied (fx is the type by default) + promise: function( type, obj ) { + var tmp, + count = 1, + defer = jQuery.Deferred(), + elements = this, + i = this.length, + resolve = function() { + if ( !( --count ) ) { + defer.resolveWith( elements, [ elements ] ); + } + }; + + if ( typeof type !== "string" ) { + obj = type; + type = undefined; + } + type = type || "fx"; + + while( i-- ) { + tmp = jQuery._data( elements[ i ], type + "queueHooks" ); + if ( tmp && tmp.empty ) { + count++; + tmp.empty.add( resolve ); + } + } + resolve(); + return defer.promise( obj ); + } +}); +var nodeHook, boolHook, + rclass = /[\t\r\n]/g, + rreturn = /\r/g, + rfocusable = /^(?:input|select|textarea|button|object)$/i, + rclickable = /^(?:a|area)$/i, + rboolean = /^(?:checked|selected|autofocus|autoplay|async|controls|defer|disabled|hidden|loop|multiple|open|readonly|required|scoped)$/i, + ruseDefault = /^(?:checked|selected)$/i, + getSetAttribute = jQuery.support.getSetAttribute, + getSetInput = jQuery.support.input; + +jQuery.fn.extend({ + attr: function( name, value ) { + return jQuery.access( this, jQuery.attr, name, value, arguments.length > 1 ); + }, + + removeAttr: function( name ) { + return this.each(function() { + jQuery.removeAttr( this, name ); + }); + }, + + prop: function( name, value ) { + return jQuery.access( this, jQuery.prop, name, value, arguments.length > 1 ); + }, + + removeProp: function( name ) { + name = jQuery.propFix[ name ] || name; + return this.each(function() { + // try/catch handles cases where IE balks (such as removing a property on window) + try { + this[ name ] = undefined; + delete this[ name ]; + } catch( e ) {} + }); + }, + + addClass: function( value ) { + var classes, elem, cur, clazz, j, + i = 0, + len = this.length, + proceed = typeof value === "string" && value; + + if ( jQuery.isFunction( value ) ) { + return this.each(function( j ) { + jQuery( this ).addClass( value.call( this, j, this.className ) ); + }); + } + + if ( proceed ) { + // The disjunction here is for better compressibility (see removeClass) + classes = ( value || "" ).match( core_rnotwhite ) || []; + + for ( ; i < len; i++ ) { + elem = this[ i ]; + cur = elem.nodeType === 1 && ( elem.className ? + ( " " + elem.className + " " ).replace( rclass, " " ) : + " " + ); + + if ( cur ) { + j = 0; + while ( (clazz = classes[j++]) ) { + if ( cur.indexOf( " " + clazz + " " ) < 0 ) { + cur += clazz + " "; + } + } + elem.className = jQuery.trim( cur ); + + } + } + } + + return this; + }, + + removeClass: function( value ) { + var classes, elem, cur, clazz, j, + i = 0, + len = this.length, + proceed = arguments.length === 0 || typeof value === "string" && value; + + if ( jQuery.isFunction( value ) ) { + return this.each(function( j ) { + jQuery( this ).removeClass( value.call( this, j, this.className ) ); + }); + } + if ( proceed ) { + classes = ( value || "" ).match( core_rnotwhite ) || []; + + for ( ; i < len; i++ ) { + elem = this[ i ]; + // This expression is here for better compressibility (see addClass) + cur = elem.nodeType === 1 && ( elem.className ? + ( " " + elem.className + " " ).replace( rclass, " " ) : + "" + ); + + if ( cur ) { + j = 0; + while ( (clazz = classes[j++]) ) { + // Remove *all* instances + while ( cur.indexOf( " " + clazz + " " ) >= 0 ) { + cur = cur.replace( " " + clazz + " ", " " ); + } + } + elem.className = value ? jQuery.trim( cur ) : ""; + } + } + } + + return this; + }, + + toggleClass: function( value, stateVal ) { + var type = typeof value, + isBool = typeof stateVal === "boolean"; + + if ( jQuery.isFunction( value ) ) { + return this.each(function( i ) { + jQuery( this ).toggleClass( value.call(this, i, this.className, stateVal), stateVal ); + }); + } + + return this.each(function() { + if ( type === "string" ) { + // toggle individual class names + var className, + i = 0, + self = jQuery( this ), + state = stateVal, + classNames = value.match( core_rnotwhite ) || []; + + while ( (className = classNames[ i++ ]) ) { + // check each className given, space separated list + state = isBool ? state : !self.hasClass( className ); + self[ state ? "addClass" : "removeClass" ]( className ); + } + + // Toggle whole class name + } else if ( type === core_strundefined || type === "boolean" ) { + if ( this.className ) { + // store className if set + jQuery._data( this, "__className__", this.className ); + } + + // If the element has a class name or if we're passed "false", + // then remove the whole classname (if there was one, the above saved it). + // Otherwise bring back whatever was previously saved (if anything), + // falling back to the empty string if nothing was stored. + this.className = this.className || value === false ? "" : jQuery._data( this, "__className__" ) || ""; + } + }); + }, + + hasClass: function( selector ) { + var className = " " + selector + " ", + i = 0, + l = this.length; + for ( ; i < l; i++ ) { + if ( this[i].nodeType === 1 && (" " + this[i].className + " ").replace(rclass, " ").indexOf( className ) >= 0 ) { + return true; + } + } + + return false; + }, + + val: function( value ) { + var ret, hooks, isFunction, + elem = this[0]; + + if ( !arguments.length ) { + if ( elem ) { + hooks = jQuery.valHooks[ elem.type ] || jQuery.valHooks[ elem.nodeName.toLowerCase() ]; + + if ( hooks && "get" in hooks && (ret = hooks.get( elem, "value" )) !== undefined ) { + return ret; + } + + ret = elem.value; + + return typeof ret === "string" ? + // handle most common string cases + ret.replace(rreturn, "") : + // handle cases where value is null/undef or number + ret == null ? "" : ret; + } + + return; + } + + isFunction = jQuery.isFunction( value ); + + return this.each(function( i ) { + var val, + self = jQuery(this); + + if ( this.nodeType !== 1 ) { + return; + } + + if ( isFunction ) { + val = value.call( this, i, self.val() ); + } else { + val = value; + } + + // Treat null/undefined as ""; convert numbers to string + if ( val == null ) { + val = ""; + } else if ( typeof val === "number" ) { + val += ""; + } else if ( jQuery.isArray( val ) ) { + val = jQuery.map(val, function ( value ) { + return value == null ? "" : value + ""; + }); + } + + hooks = jQuery.valHooks[ this.type ] || jQuery.valHooks[ this.nodeName.toLowerCase() ]; + + // If set returns undefined, fall back to normal setting + if ( !hooks || !("set" in hooks) || hooks.set( this, val, "value" ) === undefined ) { + this.value = val; + } + }); + } +}); + +jQuery.extend({ + valHooks: { + option: { + get: function( elem ) { + // attributes.value is undefined in Blackberry 4.7 but + // uses .value. See #6932 + var val = elem.attributes.value; + return !val || val.specified ? elem.value : elem.text; + } + }, + select: { + get: function( elem ) { + var value, option, + options = elem.options, + index = elem.selectedIndex, + one = elem.type === "select-one" || index < 0, + values = one ? null : [], + max = one ? index + 1 : options.length, + i = index < 0 ? + max : + one ? index : 0; + + // Loop through all the selected options + for ( ; i < max; i++ ) { + option = options[ i ]; + + // oldIE doesn't update selected after form reset (#2551) + if ( ( option.selected || i === index ) && + // Don't return options that are disabled or in a disabled optgroup + ( jQuery.support.optDisabled ? !option.disabled : option.getAttribute("disabled") === null ) && + ( !option.parentNode.disabled || !jQuery.nodeName( option.parentNode, "optgroup" ) ) ) { + + // Get the specific value for the option + value = jQuery( option ).val(); + + // We don't need an array for one selects + if ( one ) { + return value; + } + + // Multi-Selects return an array + values.push( value ); + } + } + + return values; + }, + + set: function( elem, value ) { + var values = jQuery.makeArray( value ); + + jQuery(elem).find("option").each(function() { + this.selected = jQuery.inArray( jQuery(this).val(), values ) >= 0; + }); + + if ( !values.length ) { + elem.selectedIndex = -1; + } + return values; + } + } + }, + + attr: function( elem, name, value ) { + var hooks, notxml, ret, + nType = elem.nodeType; + + // don't get/set attributes on text, comment and attribute nodes + if ( !elem || nType === 3 || nType === 8 || nType === 2 ) { + return; + } + + // Fallback to prop when attributes are not supported + if ( typeof elem.getAttribute === core_strundefined ) { + return jQuery.prop( elem, name, value ); + } + + notxml = nType !== 1 || !jQuery.isXMLDoc( elem ); + + // All attributes are lowercase + // Grab necessary hook if one is defined + if ( notxml ) { + name = name.toLowerCase(); + hooks = jQuery.attrHooks[ name ] || ( rboolean.test( name ) ? boolHook : nodeHook ); + } + + if ( value !== undefined ) { + + if ( value === null ) { + jQuery.removeAttr( elem, name ); + + } else if ( hooks && notxml && "set" in hooks && (ret = hooks.set( elem, value, name )) !== undefined ) { + return ret; + + } else { + elem.setAttribute( name, value + "" ); + return value; + } + + } else if ( hooks && notxml && "get" in hooks && (ret = hooks.get( elem, name )) !== null ) { + return ret; + + } else { + + // In IE9+, Flash objects don't have .getAttribute (#12945) + // Support: IE9+ + if ( typeof elem.getAttribute !== core_strundefined ) { + ret = elem.getAttribute( name ); + } + + // Non-existent attributes return null, we normalize to undefined + return ret == null ? + undefined : + ret; + } + }, + + removeAttr: function( elem, value ) { + var name, propName, + i = 0, + attrNames = value && value.match( core_rnotwhite ); + + if ( attrNames && elem.nodeType === 1 ) { + while ( (name = attrNames[i++]) ) { + propName = jQuery.propFix[ name ] || name; + + // Boolean attributes get special treatment (#10870) + if ( rboolean.test( name ) ) { + // Set corresponding property to false for boolean attributes + // Also clear defaultChecked/defaultSelected (if appropriate) for IE<8 + if ( !getSetAttribute && ruseDefault.test( name ) ) { + elem[ jQuery.camelCase( "default-" + name ) ] = + elem[ propName ] = false; + } else { + elem[ propName ] = false; + } + + // See #9699 for explanation of this approach (setting first, then removal) + } else { + jQuery.attr( elem, name, "" ); + } + + elem.removeAttribute( getSetAttribute ? name : propName ); + } + } + }, + + attrHooks: { + type: { + set: function( elem, value ) { + if ( !jQuery.support.radioValue && value === "radio" && jQuery.nodeName(elem, "input") ) { + // Setting the type on a radio button after the value resets the value in IE6-9 + // Reset value to default in case type is set after value during creation + var val = elem.value; + elem.setAttribute( "type", value ); + if ( val ) { + elem.value = val; + } + return value; + } + } + } + }, + + propFix: { + tabindex: "tabIndex", + readonly: "readOnly", + "for": "htmlFor", + "class": "className", + maxlength: "maxLength", + cellspacing: "cellSpacing", + cellpadding: "cellPadding", + rowspan: "rowSpan", + colspan: "colSpan", + usemap: "useMap", + frameborder: "frameBorder", + contenteditable: "contentEditable" + }, + + prop: function( elem, name, value ) { + var ret, hooks, notxml, + nType = elem.nodeType; + + // don't get/set properties on text, comment and attribute nodes + if ( !elem || nType === 3 || nType === 8 || nType === 2 ) { + return; + } + + notxml = nType !== 1 || !jQuery.isXMLDoc( elem ); + + if ( notxml ) { + // Fix name and attach hooks + name = jQuery.propFix[ name ] || name; + hooks = jQuery.propHooks[ name ]; + } + + if ( value !== undefined ) { + if ( hooks && "set" in hooks && (ret = hooks.set( elem, value, name )) !== undefined ) { + return ret; + + } else { + return ( elem[ name ] = value ); + } + + } else { + if ( hooks && "get" in hooks && (ret = hooks.get( elem, name )) !== null ) { + return ret; + + } else { + return elem[ name ]; + } + } + }, + + propHooks: { + tabIndex: { + get: function( elem ) { + // elem.tabIndex doesn't always return the correct value when it hasn't been explicitly set + // http://fluidproject.org/blog/2008/01/09/getting-setting-and-removing-tabindex-values-with-javascript/ + var attributeNode = elem.getAttributeNode("tabindex"); + + return attributeNode && attributeNode.specified ? + parseInt( attributeNode.value, 10 ) : + rfocusable.test( elem.nodeName ) || rclickable.test( elem.nodeName ) && elem.href ? + 0 : + undefined; + } + } + } +}); + +// Hook for boolean attributes +boolHook = { + get: function( elem, name ) { + var + // Use .prop to determine if this attribute is understood as boolean + prop = jQuery.prop( elem, name ), + + // Fetch it accordingly + attr = typeof prop === "boolean" && elem.getAttribute( name ), + detail = typeof prop === "boolean" ? + + getSetInput && getSetAttribute ? + attr != null : + // oldIE fabricates an empty string for missing boolean attributes + // and conflates checked/selected into attroperties + ruseDefault.test( name ) ? + elem[ jQuery.camelCase( "default-" + name ) ] : + !!attr : + + // fetch an attribute node for properties not recognized as boolean + elem.getAttributeNode( name ); + + return detail && detail.value !== false ? + name.toLowerCase() : + undefined; + }, + set: function( elem, value, name ) { + if ( value === false ) { + // Remove boolean attributes when set to false + jQuery.removeAttr( elem, name ); + } else if ( getSetInput && getSetAttribute || !ruseDefault.test( name ) ) { + // IE<8 needs the *property* name + elem.setAttribute( !getSetAttribute && jQuery.propFix[ name ] || name, name ); + + // Use defaultChecked and defaultSelected for oldIE + } else { + elem[ jQuery.camelCase( "default-" + name ) ] = elem[ name ] = true; + } + + return name; + } +}; + +// fix oldIE value attroperty +if ( !getSetInput || !getSetAttribute ) { + jQuery.attrHooks.value = { + get: function( elem, name ) { + var ret = elem.getAttributeNode( name ); + return jQuery.nodeName( elem, "input" ) ? + + // Ignore the value *property* by using defaultValue + elem.defaultValue : + + ret && ret.specified ? ret.value : undefined; + }, + set: function( elem, value, name ) { + if ( jQuery.nodeName( elem, "input" ) ) { + // Does not return so that setAttribute is also used + elem.defaultValue = value; + } else { + // Use nodeHook if defined (#1954); otherwise setAttribute is fine + return nodeHook && nodeHook.set( elem, value, name ); + } + } + }; +} + +// IE6/7 do not support getting/setting some attributes with get/setAttribute +if ( !getSetAttribute ) { + + // Use this for any attribute in IE6/7 + // This fixes almost every IE6/7 issue + nodeHook = jQuery.valHooks.button = { + get: function( elem, name ) { + var ret = elem.getAttributeNode( name ); + return ret && ( name === "id" || name === "name" || name === "coords" ? ret.value !== "" : ret.specified ) ? + ret.value : + undefined; + }, + set: function( elem, value, name ) { + // Set the existing or create a new attribute node + var ret = elem.getAttributeNode( name ); + if ( !ret ) { + elem.setAttributeNode( + (ret = elem.ownerDocument.createAttribute( name )) + ); + } + + ret.value = value += ""; + + // Break association with cloned elements by also using setAttribute (#9646) + return name === "value" || value === elem.getAttribute( name ) ? + value : + undefined; + } + }; + + // Set contenteditable to false on removals(#10429) + // Setting to empty string throws an error as an invalid value + jQuery.attrHooks.contenteditable = { + get: nodeHook.get, + set: function( elem, value, name ) { + nodeHook.set( elem, value === "" ? false : value, name ); + } + }; + + // Set width and height to auto instead of 0 on empty string( Bug #8150 ) + // This is for removals + jQuery.each([ "width", "height" ], function( i, name ) { + jQuery.attrHooks[ name ] = jQuery.extend( jQuery.attrHooks[ name ], { + set: function( elem, value ) { + if ( value === "" ) { + elem.setAttribute( name, "auto" ); + return value; + } + } + }); + }); +} + + +// Some attributes require a special call on IE +// http://msdn.microsoft.com/en-us/library/ms536429%28VS.85%29.aspx +if ( !jQuery.support.hrefNormalized ) { + jQuery.each([ "href", "src", "width", "height" ], function( i, name ) { + jQuery.attrHooks[ name ] = jQuery.extend( jQuery.attrHooks[ name ], { + get: function( elem ) { + var ret = elem.getAttribute( name, 2 ); + return ret == null ? undefined : ret; + } + }); + }); + + // href/src property should get the full normalized URL (#10299/#12915) + jQuery.each([ "href", "src" ], function( i, name ) { + jQuery.propHooks[ name ] = { + get: function( elem ) { + return elem.getAttribute( name, 4 ); + } + }; + }); +} + +if ( !jQuery.support.style ) { + jQuery.attrHooks.style = { + get: function( elem ) { + // Return undefined in the case of empty string + // Note: IE uppercases css property names, but if we were to .toLowerCase() + // .cssText, that would destroy case senstitivity in URL's, like in "background" + return elem.style.cssText || undefined; + }, + set: function( elem, value ) { + return ( elem.style.cssText = value + "" ); + } + }; +} + +// Safari mis-reports the default selected property of an option +// Accessing the parent's selectedIndex property fixes it +if ( !jQuery.support.optSelected ) { + jQuery.propHooks.selected = jQuery.extend( jQuery.propHooks.selected, { + get: function( elem ) { + var parent = elem.parentNode; + + if ( parent ) { + parent.selectedIndex; + + // Make sure that it also works with optgroups, see #5701 + if ( parent.parentNode ) { + parent.parentNode.selectedIndex; + } + } + return null; + } + }); +} + +// IE6/7 call enctype encoding +if ( !jQuery.support.enctype ) { + jQuery.propFix.enctype = "encoding"; +} + +// Radios and checkboxes getter/setter +if ( !jQuery.support.checkOn ) { + jQuery.each([ "radio", "checkbox" ], function() { + jQuery.valHooks[ this ] = { + get: function( elem ) { + // Handle the case where in Webkit "" is returned instead of "on" if a value isn't specified + return elem.getAttribute("value") === null ? "on" : elem.value; + } + }; + }); +} +jQuery.each([ "radio", "checkbox" ], function() { + jQuery.valHooks[ this ] = jQuery.extend( jQuery.valHooks[ this ], { + set: function( elem, value ) { + if ( jQuery.isArray( value ) ) { + return ( elem.checked = jQuery.inArray( jQuery(elem).val(), value ) >= 0 ); + } + } + }); +}); +var rformElems = /^(?:input|select|textarea)$/i, + rkeyEvent = /^key/, + rmouseEvent = /^(?:mouse|contextmenu)|click/, + rfocusMorph = /^(?:focusinfocus|focusoutblur)$/, + rtypenamespace = /^([^.]*)(?:\.(.+)|)$/; + +function returnTrue() { + return true; +} + +function returnFalse() { + return false; +} + +/* + * Helper functions for managing events -- not part of the public interface. + * Props to Dean Edwards' addEvent library for many of the ideas. + */ +jQuery.event = { + + global: {}, + + add: function( elem, types, handler, data, selector ) { + var tmp, events, t, handleObjIn, + special, eventHandle, handleObj, + handlers, type, namespaces, origType, + elemData = jQuery._data( elem ); + + // Don't attach events to noData or text/comment nodes (but allow plain objects) + if ( !elemData ) { + return; + } + + // Caller can pass in an object of custom data in lieu of the handler + if ( handler.handler ) { + handleObjIn = handler; + handler = handleObjIn.handler; + selector = handleObjIn.selector; + } + + // Make sure that the handler has a unique ID, used to find/remove it later + if ( !handler.guid ) { + handler.guid = jQuery.guid++; + } + + // Init the element's event structure and main handler, if this is the first + if ( !(events = elemData.events) ) { + events = elemData.events = {}; + } + if ( !(eventHandle = elemData.handle) ) { + eventHandle = elemData.handle = function( e ) { + // Discard the second event of a jQuery.event.trigger() and + // when an event is called after a page has unloaded + return typeof jQuery !== core_strundefined && (!e || jQuery.event.triggered !== e.type) ? + jQuery.event.dispatch.apply( eventHandle.elem, arguments ) : + undefined; + }; + // Add elem as a property of the handle fn to prevent a memory leak with IE non-native events + eventHandle.elem = elem; + } + + // Handle multiple events separated by a space + // jQuery(...).bind("mouseover mouseout", fn); + types = ( types || "" ).match( core_rnotwhite ) || [""]; + t = types.length; + while ( t-- ) { + tmp = rtypenamespace.exec( types[t] ) || []; + type = origType = tmp[1]; + namespaces = ( tmp[2] || "" ).split( "." ).sort(); + + // If event changes its type, use the special event handlers for the changed type + special = jQuery.event.special[ type ] || {}; + + // If selector defined, determine special event api type, otherwise given type + type = ( selector ? special.delegateType : special.bindType ) || type; + + // Update special based on newly reset type + special = jQuery.event.special[ type ] || {}; + + // handleObj is passed to all event handlers + handleObj = jQuery.extend({ + type: type, + origType: origType, + data: data, + handler: handler, + guid: handler.guid, + selector: selector, + needsContext: selector && jQuery.expr.match.needsContext.test( selector ), + namespace: namespaces.join(".") + }, handleObjIn ); + + // Init the event handler queue if we're the first + if ( !(handlers = events[ type ]) ) { + handlers = events[ type ] = []; + handlers.delegateCount = 0; + + // Only use addEventListener/attachEvent if the special events handler returns false + if ( !special.setup || special.setup.call( elem, data, namespaces, eventHandle ) === false ) { + // Bind the global event handler to the element + if ( elem.addEventListener ) { + elem.addEventListener( type, eventHandle, false ); + + } else if ( elem.attachEvent ) { + elem.attachEvent( "on" + type, eventHandle ); + } + } + } + + if ( special.add ) { + special.add.call( elem, handleObj ); + + if ( !handleObj.handler.guid ) { + handleObj.handler.guid = handler.guid; + } + } + + // Add to the element's handler list, delegates in front + if ( selector ) { + handlers.splice( handlers.delegateCount++, 0, handleObj ); + } else { + handlers.push( handleObj ); + } + + // Keep track of which events have ever been used, for event optimization + jQuery.event.global[ type ] = true; + } + + // Nullify elem to prevent memory leaks in IE + elem = null; + }, + + // Detach an event or set of events from an element + remove: function( elem, types, handler, selector, mappedTypes ) { + var j, handleObj, tmp, + origCount, t, events, + special, handlers, type, + namespaces, origType, + elemData = jQuery.hasData( elem ) && jQuery._data( elem ); + + if ( !elemData || !(events = elemData.events) ) { + return; + } + + // Once for each type.namespace in types; type may be omitted + types = ( types || "" ).match( core_rnotwhite ) || [""]; + t = types.length; + while ( t-- ) { + tmp = rtypenamespace.exec( types[t] ) || []; + type = origType = tmp[1]; + namespaces = ( tmp[2] || "" ).split( "." ).sort(); + + // Unbind all events (on this namespace, if provided) for the element + if ( !type ) { + for ( type in events ) { + jQuery.event.remove( elem, type + types[ t ], handler, selector, true ); + } + continue; + } + + special = jQuery.event.special[ type ] || {}; + type = ( selector ? special.delegateType : special.bindType ) || type; + handlers = events[ type ] || []; + tmp = tmp[2] && new RegExp( "(^|\\.)" + namespaces.join("\\.(?:.*\\.|)") + "(\\.|$)" ); + + // Remove matching events + origCount = j = handlers.length; + while ( j-- ) { + handleObj = handlers[ j ]; + + if ( ( mappedTypes || origType === handleObj.origType ) && + ( !handler || handler.guid === handleObj.guid ) && + ( !tmp || tmp.test( handleObj.namespace ) ) && + ( !selector || selector === handleObj.selector || selector === "**" && handleObj.selector ) ) { + handlers.splice( j, 1 ); + + if ( handleObj.selector ) { + handlers.delegateCount--; + } + if ( special.remove ) { + special.remove.call( elem, handleObj ); + } + } + } + + // Remove generic event handler if we removed something and no more handlers exist + // (avoids potential for endless recursion during removal of special event handlers) + if ( origCount && !handlers.length ) { + if ( !special.teardown || special.teardown.call( elem, namespaces, elemData.handle ) === false ) { + jQuery.removeEvent( elem, type, elemData.handle ); + } + + delete events[ type ]; + } + } + + // Remove the expando if it's no longer used + if ( jQuery.isEmptyObject( events ) ) { + delete elemData.handle; + + // removeData also checks for emptiness and clears the expando if empty + // so use it instead of delete + jQuery._removeData( elem, "events" ); + } + }, + + trigger: function( event, data, elem, onlyHandlers ) { + var handle, ontype, cur, + bubbleType, special, tmp, i, + eventPath = [ elem || document ], + type = core_hasOwn.call( event, "type" ) ? event.type : event, + namespaces = core_hasOwn.call( event, "namespace" ) ? event.namespace.split(".") : []; + + cur = tmp = elem = elem || document; + + // Don't do events on text and comment nodes + if ( elem.nodeType === 3 || elem.nodeType === 8 ) { + return; + } + + // focus/blur morphs to focusin/out; ensure we're not firing them right now + if ( rfocusMorph.test( type + jQuery.event.triggered ) ) { + return; + } + + if ( type.indexOf(".") >= 0 ) { + // Namespaced trigger; create a regexp to match event type in handle() + namespaces = type.split("."); + type = namespaces.shift(); + namespaces.sort(); + } + ontype = type.indexOf(":") < 0 && "on" + type; + + // Caller can pass in a jQuery.Event object, Object, or just an event type string + event = event[ jQuery.expando ] ? + event : + new jQuery.Event( type, typeof event === "object" && event ); + + event.isTrigger = true; + event.namespace = namespaces.join("."); + event.namespace_re = event.namespace ? + new RegExp( "(^|\\.)" + namespaces.join("\\.(?:.*\\.|)") + "(\\.|$)" ) : + null; + + // Clean up the event in case it is being reused + event.result = undefined; + if ( !event.target ) { + event.target = elem; + } + + // Clone any incoming data and prepend the event, creating the handler arg list + data = data == null ? + [ event ] : + jQuery.makeArray( data, [ event ] ); + + // Allow special events to draw outside the lines + special = jQuery.event.special[ type ] || {}; + if ( !onlyHandlers && special.trigger && special.trigger.apply( elem, data ) === false ) { + return; + } + + // Determine event propagation path in advance, per W3C events spec (#9951) + // Bubble up to document, then to window; watch for a global ownerDocument var (#9724) + if ( !onlyHandlers && !special.noBubble && !jQuery.isWindow( elem ) ) { + + bubbleType = special.delegateType || type; + if ( !rfocusMorph.test( bubbleType + type ) ) { + cur = cur.parentNode; + } + for ( ; cur; cur = cur.parentNode ) { + eventPath.push( cur ); + tmp = cur; + } + + // Only add window if we got to document (e.g., not plain obj or detached DOM) + if ( tmp === (elem.ownerDocument || document) ) { + eventPath.push( tmp.defaultView || tmp.parentWindow || window ); + } + } + + // Fire handlers on the event path + i = 0; + while ( (cur = eventPath[i++]) && !event.isPropagationStopped() ) { + + event.type = i > 1 ? + bubbleType : + special.bindType || type; + + // jQuery handler + handle = ( jQuery._data( cur, "events" ) || {} )[ event.type ] && jQuery._data( cur, "handle" ); + if ( handle ) { + handle.apply( cur, data ); + } + + // Native handler + handle = ontype && cur[ ontype ]; + if ( handle && jQuery.acceptData( cur ) && handle.apply && handle.apply( cur, data ) === false ) { + event.preventDefault(); + } + } + event.type = type; + + // If nobody prevented the default action, do it now + if ( !onlyHandlers && !event.isDefaultPrevented() ) { + + if ( (!special._default || special._default.apply( elem.ownerDocument, data ) === false) && + !(type === "click" && jQuery.nodeName( elem, "a" )) && jQuery.acceptData( elem ) ) { + + // Call a native DOM method on the target with the same name name as the event. + // Can't use an .isFunction() check here because IE6/7 fails that test. + // Don't do default actions on window, that's where global variables be (#6170) + if ( ontype && elem[ type ] && !jQuery.isWindow( elem ) ) { + + // Don't re-trigger an onFOO event when we call its FOO() method + tmp = elem[ ontype ]; + + if ( tmp ) { + elem[ ontype ] = null; + } + + // Prevent re-triggering of the same event, since we already bubbled it above + jQuery.event.triggered = type; + try { + elem[ type ](); + } catch ( e ) { + // IE<9 dies on focus/blur to hidden element (#1486,#12518) + // only reproducible on winXP IE8 native, not IE9 in IE8 mode + } + jQuery.event.triggered = undefined; + + if ( tmp ) { + elem[ ontype ] = tmp; + } + } + } + } + + return event.result; + }, + + dispatch: function( event ) { + + // Make a writable jQuery.Event from the native event object + event = jQuery.event.fix( event ); + + var i, ret, handleObj, matched, j, + handlerQueue = [], + args = core_slice.call( arguments ), + handlers = ( jQuery._data( this, "events" ) || {} )[ event.type ] || [], + special = jQuery.event.special[ event.type ] || {}; + + // Use the fix-ed jQuery.Event rather than the (read-only) native event + args[0] = event; + event.delegateTarget = this; + + // Call the preDispatch hook for the mapped type, and let it bail if desired + if ( special.preDispatch && special.preDispatch.call( this, event ) === false ) { + return; + } + + // Determine handlers + handlerQueue = jQuery.event.handlers.call( this, event, handlers ); + + // Run delegates first; they may want to stop propagation beneath us + i = 0; + while ( (matched = handlerQueue[ i++ ]) && !event.isPropagationStopped() ) { + event.currentTarget = matched.elem; + + j = 0; + while ( (handleObj = matched.handlers[ j++ ]) && !event.isImmediatePropagationStopped() ) { + + // Triggered event must either 1) have no namespace, or + // 2) have namespace(s) a subset or equal to those in the bound event (both can have no namespace). + if ( !event.namespace_re || event.namespace_re.test( handleObj.namespace ) ) { + + event.handleObj = handleObj; + event.data = handleObj.data; + + ret = ( (jQuery.event.special[ handleObj.origType ] || {}).handle || handleObj.handler ) + .apply( matched.elem, args ); + + if ( ret !== undefined ) { + if ( (event.result = ret) === false ) { + event.preventDefault(); + event.stopPropagation(); + } + } + } + } + } + + // Call the postDispatch hook for the mapped type + if ( special.postDispatch ) { + special.postDispatch.call( this, event ); + } + + return event.result; + }, + + handlers: function( event, handlers ) { + var sel, handleObj, matches, i, + handlerQueue = [], + delegateCount = handlers.delegateCount, + cur = event.target; + + // Find delegate handlers + // Black-hole SVG instance trees (#13180) + // Avoid non-left-click bubbling in Firefox (#3861) + if ( delegateCount && cur.nodeType && (!event.button || event.type !== "click") ) { + + for ( ; cur != this; cur = cur.parentNode || this ) { + + // Don't check non-elements (#13208) + // Don't process clicks on disabled elements (#6911, #8165, #11382, #11764) + if ( cur.nodeType === 1 && (cur.disabled !== true || event.type !== "click") ) { + matches = []; + for ( i = 0; i < delegateCount; i++ ) { + handleObj = handlers[ i ]; + + // Don't conflict with Object.prototype properties (#13203) + sel = handleObj.selector + " "; + + if ( matches[ sel ] === undefined ) { + matches[ sel ] = handleObj.needsContext ? + jQuery( sel, this ).index( cur ) >= 0 : + jQuery.find( sel, this, null, [ cur ] ).length; + } + if ( matches[ sel ] ) { + matches.push( handleObj ); + } + } + if ( matches.length ) { + handlerQueue.push({ elem: cur, handlers: matches }); + } + } + } + } + + // Add the remaining (directly-bound) handlers + if ( delegateCount < handlers.length ) { + handlerQueue.push({ elem: this, handlers: handlers.slice( delegateCount ) }); + } + + return handlerQueue; + }, + + fix: function( event ) { + if ( event[ jQuery.expando ] ) { + return event; + } + + // Create a writable copy of the event object and normalize some properties + var i, prop, copy, + type = event.type, + originalEvent = event, + fixHook = this.fixHooks[ type ]; + + if ( !fixHook ) { + this.fixHooks[ type ] = fixHook = + rmouseEvent.test( type ) ? this.mouseHooks : + rkeyEvent.test( type ) ? this.keyHooks : + {}; + } + copy = fixHook.props ? this.props.concat( fixHook.props ) : this.props; + + event = new jQuery.Event( originalEvent ); + + i = copy.length; + while ( i-- ) { + prop = copy[ i ]; + event[ prop ] = originalEvent[ prop ]; + } + + // Support: IE<9 + // Fix target property (#1925) + if ( !event.target ) { + event.target = originalEvent.srcElement || document; + } + + // Support: Chrome 23+, Safari? + // Target should not be a text node (#504, #13143) + if ( event.target.nodeType === 3 ) { + event.target = event.target.parentNode; + } + + // Support: IE<9 + // For mouse/key events, metaKey==false if it's undefined (#3368, #11328) + event.metaKey = !!event.metaKey; + + return fixHook.filter ? fixHook.filter( event, originalEvent ) : event; + }, + + // Includes some event props shared by KeyEvent and MouseEvent + props: "altKey bubbles cancelable ctrlKey currentTarget eventPhase metaKey relatedTarget shiftKey target timeStamp view which".split(" "), + + fixHooks: {}, + + keyHooks: { + props: "char charCode key keyCode".split(" "), + filter: function( event, original ) { + + // Add which for key events + if ( event.which == null ) { + event.which = original.charCode != null ? original.charCode : original.keyCode; + } + + return event; + } + }, + + mouseHooks: { + props: "button buttons clientX clientY fromElement offsetX offsetY pageX pageY screenX screenY toElement".split(" "), + filter: function( event, original ) { + var body, eventDoc, doc, + button = original.button, + fromElement = original.fromElement; + + // Calculate pageX/Y if missing and clientX/Y available + if ( event.pageX == null && original.clientX != null ) { + eventDoc = event.target.ownerDocument || document; + doc = eventDoc.documentElement; + body = eventDoc.body; + + event.pageX = original.clientX + ( doc && doc.scrollLeft || body && body.scrollLeft || 0 ) - ( doc && doc.clientLeft || body && body.clientLeft || 0 ); + event.pageY = original.clientY + ( doc && doc.scrollTop || body && body.scrollTop || 0 ) - ( doc && doc.clientTop || body && body.clientTop || 0 ); + } + + // Add relatedTarget, if necessary + if ( !event.relatedTarget && fromElement ) { + event.relatedTarget = fromElement === event.target ? original.toElement : fromElement; + } + + // Add which for click: 1 === left; 2 === middle; 3 === right + // Note: button is not normalized, so don't use it + if ( !event.which && button !== undefined ) { + event.which = ( button & 1 ? 1 : ( button & 2 ? 3 : ( button & 4 ? 2 : 0 ) ) ); + } + + return event; + } + }, + + special: { + load: { + // Prevent triggered image.load events from bubbling to window.load + noBubble: true + }, + click: { + // For checkbox, fire native event so checked state will be right + trigger: function() { + if ( jQuery.nodeName( this, "input" ) && this.type === "checkbox" && this.click ) { + this.click(); + return false; + } + } + }, + focus: { + // Fire native event if possible so blur/focus sequence is correct + trigger: function() { + if ( this !== document.activeElement && this.focus ) { + try { + this.focus(); + return false; + } catch ( e ) { + // Support: IE<9 + // If we error on focus to hidden element (#1486, #12518), + // let .trigger() run the handlers + } + } + }, + delegateType: "focusin" + }, + blur: { + trigger: function() { + if ( this === document.activeElement && this.blur ) { + this.blur(); + return false; + } + }, + delegateType: "focusout" + }, + + beforeunload: { + postDispatch: function( event ) { + + // Even when returnValue equals to undefined Firefox will still show alert + if ( event.result !== undefined ) { + event.originalEvent.returnValue = event.result; + } + } + } + }, + + simulate: function( type, elem, event, bubble ) { + // Piggyback on a donor event to simulate a different one. + // Fake originalEvent to avoid donor's stopPropagation, but if the + // simulated event prevents default then we do the same on the donor. + var e = jQuery.extend( + new jQuery.Event(), + event, + { type: type, + isSimulated: true, + originalEvent: {} + } + ); + if ( bubble ) { + jQuery.event.trigger( e, null, elem ); + } else { + jQuery.event.dispatch.call( elem, e ); + } + if ( e.isDefaultPrevented() ) { + event.preventDefault(); + } + } +}; + +jQuery.removeEvent = document.removeEventListener ? + function( elem, type, handle ) { + if ( elem.removeEventListener ) { + elem.removeEventListener( type, handle, false ); + } + } : + function( elem, type, handle ) { + var name = "on" + type; + + if ( elem.detachEvent ) { + + // #8545, #7054, preventing memory leaks for custom events in IE6-8 + // detachEvent needed property on element, by name of that event, to properly expose it to GC + if ( typeof elem[ name ] === core_strundefined ) { + elem[ name ] = null; + } + + elem.detachEvent( name, handle ); + } + }; + +jQuery.Event = function( src, props ) { + // Allow instantiation without the 'new' keyword + if ( !(this instanceof jQuery.Event) ) { + return new jQuery.Event( src, props ); + } + + // Event object + if ( src && src.type ) { + this.originalEvent = src; + this.type = src.type; + + // Events bubbling up the document may have been marked as prevented + // by a handler lower down the tree; reflect the correct value. + this.isDefaultPrevented = ( src.defaultPrevented || src.returnValue === false || + src.getPreventDefault && src.getPreventDefault() ) ? returnTrue : returnFalse; + + // Event type + } else { + this.type = src; + } + + // Put explicitly provided properties onto the event object + if ( props ) { + jQuery.extend( this, props ); + } + + // Create a timestamp if incoming event doesn't have one + this.timeStamp = src && src.timeStamp || jQuery.now(); + + // Mark it as fixed + this[ jQuery.expando ] = true; +}; + +// jQuery.Event is based on DOM3 Events as specified by the ECMAScript Language Binding +// http://www.w3.org/TR/2003/WD-DOM-Level-3-Events-20030331/ecma-script-binding.html +jQuery.Event.prototype = { + isDefaultPrevented: returnFalse, + isPropagationStopped: returnFalse, + isImmediatePropagationStopped: returnFalse, + + preventDefault: function() { + var e = this.originalEvent; + + this.isDefaultPrevented = returnTrue; + if ( !e ) { + return; + } + + // If preventDefault exists, run it on the original event + if ( e.preventDefault ) { + e.preventDefault(); + + // Support: IE + // Otherwise set the returnValue property of the original event to false + } else { + e.returnValue = false; + } + }, + stopPropagation: function() { + var e = this.originalEvent; + + this.isPropagationStopped = returnTrue; + if ( !e ) { + return; + } + // If stopPropagation exists, run it on the original event + if ( e.stopPropagation ) { + e.stopPropagation(); + } + + // Support: IE + // Set the cancelBubble property of the original event to true + e.cancelBubble = true; + }, + stopImmediatePropagation: function() { + this.isImmediatePropagationStopped = returnTrue; + this.stopPropagation(); + } +}; + +// Create mouseenter/leave events using mouseover/out and event-time checks +jQuery.each({ + mouseenter: "mouseover", + mouseleave: "mouseout" +}, function( orig, fix ) { + jQuery.event.special[ orig ] = { + delegateType: fix, + bindType: fix, + + handle: function( event ) { + var ret, + target = this, + related = event.relatedTarget, + handleObj = event.handleObj; + + // For mousenter/leave call the handler if related is outside the target. + // NB: No relatedTarget if the mouse left/entered the browser window + if ( !related || (related !== target && !jQuery.contains( target, related )) ) { + event.type = handleObj.origType; + ret = handleObj.handler.apply( this, arguments ); + event.type = fix; + } + return ret; + } + }; +}); + +// IE submit delegation +if ( !jQuery.support.submitBubbles ) { + + jQuery.event.special.submit = { + setup: function() { + // Only need this for delegated form submit events + if ( jQuery.nodeName( this, "form" ) ) { + return false; + } + + // Lazy-add a submit handler when a descendant form may potentially be submitted + jQuery.event.add( this, "click._submit keypress._submit", function( e ) { + // Node name check avoids a VML-related crash in IE (#9807) + var elem = e.target, + form = jQuery.nodeName( elem, "input" ) || jQuery.nodeName( elem, "button" ) ? elem.form : undefined; + if ( form && !jQuery._data( form, "submitBubbles" ) ) { + jQuery.event.add( form, "submit._submit", function( event ) { + event._submit_bubble = true; + }); + jQuery._data( form, "submitBubbles", true ); + } + }); + // return undefined since we don't need an event listener + }, + + postDispatch: function( event ) { + // If form was submitted by the user, bubble the event up the tree + if ( event._submit_bubble ) { + delete event._submit_bubble; + if ( this.parentNode && !event.isTrigger ) { + jQuery.event.simulate( "submit", this.parentNode, event, true ); + } + } + }, + + teardown: function() { + // Only need this for delegated form submit events + if ( jQuery.nodeName( this, "form" ) ) { + return false; + } + + // Remove delegated handlers; cleanData eventually reaps submit handlers attached above + jQuery.event.remove( this, "._submit" ); + } + }; +} + +// IE change delegation and checkbox/radio fix +if ( !jQuery.support.changeBubbles ) { + + jQuery.event.special.change = { + + setup: function() { + + if ( rformElems.test( this.nodeName ) ) { + // IE doesn't fire change on a check/radio until blur; trigger it on click + // after a propertychange. Eat the blur-change in special.change.handle. + // This still fires onchange a second time for check/radio after blur. + if ( this.type === "checkbox" || this.type === "radio" ) { + jQuery.event.add( this, "propertychange._change", function( event ) { + if ( event.originalEvent.propertyName === "checked" ) { + this._just_changed = true; + } + }); + jQuery.event.add( this, "click._change", function( event ) { + if ( this._just_changed && !event.isTrigger ) { + this._just_changed = false; + } + // Allow triggered, simulated change events (#11500) + jQuery.event.simulate( "change", this, event, true ); + }); + } + return false; + } + // Delegated event; lazy-add a change handler on descendant inputs + jQuery.event.add( this, "beforeactivate._change", function( e ) { + var elem = e.target; + + if ( rformElems.test( elem.nodeName ) && !jQuery._data( elem, "changeBubbles" ) ) { + jQuery.event.add( elem, "change._change", function( event ) { + if ( this.parentNode && !event.isSimulated && !event.isTrigger ) { + jQuery.event.simulate( "change", this.parentNode, event, true ); + } + }); + jQuery._data( elem, "changeBubbles", true ); + } + }); + }, + + handle: function( event ) { + var elem = event.target; + + // Swallow native change events from checkbox/radio, we already triggered them above + if ( this !== elem || event.isSimulated || event.isTrigger || (elem.type !== "radio" && elem.type !== "checkbox") ) { + return event.handleObj.handler.apply( this, arguments ); + } + }, + + teardown: function() { + jQuery.event.remove( this, "._change" ); + + return !rformElems.test( this.nodeName ); + } + }; +} + +// Create "bubbling" focus and blur events +if ( !jQuery.support.focusinBubbles ) { + jQuery.each({ focus: "focusin", blur: "focusout" }, function( orig, fix ) { + + // Attach a single capturing handler while someone wants focusin/focusout + var attaches = 0, + handler = function( event ) { + jQuery.event.simulate( fix, event.target, jQuery.event.fix( event ), true ); + }; + + jQuery.event.special[ fix ] = { + setup: function() { + if ( attaches++ === 0 ) { + document.addEventListener( orig, handler, true ); + } + }, + teardown: function() { + if ( --attaches === 0 ) { + document.removeEventListener( orig, handler, true ); + } + } + }; + }); +} + +jQuery.fn.extend({ + + on: function( types, selector, data, fn, /*INTERNAL*/ one ) { + var type, origFn; + + // Types can be a map of types/handlers + if ( typeof types === "object" ) { + // ( types-Object, selector, data ) + if ( typeof selector !== "string" ) { + // ( types-Object, data ) + data = data || selector; + selector = undefined; + } + for ( type in types ) { + this.on( type, selector, data, types[ type ], one ); + } + return this; + } + + if ( data == null && fn == null ) { + // ( types, fn ) + fn = selector; + data = selector = undefined; + } else if ( fn == null ) { + if ( typeof selector === "string" ) { + // ( types, selector, fn ) + fn = data; + data = undefined; + } else { + // ( types, data, fn ) + fn = data; + data = selector; + selector = undefined; + } + } + if ( fn === false ) { + fn = returnFalse; + } else if ( !fn ) { + return this; + } + + if ( one === 1 ) { + origFn = fn; + fn = function( event ) { + // Can use an empty set, since event contains the info + jQuery().off( event ); + return origFn.apply( this, arguments ); + }; + // Use same guid so caller can remove using origFn + fn.guid = origFn.guid || ( origFn.guid = jQuery.guid++ ); + } + return this.each( function() { + jQuery.event.add( this, types, fn, data, selector ); + }); + }, + one: function( types, selector, data, fn ) { + return this.on( types, selector, data, fn, 1 ); + }, + off: function( types, selector, fn ) { + var handleObj, type; + if ( types && types.preventDefault && types.handleObj ) { + // ( event ) dispatched jQuery.Event + handleObj = types.handleObj; + jQuery( types.delegateTarget ).off( + handleObj.namespace ? handleObj.origType + "." + handleObj.namespace : handleObj.origType, + handleObj.selector, + handleObj.handler + ); + return this; + } + if ( typeof types === "object" ) { + // ( types-object [, selector] ) + for ( type in types ) { + this.off( type, selector, types[ type ] ); + } + return this; + } + if ( selector === false || typeof selector === "function" ) { + // ( types [, fn] ) + fn = selector; + selector = undefined; + } + if ( fn === false ) { + fn = returnFalse; + } + return this.each(function() { + jQuery.event.remove( this, types, fn, selector ); + }); + }, + + bind: function( types, data, fn ) { + return this.on( types, null, data, fn ); + }, + unbind: function( types, fn ) { + return this.off( types, null, fn ); + }, + + delegate: function( selector, types, data, fn ) { + return this.on( types, selector, data, fn ); + }, + undelegate: function( selector, types, fn ) { + // ( namespace ) or ( selector, types [, fn] ) + return arguments.length === 1 ? this.off( selector, "**" ) : this.off( types, selector || "**", fn ); + }, + + trigger: function( type, data ) { + return this.each(function() { + jQuery.event.trigger( type, data, this ); + }); + }, + triggerHandler: function( type, data ) { + var elem = this[0]; + if ( elem ) { + return jQuery.event.trigger( type, data, elem, true ); + } + } +}); +/*! + * Sizzle CSS Selector Engine + * Copyright 2012 jQuery Foundation and other contributors + * Released under the MIT license + * http://sizzlejs.com/ + */ +(function( window, undefined ) { + +var i, + cachedruns, + Expr, + getText, + isXML, + compile, + hasDuplicate, + outermostContext, + + // Local document vars + setDocument, + document, + docElem, + documentIsXML, + rbuggyQSA, + rbuggyMatches, + matches, + contains, + sortOrder, + + // Instance-specific data + expando = "sizzle" + -(new Date()), + preferredDoc = window.document, + support = {}, + dirruns = 0, + done = 0, + classCache = createCache(), + tokenCache = createCache(), + compilerCache = createCache(), + + // General-purpose constants + strundefined = typeof undefined, + MAX_NEGATIVE = 1 << 31, + + // Array methods + arr = [], + pop = arr.pop, + push = arr.push, + slice = arr.slice, + // Use a stripped-down indexOf if we can't use a native one + indexOf = arr.indexOf || function( elem ) { + var i = 0, + len = this.length; + for ( ; i < len; i++ ) { + if ( this[i] === elem ) { + return i; + } + } + return -1; + }, + + + // Regular expressions + + // Whitespace characters http://www.w3.org/TR/css3-selectors/#whitespace + whitespace = "[\\x20\\t\\r\\n\\f]", + // http://www.w3.org/TR/css3-syntax/#characters + characterEncoding = "(?:\\\\.|[\\w-]|[^\\x00-\\xa0])+", + + // Loosely modeled on CSS identifier characters + // An unquoted value should be a CSS identifier http://www.w3.org/TR/css3-selectors/#attribute-selectors + // Proper syntax: http://www.w3.org/TR/CSS21/syndata.html#value-def-identifier + identifier = characterEncoding.replace( "w", "w#" ), + + // Acceptable operators http://www.w3.org/TR/selectors/#attribute-selectors + operators = "([*^$|!~]?=)", + attributes = "\\[" + whitespace + "*(" + characterEncoding + ")" + whitespace + + "*(?:" + operators + whitespace + "*(?:(['\"])((?:\\\\.|[^\\\\])*?)\\3|(" + identifier + ")|)|)" + whitespace + "*\\]", + + // Prefer arguments quoted, + // then not containing pseudos/brackets, + // then attribute selectors/non-parenthetical expressions, + // then anything else + // These preferences are here to reduce the number of selectors + // needing tokenize in the PSEUDO preFilter + pseudos = ":(" + characterEncoding + ")(?:\\(((['\"])((?:\\\\.|[^\\\\])*?)\\3|((?:\\\\.|[^\\\\()[\\]]|" + attributes.replace( 3, 8 ) + ")*)|.*)\\)|)", + + // Leading and non-escaped trailing whitespace, capturing some non-whitespace characters preceding the latter + rtrim = new RegExp( "^" + whitespace + "+|((?:^|[^\\\\])(?:\\\\.)*)" + whitespace + "+$", "g" ), + + rcomma = new RegExp( "^" + whitespace + "*," + whitespace + "*" ), + rcombinators = new RegExp( "^" + whitespace + "*([\\x20\\t\\r\\n\\f>+~])" + whitespace + "*" ), + rpseudo = new RegExp( pseudos ), + ridentifier = new RegExp( "^" + identifier + "$" ), + + matchExpr = { + "ID": new RegExp( "^#(" + characterEncoding + ")" ), + "CLASS": new RegExp( "^\\.(" + characterEncoding + ")" ), + "NAME": new RegExp( "^\\[name=['\"]?(" + characterEncoding + ")['\"]?\\]" ), + "TAG": new RegExp( "^(" + characterEncoding.replace( "w", "w*" ) + ")" ), + "ATTR": new RegExp( "^" + attributes ), + "PSEUDO": new RegExp( "^" + pseudos ), + "CHILD": new RegExp( "^:(only|first|last|nth|nth-last)-(child|of-type)(?:\\(" + whitespace + + "*(even|odd|(([+-]|)(\\d*)n|)" + whitespace + "*(?:([+-]|)" + whitespace + + "*(\\d+)|))" + whitespace + "*\\)|)", "i" ), + // For use in libraries implementing .is() + // We use this for POS matching in `select` + "needsContext": new RegExp( "^" + whitespace + "*[>+~]|:(even|odd|eq|gt|lt|nth|first|last)(?:\\(" + + whitespace + "*((?:-\\d)?\\d*)" + whitespace + "*\\)|)(?=[^-]|$)", "i" ) + }, + + rsibling = /[\x20\t\r\n\f]*[+~]/, + + rnative = /^[^{]+\{\s*\[native code/, + + // Easily-parseable/retrievable ID or TAG or CLASS selectors + rquickExpr = /^(?:#([\w-]+)|(\w+)|\.([\w-]+))$/, + + rinputs = /^(?:input|select|textarea|button)$/i, + rheader = /^h\d$/i, + + rescape = /'|\\/g, + rattributeQuotes = /\=[\x20\t\r\n\f]*([^'"\]]*)[\x20\t\r\n\f]*\]/g, + + // CSS escapes http://www.w3.org/TR/CSS21/syndata.html#escaped-characters + runescape = /\\([\da-fA-F]{1,6}[\x20\t\r\n\f]?|.)/g, + funescape = function( _, escaped ) { + var high = "0x" + escaped - 0x10000; + // NaN means non-codepoint + return high !== high ? + escaped : + // BMP codepoint + high < 0 ? + String.fromCharCode( high + 0x10000 ) : + // Supplemental Plane codepoint (surrogate pair) + String.fromCharCode( high >> 10 | 0xD800, high & 0x3FF | 0xDC00 ); + }; + +// Use a stripped-down slice if we can't use a native one +try { + slice.call( preferredDoc.documentElement.childNodes, 0 )[0].nodeType; +} catch ( e ) { + slice = function( i ) { + var elem, + results = []; + while ( (elem = this[i++]) ) { + results.push( elem ); + } + return results; + }; +} + +/** + * For feature detection + * @param {Function} fn The function to test for native support + */ +function isNative( fn ) { + return rnative.test( fn + "" ); +} + +/** + * Create key-value caches of limited size + * @returns {Function(string, Object)} Returns the Object data after storing it on itself with + * property name the (space-suffixed) string and (if the cache is larger than Expr.cacheLength) + * deleting the oldest entry + */ +function createCache() { + var cache, + keys = []; + + return (cache = function( key, value ) { + // Use (key + " ") to avoid collision with native prototype properties (see Issue #157) + if ( keys.push( key += " " ) > Expr.cacheLength ) { + // Only keep the most recent entries + delete cache[ keys.shift() ]; + } + return (cache[ key ] = value); + }); +} + +/** + * Mark a function for special use by Sizzle + * @param {Function} fn The function to mark + */ +function markFunction( fn ) { + fn[ expando ] = true; + return fn; +} + +/** + * Support testing using an element + * @param {Function} fn Passed the created div and expects a boolean result + */ +function assert( fn ) { + var div = document.createElement("div"); + + try { + return fn( div ); + } catch (e) { + return false; + } finally { + // release memory in IE + div = null; + } +} + +function Sizzle( selector, context, results, seed ) { + var match, elem, m, nodeType, + // QSA vars + i, groups, old, nid, newContext, newSelector; + + if ( ( context ? context.ownerDocument || context : preferredDoc ) !== document ) { + setDocument( context ); + } + + context = context || document; + results = results || []; + + if ( !selector || typeof selector !== "string" ) { + return results; + } + + if ( (nodeType = context.nodeType) !== 1 && nodeType !== 9 ) { + return []; + } + + if ( !documentIsXML && !seed ) { + + // Shortcuts + if ( (match = rquickExpr.exec( selector )) ) { + // Speed-up: Sizzle("#ID") + if ( (m = match[1]) ) { + if ( nodeType === 9 ) { + elem = context.getElementById( m ); + // Check parentNode to catch when Blackberry 4.6 returns + // nodes that are no longer in the document #6963 + if ( elem && elem.parentNode ) { + // Handle the case where IE, Opera, and Webkit return items + // by name instead of ID + if ( elem.id === m ) { + results.push( elem ); + return results; + } + } else { + return results; + } + } else { + // Context is not a document + if ( context.ownerDocument && (elem = context.ownerDocument.getElementById( m )) && + contains( context, elem ) && elem.id === m ) { + results.push( elem ); + return results; + } + } + + // Speed-up: Sizzle("TAG") + } else if ( match[2] ) { + push.apply( results, slice.call(context.getElementsByTagName( selector ), 0) ); + return results; + + // Speed-up: Sizzle(".CLASS") + } else if ( (m = match[3]) && support.getByClassName && context.getElementsByClassName ) { + push.apply( results, slice.call(context.getElementsByClassName( m ), 0) ); + return results; + } + } + + // QSA path + if ( support.qsa && !rbuggyQSA.test(selector) ) { + old = true; + nid = expando; + newContext = context; + newSelector = nodeType === 9 && selector; + + // qSA works strangely on Element-rooted queries + // We can work around this by specifying an extra ID on the root + // and working up from there (Thanks to Andrew Dupont for the technique) + // IE 8 doesn't work on object elements + if ( nodeType === 1 && context.nodeName.toLowerCase() !== "object" ) { + groups = tokenize( selector ); + + if ( (old = context.getAttribute("id")) ) { + nid = old.replace( rescape, "\\$&" ); + } else { + context.setAttribute( "id", nid ); + } + nid = "[id='" + nid + "'] "; + + i = groups.length; + while ( i-- ) { + groups[i] = nid + toSelector( groups[i] ); + } + newContext = rsibling.test( selector ) && context.parentNode || context; + newSelector = groups.join(","); + } + + if ( newSelector ) { + try { + push.apply( results, slice.call( newContext.querySelectorAll( + newSelector + ), 0 ) ); + return results; + } catch(qsaError) { + } finally { + if ( !old ) { + context.removeAttribute("id"); + } + } + } + } + } + + // All others + return select( selector.replace( rtrim, "$1" ), context, results, seed ); +} + +/** + * Detect xml + * @param {Element|Object} elem An element or a document + */ +isXML = Sizzle.isXML = function( elem ) { + // documentElement is verified for cases where it doesn't yet exist + // (such as loading iframes in IE - #4833) + var documentElement = elem && (elem.ownerDocument || elem).documentElement; + return documentElement ? documentElement.nodeName !== "HTML" : false; +}; + +/** + * Sets document-related variables once based on the current document + * @param {Element|Object} [doc] An element or document object to use to set the document + * @returns {Object} Returns the current document + */ +setDocument = Sizzle.setDocument = function( node ) { + var doc = node ? node.ownerDocument || node : preferredDoc; + + // If no document and documentElement is available, return + if ( doc === document || doc.nodeType !== 9 || !doc.documentElement ) { + return document; + } + + // Set our document + document = doc; + docElem = doc.documentElement; + + // Support tests + documentIsXML = isXML( doc ); + + // Check if getElementsByTagName("*") returns only elements + support.tagNameNoComments = assert(function( div ) { + div.appendChild( doc.createComment("") ); + return !div.getElementsByTagName("*").length; + }); + + // Check if attributes should be retrieved by attribute nodes + support.attributes = assert(function( div ) { + div.innerHTML = ""; + var type = typeof div.lastChild.getAttribute("multiple"); + // IE8 returns a string for some attributes even when not present + return type !== "boolean" && type !== "string"; + }); + + // Check if getElementsByClassName can be trusted + support.getByClassName = assert(function( div ) { + // Opera can't find a second classname (in 9.6) + div.innerHTML = ""; + if ( !div.getElementsByClassName || !div.getElementsByClassName("e").length ) { + return false; + } + + // Safari 3.2 caches class attributes and doesn't catch changes + div.lastChild.className = "e"; + return div.getElementsByClassName("e").length === 2; + }); + + // Check if getElementById returns elements by name + // Check if getElementsByName privileges form controls or returns elements by ID + support.getByName = assert(function( div ) { + // Inject content + div.id = expando + 0; + div.innerHTML = "
"; + docElem.insertBefore( div, docElem.firstChild ); + + // Test + var pass = doc.getElementsByName && + // buggy browsers will return fewer than the correct 2 + doc.getElementsByName( expando ).length === 2 + + // buggy browsers will return more than the correct 0 + doc.getElementsByName( expando + 0 ).length; + support.getIdNotName = !doc.getElementById( expando ); + + // Cleanup + docElem.removeChild( div ); + + return pass; + }); + + // IE6/7 return modified attributes + Expr.attrHandle = assert(function( div ) { + div.innerHTML = ""; + return div.firstChild && typeof div.firstChild.getAttribute !== strundefined && + div.firstChild.getAttribute("href") === "#"; + }) ? + {} : + { + "href": function( elem ) { + return elem.getAttribute( "href", 2 ); + }, + "type": function( elem ) { + return elem.getAttribute("type"); + } + }; + + // ID find and filter + if ( support.getIdNotName ) { + Expr.find["ID"] = function( id, context ) { + if ( typeof context.getElementById !== strundefined && !documentIsXML ) { + var m = context.getElementById( id ); + // Check parentNode to catch when Blackberry 4.6 returns + // nodes that are no longer in the document #6963 + return m && m.parentNode ? [m] : []; + } + }; + Expr.filter["ID"] = function( id ) { + var attrId = id.replace( runescape, funescape ); + return function( elem ) { + return elem.getAttribute("id") === attrId; + }; + }; + } else { + Expr.find["ID"] = function( id, context ) { + if ( typeof context.getElementById !== strundefined && !documentIsXML ) { + var m = context.getElementById( id ); + + return m ? + m.id === id || typeof m.getAttributeNode !== strundefined && m.getAttributeNode("id").value === id ? + [m] : + undefined : + []; + } + }; + Expr.filter["ID"] = function( id ) { + var attrId = id.replace( runescape, funescape ); + return function( elem ) { + var node = typeof elem.getAttributeNode !== strundefined && elem.getAttributeNode("id"); + return node && node.value === attrId; + }; + }; + } + + // Tag + Expr.find["TAG"] = support.tagNameNoComments ? + function( tag, context ) { + if ( typeof context.getElementsByTagName !== strundefined ) { + return context.getElementsByTagName( tag ); + } + } : + function( tag, context ) { + var elem, + tmp = [], + i = 0, + results = context.getElementsByTagName( tag ); + + // Filter out possible comments + if ( tag === "*" ) { + while ( (elem = results[i++]) ) { + if ( elem.nodeType === 1 ) { + tmp.push( elem ); + } + } + + return tmp; + } + return results; + }; + + // Name + Expr.find["NAME"] = support.getByName && function( tag, context ) { + if ( typeof context.getElementsByName !== strundefined ) { + return context.getElementsByName( name ); + } + }; + + // Class + Expr.find["CLASS"] = support.getByClassName && function( className, context ) { + if ( typeof context.getElementsByClassName !== strundefined && !documentIsXML ) { + return context.getElementsByClassName( className ); + } + }; + + // QSA and matchesSelector support + + // matchesSelector(:active) reports false when true (IE9/Opera 11.5) + rbuggyMatches = []; + + // qSa(:focus) reports false when true (Chrome 21), + // no need to also add to buggyMatches since matches checks buggyQSA + // A support test would require too much code (would include document ready) + rbuggyQSA = [ ":focus" ]; + + if ( (support.qsa = isNative(doc.querySelectorAll)) ) { + // Build QSA regex + // Regex strategy adopted from Diego Perini + assert(function( div ) { + // Select is set to empty string on purpose + // This is to test IE's treatment of not explictly + // setting a boolean content attribute, + // since its presence should be enough + // http://bugs.jquery.com/ticket/12359 + div.innerHTML = ""; + + // IE8 - Some boolean attributes are not treated correctly + if ( !div.querySelectorAll("[selected]").length ) { + rbuggyQSA.push( "\\[" + whitespace + "*(?:checked|disabled|ismap|multiple|readonly|selected|value)" ); + } + + // Webkit/Opera - :checked should return selected option elements + // http://www.w3.org/TR/2011/REC-css3-selectors-20110929/#checked + // IE8 throws error here and will not see later tests + if ( !div.querySelectorAll(":checked").length ) { + rbuggyQSA.push(":checked"); + } + }); + + assert(function( div ) { + + // Opera 10-12/IE8 - ^= $= *= and empty values + // Should not select anything + div.innerHTML = ""; + if ( div.querySelectorAll("[i^='']").length ) { + rbuggyQSA.push( "[*^$]=" + whitespace + "*(?:\"\"|'')" ); + } + + // FF 3.5 - :enabled/:disabled and hidden elements (hidden elements are still enabled) + // IE8 throws error here and will not see later tests + if ( !div.querySelectorAll(":enabled").length ) { + rbuggyQSA.push( ":enabled", ":disabled" ); + } + + // Opera 10-11 does not throw on post-comma invalid pseudos + div.querySelectorAll("*,:x"); + rbuggyQSA.push(",.*:"); + }); + } + + if ( (support.matchesSelector = isNative( (matches = docElem.matchesSelector || + docElem.mozMatchesSelector || + docElem.webkitMatchesSelector || + docElem.oMatchesSelector || + docElem.msMatchesSelector) )) ) { + + assert(function( div ) { + // Check to see if it's possible to do matchesSelector + // on a disconnected node (IE 9) + support.disconnectedMatch = matches.call( div, "div" ); + + // This should fail with an exception + // Gecko does not error, returns false instead + matches.call( div, "[s!='']:x" ); + rbuggyMatches.push( "!=", pseudos ); + }); + } + + rbuggyQSA = new RegExp( rbuggyQSA.join("|") ); + rbuggyMatches = new RegExp( rbuggyMatches.join("|") ); + + // Element contains another + // Purposefully does not implement inclusive descendent + // As in, an element does not contain itself + contains = isNative(docElem.contains) || docElem.compareDocumentPosition ? + function( a, b ) { + var adown = a.nodeType === 9 ? a.documentElement : a, + bup = b && b.parentNode; + return a === bup || !!( bup && bup.nodeType === 1 && ( + adown.contains ? + adown.contains( bup ) : + a.compareDocumentPosition && a.compareDocumentPosition( bup ) & 16 + )); + } : + function( a, b ) { + if ( b ) { + while ( (b = b.parentNode) ) { + if ( b === a ) { + return true; + } + } + } + return false; + }; + + // Document order sorting + sortOrder = docElem.compareDocumentPosition ? + function( a, b ) { + var compare; + + if ( a === b ) { + hasDuplicate = true; + return 0; + } + + if ( (compare = b.compareDocumentPosition && a.compareDocumentPosition && a.compareDocumentPosition( b )) ) { + if ( compare & 1 || a.parentNode && a.parentNode.nodeType === 11 ) { + if ( a === doc || contains( preferredDoc, a ) ) { + return -1; + } + if ( b === doc || contains( preferredDoc, b ) ) { + return 1; + } + return 0; + } + return compare & 4 ? -1 : 1; + } + + return a.compareDocumentPosition ? -1 : 1; + } : + function( a, b ) { + var cur, + i = 0, + aup = a.parentNode, + bup = b.parentNode, + ap = [ a ], + bp = [ b ]; + + // Exit early if the nodes are identical + if ( a === b ) { + hasDuplicate = true; + return 0; + + // Parentless nodes are either documents or disconnected + } else if ( !aup || !bup ) { + return a === doc ? -1 : + b === doc ? 1 : + aup ? -1 : + bup ? 1 : + 0; + + // If the nodes are siblings, we can do a quick check + } else if ( aup === bup ) { + return siblingCheck( a, b ); + } + + // Otherwise we need full lists of their ancestors for comparison + cur = a; + while ( (cur = cur.parentNode) ) { + ap.unshift( cur ); + } + cur = b; + while ( (cur = cur.parentNode) ) { + bp.unshift( cur ); + } + + // Walk down the tree looking for a discrepancy + while ( ap[i] === bp[i] ) { + i++; + } + + return i ? + // Do a sibling check if the nodes have a common ancestor + siblingCheck( ap[i], bp[i] ) : + + // Otherwise nodes in our document sort first + ap[i] === preferredDoc ? -1 : + bp[i] === preferredDoc ? 1 : + 0; + }; + + // Always assume the presence of duplicates if sort doesn't + // pass them to our comparison function (as in Google Chrome). + hasDuplicate = false; + [0, 0].sort( sortOrder ); + support.detectDuplicates = hasDuplicate; + + return document; +}; + +Sizzle.matches = function( expr, elements ) { + return Sizzle( expr, null, null, elements ); +}; + +Sizzle.matchesSelector = function( elem, expr ) { + // Set document vars if needed + if ( ( elem.ownerDocument || elem ) !== document ) { + setDocument( elem ); + } + + // Make sure that attribute selectors are quoted + expr = expr.replace( rattributeQuotes, "='$1']" ); + + // rbuggyQSA always contains :focus, so no need for an existence check + if ( support.matchesSelector && !documentIsXML && (!rbuggyMatches || !rbuggyMatches.test(expr)) && !rbuggyQSA.test(expr) ) { + try { + var ret = matches.call( elem, expr ); + + // IE 9's matchesSelector returns false on disconnected nodes + if ( ret || support.disconnectedMatch || + // As well, disconnected nodes are said to be in a document + // fragment in IE 9 + elem.document && elem.document.nodeType !== 11 ) { + return ret; + } + } catch(e) {} + } + + return Sizzle( expr, document, null, [elem] ).length > 0; +}; + +Sizzle.contains = function( context, elem ) { + // Set document vars if needed + if ( ( context.ownerDocument || context ) !== document ) { + setDocument( context ); + } + return contains( context, elem ); +}; + +Sizzle.attr = function( elem, name ) { + var val; + + // Set document vars if needed + if ( ( elem.ownerDocument || elem ) !== document ) { + setDocument( elem ); + } + + if ( !documentIsXML ) { + name = name.toLowerCase(); + } + if ( (val = Expr.attrHandle[ name ]) ) { + return val( elem ); + } + if ( documentIsXML || support.attributes ) { + return elem.getAttribute( name ); + } + return ( (val = elem.getAttributeNode( name )) || elem.getAttribute( name ) ) && elem[ name ] === true ? + name : + val && val.specified ? val.value : null; +}; + +Sizzle.error = function( msg ) { + throw new Error( "Syntax error, unrecognized expression: " + msg ); +}; + +// Document sorting and removing duplicates +Sizzle.uniqueSort = function( results ) { + var elem, + duplicates = [], + i = 1, + j = 0; + + // Unless we *know* we can detect duplicates, assume their presence + hasDuplicate = !support.detectDuplicates; + results.sort( sortOrder ); + + if ( hasDuplicate ) { + for ( ; (elem = results[i]); i++ ) { + if ( elem === results[ i - 1 ] ) { + j = duplicates.push( i ); + } + } + while ( j-- ) { + results.splice( duplicates[ j ], 1 ); + } + } + + return results; +}; + +function siblingCheck( a, b ) { + var cur = b && a, + diff = cur && ( ~b.sourceIndex || MAX_NEGATIVE ) - ( ~a.sourceIndex || MAX_NEGATIVE ); + + // Use IE sourceIndex if available on both nodes + if ( diff ) { + return diff; + } + + // Check if b follows a + if ( cur ) { + while ( (cur = cur.nextSibling) ) { + if ( cur === b ) { + return -1; + } + } + } + + return a ? 1 : -1; +} + +// Returns a function to use in pseudos for input types +function createInputPseudo( type ) { + return function( elem ) { + var name = elem.nodeName.toLowerCase(); + return name === "input" && elem.type === type; + }; +} + +// Returns a function to use in pseudos for buttons +function createButtonPseudo( type ) { + return function( elem ) { + var name = elem.nodeName.toLowerCase(); + return (name === "input" || name === "button") && elem.type === type; + }; +} + +// Returns a function to use in pseudos for positionals +function createPositionalPseudo( fn ) { + return markFunction(function( argument ) { + argument = +argument; + return markFunction(function( seed, matches ) { + var j, + matchIndexes = fn( [], seed.length, argument ), + i = matchIndexes.length; + + // Match elements found at the specified indexes + while ( i-- ) { + if ( seed[ (j = matchIndexes[i]) ] ) { + seed[j] = !(matches[j] = seed[j]); + } + } + }); + }); +} + +/** + * Utility function for retrieving the text value of an array of DOM nodes + * @param {Array|Element} elem + */ +getText = Sizzle.getText = function( elem ) { + var node, + ret = "", + i = 0, + nodeType = elem.nodeType; + + if ( !nodeType ) { + // If no nodeType, this is expected to be an array + for ( ; (node = elem[i]); i++ ) { + // Do not traverse comment nodes + ret += getText( node ); + } + } else if ( nodeType === 1 || nodeType === 9 || nodeType === 11 ) { + // Use textContent for elements + // innerText usage removed for consistency of new lines (see #11153) + if ( typeof elem.textContent === "string" ) { + return elem.textContent; + } else { + // Traverse its children + for ( elem = elem.firstChild; elem; elem = elem.nextSibling ) { + ret += getText( elem ); + } + } + } else if ( nodeType === 3 || nodeType === 4 ) { + return elem.nodeValue; + } + // Do not include comment or processing instruction nodes + + return ret; +}; + +Expr = Sizzle.selectors = { + + // Can be adjusted by the user + cacheLength: 50, + + createPseudo: markFunction, + + match: matchExpr, + + find: {}, + + relative: { + ">": { dir: "parentNode", first: true }, + " ": { dir: "parentNode" }, + "+": { dir: "previousSibling", first: true }, + "~": { dir: "previousSibling" } + }, + + preFilter: { + "ATTR": function( match ) { + match[1] = match[1].replace( runescape, funescape ); + + // Move the given value to match[3] whether quoted or unquoted + match[3] = ( match[4] || match[5] || "" ).replace( runescape, funescape ); + + if ( match[2] === "~=" ) { + match[3] = " " + match[3] + " "; + } + + return match.slice( 0, 4 ); + }, + + "CHILD": function( match ) { + /* matches from matchExpr["CHILD"] + 1 type (only|nth|...) + 2 what (child|of-type) + 3 argument (even|odd|\d*|\d*n([+-]\d+)?|...) + 4 xn-component of xn+y argument ([+-]?\d*n|) + 5 sign of xn-component + 6 x of xn-component + 7 sign of y-component + 8 y of y-component + */ + match[1] = match[1].toLowerCase(); + + if ( match[1].slice( 0, 3 ) === "nth" ) { + // nth-* requires argument + if ( !match[3] ) { + Sizzle.error( match[0] ); + } + + // numeric x and y parameters for Expr.filter.CHILD + // remember that false/true cast respectively to 0/1 + match[4] = +( match[4] ? match[5] + (match[6] || 1) : 2 * ( match[3] === "even" || match[3] === "odd" ) ); + match[5] = +( ( match[7] + match[8] ) || match[3] === "odd" ); + + // other types prohibit arguments + } else if ( match[3] ) { + Sizzle.error( match[0] ); + } + + return match; + }, + + "PSEUDO": function( match ) { + var excess, + unquoted = !match[5] && match[2]; + + if ( matchExpr["CHILD"].test( match[0] ) ) { + return null; + } + + // Accept quoted arguments as-is + if ( match[4] ) { + match[2] = match[4]; + + // Strip excess characters from unquoted arguments + } else if ( unquoted && rpseudo.test( unquoted ) && + // Get excess from tokenize (recursively) + (excess = tokenize( unquoted, true )) && + // advance to the next closing parenthesis + (excess = unquoted.indexOf( ")", unquoted.length - excess ) - unquoted.length) ) { + + // excess is a negative index + match[0] = match[0].slice( 0, excess ); + match[2] = unquoted.slice( 0, excess ); + } + + // Return only captures needed by the pseudo filter method (type and argument) + return match.slice( 0, 3 ); + } + }, + + filter: { + + "TAG": function( nodeName ) { + if ( nodeName === "*" ) { + return function() { return true; }; + } + + nodeName = nodeName.replace( runescape, funescape ).toLowerCase(); + return function( elem ) { + return elem.nodeName && elem.nodeName.toLowerCase() === nodeName; + }; + }, + + "CLASS": function( className ) { + var pattern = classCache[ className + " " ]; + + return pattern || + (pattern = new RegExp( "(^|" + whitespace + ")" + className + "(" + whitespace + "|$)" )) && + classCache( className, function( elem ) { + return pattern.test( elem.className || (typeof elem.getAttribute !== strundefined && elem.getAttribute("class")) || "" ); + }); + }, + + "ATTR": function( name, operator, check ) { + return function( elem ) { + var result = Sizzle.attr( elem, name ); + + if ( result == null ) { + return operator === "!="; + } + if ( !operator ) { + return true; + } + + result += ""; + + return operator === "=" ? result === check : + operator === "!=" ? result !== check : + operator === "^=" ? check && result.indexOf( check ) === 0 : + operator === "*=" ? check && result.indexOf( check ) > -1 : + operator === "$=" ? check && result.slice( -check.length ) === check : + operator === "~=" ? ( " " + result + " " ).indexOf( check ) > -1 : + operator === "|=" ? result === check || result.slice( 0, check.length + 1 ) === check + "-" : + false; + }; + }, + + "CHILD": function( type, what, argument, first, last ) { + var simple = type.slice( 0, 3 ) !== "nth", + forward = type.slice( -4 ) !== "last", + ofType = what === "of-type"; + + return first === 1 && last === 0 ? + + // Shortcut for :nth-*(n) + function( elem ) { + return !!elem.parentNode; + } : + + function( elem, context, xml ) { + var cache, outerCache, node, diff, nodeIndex, start, + dir = simple !== forward ? "nextSibling" : "previousSibling", + parent = elem.parentNode, + name = ofType && elem.nodeName.toLowerCase(), + useCache = !xml && !ofType; + + if ( parent ) { + + // :(first|last|only)-(child|of-type) + if ( simple ) { + while ( dir ) { + node = elem; + while ( (node = node[ dir ]) ) { + if ( ofType ? node.nodeName.toLowerCase() === name : node.nodeType === 1 ) { + return false; + } + } + // Reverse direction for :only-* (if we haven't yet done so) + start = dir = type === "only" && !start && "nextSibling"; + } + return true; + } + + start = [ forward ? parent.firstChild : parent.lastChild ]; + + // non-xml :nth-child(...) stores cache data on `parent` + if ( forward && useCache ) { + // Seek `elem` from a previously-cached index + outerCache = parent[ expando ] || (parent[ expando ] = {}); + cache = outerCache[ type ] || []; + nodeIndex = cache[0] === dirruns && cache[1]; + diff = cache[0] === dirruns && cache[2]; + node = nodeIndex && parent.childNodes[ nodeIndex ]; + + while ( (node = ++nodeIndex && node && node[ dir ] || + + // Fallback to seeking `elem` from the start + (diff = nodeIndex = 0) || start.pop()) ) { + + // When found, cache indexes on `parent` and break + if ( node.nodeType === 1 && ++diff && node === elem ) { + outerCache[ type ] = [ dirruns, nodeIndex, diff ]; + break; + } + } + + // Use previously-cached element index if available + } else if ( useCache && (cache = (elem[ expando ] || (elem[ expando ] = {}))[ type ]) && cache[0] === dirruns ) { + diff = cache[1]; + + // xml :nth-child(...) or :nth-last-child(...) or :nth(-last)?-of-type(...) + } else { + // Use the same loop as above to seek `elem` from the start + while ( (node = ++nodeIndex && node && node[ dir ] || + (diff = nodeIndex = 0) || start.pop()) ) { + + if ( ( ofType ? node.nodeName.toLowerCase() === name : node.nodeType === 1 ) && ++diff ) { + // Cache the index of each encountered element + if ( useCache ) { + (node[ expando ] || (node[ expando ] = {}))[ type ] = [ dirruns, diff ]; + } + + if ( node === elem ) { + break; + } + } + } + } + + // Incorporate the offset, then check against cycle size + diff -= last; + return diff === first || ( diff % first === 0 && diff / first >= 0 ); + } + }; + }, + + "PSEUDO": function( pseudo, argument ) { + // pseudo-class names are case-insensitive + // http://www.w3.org/TR/selectors/#pseudo-classes + // Prioritize by case sensitivity in case custom pseudos are added with uppercase letters + // Remember that setFilters inherits from pseudos + var args, + fn = Expr.pseudos[ pseudo ] || Expr.setFilters[ pseudo.toLowerCase() ] || + Sizzle.error( "unsupported pseudo: " + pseudo ); + + // The user may use createPseudo to indicate that + // arguments are needed to create the filter function + // just as Sizzle does + if ( fn[ expando ] ) { + return fn( argument ); + } + + // But maintain support for old signatures + if ( fn.length > 1 ) { + args = [ pseudo, pseudo, "", argument ]; + return Expr.setFilters.hasOwnProperty( pseudo.toLowerCase() ) ? + markFunction(function( seed, matches ) { + var idx, + matched = fn( seed, argument ), + i = matched.length; + while ( i-- ) { + idx = indexOf.call( seed, matched[i] ); + seed[ idx ] = !( matches[ idx ] = matched[i] ); + } + }) : + function( elem ) { + return fn( elem, 0, args ); + }; + } + + return fn; + } + }, + + pseudos: { + // Potentially complex pseudos + "not": markFunction(function( selector ) { + // Trim the selector passed to compile + // to avoid treating leading and trailing + // spaces as combinators + var input = [], + results = [], + matcher = compile( selector.replace( rtrim, "$1" ) ); + + return matcher[ expando ] ? + markFunction(function( seed, matches, context, xml ) { + var elem, + unmatched = matcher( seed, null, xml, [] ), + i = seed.length; + + // Match elements unmatched by `matcher` + while ( i-- ) { + if ( (elem = unmatched[i]) ) { + seed[i] = !(matches[i] = elem); + } + } + }) : + function( elem, context, xml ) { + input[0] = elem; + matcher( input, null, xml, results ); + return !results.pop(); + }; + }), + + "has": markFunction(function( selector ) { + return function( elem ) { + return Sizzle( selector, elem ).length > 0; + }; + }), + + "contains": markFunction(function( text ) { + return function( elem ) { + return ( elem.textContent || elem.innerText || getText( elem ) ).indexOf( text ) > -1; + }; + }), + + // "Whether an element is represented by a :lang() selector + // is based solely on the element's language value + // being equal to the identifier C, + // or beginning with the identifier C immediately followed by "-". + // The matching of C against the element's language value is performed case-insensitively. + // The identifier C does not have to be a valid language name." + // http://www.w3.org/TR/selectors/#lang-pseudo + "lang": markFunction( function( lang ) { + // lang value must be a valid identifider + if ( !ridentifier.test(lang || "") ) { + Sizzle.error( "unsupported lang: " + lang ); + } + lang = lang.replace( runescape, funescape ).toLowerCase(); + return function( elem ) { + var elemLang; + do { + if ( (elemLang = documentIsXML ? + elem.getAttribute("xml:lang") || elem.getAttribute("lang") : + elem.lang) ) { + + elemLang = elemLang.toLowerCase(); + return elemLang === lang || elemLang.indexOf( lang + "-" ) === 0; + } + } while ( (elem = elem.parentNode) && elem.nodeType === 1 ); + return false; + }; + }), + + // Miscellaneous + "target": function( elem ) { + var hash = window.location && window.location.hash; + return hash && hash.slice( 1 ) === elem.id; + }, + + "root": function( elem ) { + return elem === docElem; + }, + + "focus": function( elem ) { + return elem === document.activeElement && (!document.hasFocus || document.hasFocus()) && !!(elem.type || elem.href || ~elem.tabIndex); + }, + + // Boolean properties + "enabled": function( elem ) { + return elem.disabled === false; + }, + + "disabled": function( elem ) { + return elem.disabled === true; + }, + + "checked": function( elem ) { + // In CSS3, :checked should return both checked and selected elements + // http://www.w3.org/TR/2011/REC-css3-selectors-20110929/#checked + var nodeName = elem.nodeName.toLowerCase(); + return (nodeName === "input" && !!elem.checked) || (nodeName === "option" && !!elem.selected); + }, + + "selected": function( elem ) { + // Accessing this property makes selected-by-default + // options in Safari work properly + if ( elem.parentNode ) { + elem.parentNode.selectedIndex; + } + + return elem.selected === true; + }, + + // Contents + "empty": function( elem ) { + // http://www.w3.org/TR/selectors/#empty-pseudo + // :empty is only affected by element nodes and content nodes(including text(3), cdata(4)), + // not comment, processing instructions, or others + // Thanks to Diego Perini for the nodeName shortcut + // Greater than "@" means alpha characters (specifically not starting with "#" or "?") + for ( elem = elem.firstChild; elem; elem = elem.nextSibling ) { + if ( elem.nodeName > "@" || elem.nodeType === 3 || elem.nodeType === 4 ) { + return false; + } + } + return true; + }, + + "parent": function( elem ) { + return !Expr.pseudos["empty"]( elem ); + }, + + // Element/input types + "header": function( elem ) { + return rheader.test( elem.nodeName ); + }, + + "input": function( elem ) { + return rinputs.test( elem.nodeName ); + }, + + "button": function( elem ) { + var name = elem.nodeName.toLowerCase(); + return name === "input" && elem.type === "button" || name === "button"; + }, + + "text": function( elem ) { + var attr; + // IE6 and 7 will map elem.type to 'text' for new HTML5 types (search, etc) + // use getAttribute instead to test this case + return elem.nodeName.toLowerCase() === "input" && + elem.type === "text" && + ( (attr = elem.getAttribute("type")) == null || attr.toLowerCase() === elem.type ); + }, + + // Position-in-collection + "first": createPositionalPseudo(function() { + return [ 0 ]; + }), + + "last": createPositionalPseudo(function( matchIndexes, length ) { + return [ length - 1 ]; + }), + + "eq": createPositionalPseudo(function( matchIndexes, length, argument ) { + return [ argument < 0 ? argument + length : argument ]; + }), + + "even": createPositionalPseudo(function( matchIndexes, length ) { + var i = 0; + for ( ; i < length; i += 2 ) { + matchIndexes.push( i ); + } + return matchIndexes; + }), + + "odd": createPositionalPseudo(function( matchIndexes, length ) { + var i = 1; + for ( ; i < length; i += 2 ) { + matchIndexes.push( i ); + } + return matchIndexes; + }), + + "lt": createPositionalPseudo(function( matchIndexes, length, argument ) { + var i = argument < 0 ? argument + length : argument; + for ( ; --i >= 0; ) { + matchIndexes.push( i ); + } + return matchIndexes; + }), + + "gt": createPositionalPseudo(function( matchIndexes, length, argument ) { + var i = argument < 0 ? argument + length : argument; + for ( ; ++i < length; ) { + matchIndexes.push( i ); + } + return matchIndexes; + }) + } +}; + +// Add button/input type pseudos +for ( i in { radio: true, checkbox: true, file: true, password: true, image: true } ) { + Expr.pseudos[ i ] = createInputPseudo( i ); +} +for ( i in { submit: true, reset: true } ) { + Expr.pseudos[ i ] = createButtonPseudo( i ); +} + +function tokenize( selector, parseOnly ) { + var matched, match, tokens, type, + soFar, groups, preFilters, + cached = tokenCache[ selector + " " ]; + + if ( cached ) { + return parseOnly ? 0 : cached.slice( 0 ); + } + + soFar = selector; + groups = []; + preFilters = Expr.preFilter; + + while ( soFar ) { + + // Comma and first run + if ( !matched || (match = rcomma.exec( soFar )) ) { + if ( match ) { + // Don't consume trailing commas as valid + soFar = soFar.slice( match[0].length ) || soFar; + } + groups.push( tokens = [] ); + } + + matched = false; + + // Combinators + if ( (match = rcombinators.exec( soFar )) ) { + matched = match.shift(); + tokens.push( { + value: matched, + // Cast descendant combinators to space + type: match[0].replace( rtrim, " " ) + } ); + soFar = soFar.slice( matched.length ); + } + + // Filters + for ( type in Expr.filter ) { + if ( (match = matchExpr[ type ].exec( soFar )) && (!preFilters[ type ] || + (match = preFilters[ type ]( match ))) ) { + matched = match.shift(); + tokens.push( { + value: matched, + type: type, + matches: match + } ); + soFar = soFar.slice( matched.length ); + } + } + + if ( !matched ) { + break; + } + } + + // Return the length of the invalid excess + // if we're just parsing + // Otherwise, throw an error or return tokens + return parseOnly ? + soFar.length : + soFar ? + Sizzle.error( selector ) : + // Cache the tokens + tokenCache( selector, groups ).slice( 0 ); +} + +function toSelector( tokens ) { + var i = 0, + len = tokens.length, + selector = ""; + for ( ; i < len; i++ ) { + selector += tokens[i].value; + } + return selector; +} + +function addCombinator( matcher, combinator, base ) { + var dir = combinator.dir, + checkNonElements = base && dir === "parentNode", + doneName = done++; + + return combinator.first ? + // Check against closest ancestor/preceding element + function( elem, context, xml ) { + while ( (elem = elem[ dir ]) ) { + if ( elem.nodeType === 1 || checkNonElements ) { + return matcher( elem, context, xml ); + } + } + } : + + // Check against all ancestor/preceding elements + function( elem, context, xml ) { + var data, cache, outerCache, + dirkey = dirruns + " " + doneName; + + // We can't set arbitrary data on XML nodes, so they don't benefit from dir caching + if ( xml ) { + while ( (elem = elem[ dir ]) ) { + if ( elem.nodeType === 1 || checkNonElements ) { + if ( matcher( elem, context, xml ) ) { + return true; + } + } + } + } else { + while ( (elem = elem[ dir ]) ) { + if ( elem.nodeType === 1 || checkNonElements ) { + outerCache = elem[ expando ] || (elem[ expando ] = {}); + if ( (cache = outerCache[ dir ]) && cache[0] === dirkey ) { + if ( (data = cache[1]) === true || data === cachedruns ) { + return data === true; + } + } else { + cache = outerCache[ dir ] = [ dirkey ]; + cache[1] = matcher( elem, context, xml ) || cachedruns; + if ( cache[1] === true ) { + return true; + } + } + } + } + } + }; +} + +function elementMatcher( matchers ) { + return matchers.length > 1 ? + function( elem, context, xml ) { + var i = matchers.length; + while ( i-- ) { + if ( !matchers[i]( elem, context, xml ) ) { + return false; + } + } + return true; + } : + matchers[0]; +} + +function condense( unmatched, map, filter, context, xml ) { + var elem, + newUnmatched = [], + i = 0, + len = unmatched.length, + mapped = map != null; + + for ( ; i < len; i++ ) { + if ( (elem = unmatched[i]) ) { + if ( !filter || filter( elem, context, xml ) ) { + newUnmatched.push( elem ); + if ( mapped ) { + map.push( i ); + } + } + } + } + + return newUnmatched; +} + +function setMatcher( preFilter, selector, matcher, postFilter, postFinder, postSelector ) { + if ( postFilter && !postFilter[ expando ] ) { + postFilter = setMatcher( postFilter ); + } + if ( postFinder && !postFinder[ expando ] ) { + postFinder = setMatcher( postFinder, postSelector ); + } + return markFunction(function( seed, results, context, xml ) { + var temp, i, elem, + preMap = [], + postMap = [], + preexisting = results.length, + + // Get initial elements from seed or context + elems = seed || multipleContexts( selector || "*", context.nodeType ? [ context ] : context, [] ), + + // Prefilter to get matcher input, preserving a map for seed-results synchronization + matcherIn = preFilter && ( seed || !selector ) ? + condense( elems, preMap, preFilter, context, xml ) : + elems, + + matcherOut = matcher ? + // If we have a postFinder, or filtered seed, or non-seed postFilter or preexisting results, + postFinder || ( seed ? preFilter : preexisting || postFilter ) ? + + // ...intermediate processing is necessary + [] : + + // ...otherwise use results directly + results : + matcherIn; + + // Find primary matches + if ( matcher ) { + matcher( matcherIn, matcherOut, context, xml ); + } + + // Apply postFilter + if ( postFilter ) { + temp = condense( matcherOut, postMap ); + postFilter( temp, [], context, xml ); + + // Un-match failing elements by moving them back to matcherIn + i = temp.length; + while ( i-- ) { + if ( (elem = temp[i]) ) { + matcherOut[ postMap[i] ] = !(matcherIn[ postMap[i] ] = elem); + } + } + } + + if ( seed ) { + if ( postFinder || preFilter ) { + if ( postFinder ) { + // Get the final matcherOut by condensing this intermediate into postFinder contexts + temp = []; + i = matcherOut.length; + while ( i-- ) { + if ( (elem = matcherOut[i]) ) { + // Restore matcherIn since elem is not yet a final match + temp.push( (matcherIn[i] = elem) ); + } + } + postFinder( null, (matcherOut = []), temp, xml ); + } + + // Move matched elements from seed to results to keep them synchronized + i = matcherOut.length; + while ( i-- ) { + if ( (elem = matcherOut[i]) && + (temp = postFinder ? indexOf.call( seed, elem ) : preMap[i]) > -1 ) { + + seed[temp] = !(results[temp] = elem); + } + } + } + + // Add elements to results, through postFinder if defined + } else { + matcherOut = condense( + matcherOut === results ? + matcherOut.splice( preexisting, matcherOut.length ) : + matcherOut + ); + if ( postFinder ) { + postFinder( null, results, matcherOut, xml ); + } else { + push.apply( results, matcherOut ); + } + } + }); +} + +function matcherFromTokens( tokens ) { + var checkContext, matcher, j, + len = tokens.length, + leadingRelative = Expr.relative[ tokens[0].type ], + implicitRelative = leadingRelative || Expr.relative[" "], + i = leadingRelative ? 1 : 0, + + // The foundational matcher ensures that elements are reachable from top-level context(s) + matchContext = addCombinator( function( elem ) { + return elem === checkContext; + }, implicitRelative, true ), + matchAnyContext = addCombinator( function( elem ) { + return indexOf.call( checkContext, elem ) > -1; + }, implicitRelative, true ), + matchers = [ function( elem, context, xml ) { + return ( !leadingRelative && ( xml || context !== outermostContext ) ) || ( + (checkContext = context).nodeType ? + matchContext( elem, context, xml ) : + matchAnyContext( elem, context, xml ) ); + } ]; + + for ( ; i < len; i++ ) { + if ( (matcher = Expr.relative[ tokens[i].type ]) ) { + matchers = [ addCombinator(elementMatcher( matchers ), matcher) ]; + } else { + matcher = Expr.filter[ tokens[i].type ].apply( null, tokens[i].matches ); + + // Return special upon seeing a positional matcher + if ( matcher[ expando ] ) { + // Find the next relative operator (if any) for proper handling + j = ++i; + for ( ; j < len; j++ ) { + if ( Expr.relative[ tokens[j].type ] ) { + break; + } + } + return setMatcher( + i > 1 && elementMatcher( matchers ), + i > 1 && toSelector( tokens.slice( 0, i - 1 ) ).replace( rtrim, "$1" ), + matcher, + i < j && matcherFromTokens( tokens.slice( i, j ) ), + j < len && matcherFromTokens( (tokens = tokens.slice( j )) ), + j < len && toSelector( tokens ) + ); + } + matchers.push( matcher ); + } + } + + return elementMatcher( matchers ); +} + +function matcherFromGroupMatchers( elementMatchers, setMatchers ) { + // A counter to specify which element is currently being matched + var matcherCachedRuns = 0, + bySet = setMatchers.length > 0, + byElement = elementMatchers.length > 0, + superMatcher = function( seed, context, xml, results, expandContext ) { + var elem, j, matcher, + setMatched = [], + matchedCount = 0, + i = "0", + unmatched = seed && [], + outermost = expandContext != null, + contextBackup = outermostContext, + // We must always have either seed elements or context + elems = seed || byElement && Expr.find["TAG"]( "*", expandContext && context.parentNode || context ), + // Use integer dirruns iff this is the outermost matcher + dirrunsUnique = (dirruns += contextBackup == null ? 1 : Math.random() || 0.1); + + if ( outermost ) { + outermostContext = context !== document && context; + cachedruns = matcherCachedRuns; + } + + // Add elements passing elementMatchers directly to results + // Keep `i` a string if there are no elements so `matchedCount` will be "00" below + for ( ; (elem = elems[i]) != null; i++ ) { + if ( byElement && elem ) { + j = 0; + while ( (matcher = elementMatchers[j++]) ) { + if ( matcher( elem, context, xml ) ) { + results.push( elem ); + break; + } + } + if ( outermost ) { + dirruns = dirrunsUnique; + cachedruns = ++matcherCachedRuns; + } + } + + // Track unmatched elements for set filters + if ( bySet ) { + // They will have gone through all possible matchers + if ( (elem = !matcher && elem) ) { + matchedCount--; + } + + // Lengthen the array for every element, matched or not + if ( seed ) { + unmatched.push( elem ); + } + } + } + + // Apply set filters to unmatched elements + matchedCount += i; + if ( bySet && i !== matchedCount ) { + j = 0; + while ( (matcher = setMatchers[j++]) ) { + matcher( unmatched, setMatched, context, xml ); + } + + if ( seed ) { + // Reintegrate element matches to eliminate the need for sorting + if ( matchedCount > 0 ) { + while ( i-- ) { + if ( !(unmatched[i] || setMatched[i]) ) { + setMatched[i] = pop.call( results ); + } + } + } + + // Discard index placeholder values to get only actual matches + setMatched = condense( setMatched ); + } + + // Add matches to results + push.apply( results, setMatched ); + + // Seedless set matches succeeding multiple successful matchers stipulate sorting + if ( outermost && !seed && setMatched.length > 0 && + ( matchedCount + setMatchers.length ) > 1 ) { + + Sizzle.uniqueSort( results ); + } + } + + // Override manipulation of globals by nested matchers + if ( outermost ) { + dirruns = dirrunsUnique; + outermostContext = contextBackup; + } + + return unmatched; + }; + + return bySet ? + markFunction( superMatcher ) : + superMatcher; +} + +compile = Sizzle.compile = function( selector, group /* Internal Use Only */ ) { + var i, + setMatchers = [], + elementMatchers = [], + cached = compilerCache[ selector + " " ]; + + if ( !cached ) { + // Generate a function of recursive functions that can be used to check each element + if ( !group ) { + group = tokenize( selector ); + } + i = group.length; + while ( i-- ) { + cached = matcherFromTokens( group[i] ); + if ( cached[ expando ] ) { + setMatchers.push( cached ); + } else { + elementMatchers.push( cached ); + } + } + + // Cache the compiled function + cached = compilerCache( selector, matcherFromGroupMatchers( elementMatchers, setMatchers ) ); + } + return cached; +}; + +function multipleContexts( selector, contexts, results ) { + var i = 0, + len = contexts.length; + for ( ; i < len; i++ ) { + Sizzle( selector, contexts[i], results ); + } + return results; +} + +function select( selector, context, results, seed ) { + var i, tokens, token, type, find, + match = tokenize( selector ); + + if ( !seed ) { + // Try to minimize operations if there is only one group + if ( match.length === 1 ) { + + // Take a shortcut and set the context if the root selector is an ID + tokens = match[0] = match[0].slice( 0 ); + if ( tokens.length > 2 && (token = tokens[0]).type === "ID" && + context.nodeType === 9 && !documentIsXML && + Expr.relative[ tokens[1].type ] ) { + + context = Expr.find["ID"]( token.matches[0].replace( runescape, funescape ), context )[0]; + if ( !context ) { + return results; + } + + selector = selector.slice( tokens.shift().value.length ); + } + + // Fetch a seed set for right-to-left matching + i = matchExpr["needsContext"].test( selector ) ? 0 : tokens.length; + while ( i-- ) { + token = tokens[i]; + + // Abort if we hit a combinator + if ( Expr.relative[ (type = token.type) ] ) { + break; + } + if ( (find = Expr.find[ type ]) ) { + // Search, expanding context for leading sibling combinators + if ( (seed = find( + token.matches[0].replace( runescape, funescape ), + rsibling.test( tokens[0].type ) && context.parentNode || context + )) ) { + + // If seed is empty or no tokens remain, we can return early + tokens.splice( i, 1 ); + selector = seed.length && toSelector( tokens ); + if ( !selector ) { + push.apply( results, slice.call( seed, 0 ) ); + return results; + } + + break; + } + } + } + } + } + + // Compile and execute a filtering function + // Provide `match` to avoid retokenization if we modified the selector above + compile( selector, match )( + seed, + context, + documentIsXML, + results, + rsibling.test( selector ) + ); + return results; +} + +// Deprecated +Expr.pseudos["nth"] = Expr.pseudos["eq"]; + +// Easy API for creating new setFilters +function setFilters() {} +Expr.filters = setFilters.prototype = Expr.pseudos; +Expr.setFilters = new setFilters(); + +// Initialize with the default document +setDocument(); + +// Override sizzle attribute retrieval +Sizzle.attr = jQuery.attr; +jQuery.find = Sizzle; +jQuery.expr = Sizzle.selectors; +jQuery.expr[":"] = jQuery.expr.pseudos; +jQuery.unique = Sizzle.uniqueSort; +jQuery.text = Sizzle.getText; +jQuery.isXMLDoc = Sizzle.isXML; +jQuery.contains = Sizzle.contains; + + +})( window ); +var runtil = /Until$/, + rparentsprev = /^(?:parents|prev(?:Until|All))/, + isSimple = /^.[^:#\[\.,]*$/, + rneedsContext = jQuery.expr.match.needsContext, + // methods guaranteed to produce a unique set when starting from a unique set + guaranteedUnique = { + children: true, + contents: true, + next: true, + prev: true + }; + +jQuery.fn.extend({ + find: function( selector ) { + var i, ret, self, + len = this.length; + + if ( typeof selector !== "string" ) { + self = this; + return this.pushStack( jQuery( selector ).filter(function() { + for ( i = 0; i < len; i++ ) { + if ( jQuery.contains( self[ i ], this ) ) { + return true; + } + } + }) ); + } + + ret = []; + for ( i = 0; i < len; i++ ) { + jQuery.find( selector, this[ i ], ret ); + } + + // Needed because $( selector, context ) becomes $( context ).find( selector ) + ret = this.pushStack( len > 1 ? jQuery.unique( ret ) : ret ); + ret.selector = ( this.selector ? this.selector + " " : "" ) + selector; + return ret; + }, + + has: function( target ) { + var i, + targets = jQuery( target, this ), + len = targets.length; + + return this.filter(function() { + for ( i = 0; i < len; i++ ) { + if ( jQuery.contains( this, targets[i] ) ) { + return true; + } + } + }); + }, + + not: function( selector ) { + return this.pushStack( winnow(this, selector, false) ); + }, + + filter: function( selector ) { + return this.pushStack( winnow(this, selector, true) ); + }, + + is: function( selector ) { + return !!selector && ( + typeof selector === "string" ? + // If this is a positional/relative selector, check membership in the returned set + // so $("p:first").is("p:last") won't return true for a doc with two "p". + rneedsContext.test( selector ) ? + jQuery( selector, this.context ).index( this[0] ) >= 0 : + jQuery.filter( selector, this ).length > 0 : + this.filter( selector ).length > 0 ); + }, + + closest: function( selectors, context ) { + var cur, + i = 0, + l = this.length, + ret = [], + pos = rneedsContext.test( selectors ) || typeof selectors !== "string" ? + jQuery( selectors, context || this.context ) : + 0; + + for ( ; i < l; i++ ) { + cur = this[i]; + + while ( cur && cur.ownerDocument && cur !== context && cur.nodeType !== 11 ) { + if ( pos ? pos.index(cur) > -1 : jQuery.find.matchesSelector(cur, selectors) ) { + ret.push( cur ); + break; + } + cur = cur.parentNode; + } + } + + return this.pushStack( ret.length > 1 ? jQuery.unique( ret ) : ret ); + }, + + // Determine the position of an element within + // the matched set of elements + index: function( elem ) { + + // No argument, return index in parent + if ( !elem ) { + return ( this[0] && this[0].parentNode ) ? this.first().prevAll().length : -1; + } + + // index in selector + if ( typeof elem === "string" ) { + return jQuery.inArray( this[0], jQuery( elem ) ); + } + + // Locate the position of the desired element + return jQuery.inArray( + // If it receives a jQuery object, the first element is used + elem.jquery ? elem[0] : elem, this ); + }, + + add: function( selector, context ) { + var set = typeof selector === "string" ? + jQuery( selector, context ) : + jQuery.makeArray( selector && selector.nodeType ? [ selector ] : selector ), + all = jQuery.merge( this.get(), set ); + + return this.pushStack( jQuery.unique(all) ); + }, + + addBack: function( selector ) { + return this.add( selector == null ? + this.prevObject : this.prevObject.filter(selector) + ); + } +}); + +jQuery.fn.andSelf = jQuery.fn.addBack; + +function sibling( cur, dir ) { + do { + cur = cur[ dir ]; + } while ( cur && cur.nodeType !== 1 ); + + return cur; +} + +jQuery.each({ + parent: function( elem ) { + var parent = elem.parentNode; + return parent && parent.nodeType !== 11 ? parent : null; + }, + parents: function( elem ) { + return jQuery.dir( elem, "parentNode" ); + }, + parentsUntil: function( elem, i, until ) { + return jQuery.dir( elem, "parentNode", until ); + }, + next: function( elem ) { + return sibling( elem, "nextSibling" ); + }, + prev: function( elem ) { + return sibling( elem, "previousSibling" ); + }, + nextAll: function( elem ) { + return jQuery.dir( elem, "nextSibling" ); + }, + prevAll: function( elem ) { + return jQuery.dir( elem, "previousSibling" ); + }, + nextUntil: function( elem, i, until ) { + return jQuery.dir( elem, "nextSibling", until ); + }, + prevUntil: function( elem, i, until ) { + return jQuery.dir( elem, "previousSibling", until ); + }, + siblings: function( elem ) { + return jQuery.sibling( ( elem.parentNode || {} ).firstChild, elem ); + }, + children: function( elem ) { + return jQuery.sibling( elem.firstChild ); + }, + contents: function( elem ) { + return jQuery.nodeName( elem, "iframe" ) ? + elem.contentDocument || elem.contentWindow.document : + jQuery.merge( [], elem.childNodes ); + } +}, function( name, fn ) { + jQuery.fn[ name ] = function( until, selector ) { + var ret = jQuery.map( this, fn, until ); + + if ( !runtil.test( name ) ) { + selector = until; + } + + if ( selector && typeof selector === "string" ) { + ret = jQuery.filter( selector, ret ); + } + + ret = this.length > 1 && !guaranteedUnique[ name ] ? jQuery.unique( ret ) : ret; + + if ( this.length > 1 && rparentsprev.test( name ) ) { + ret = ret.reverse(); + } + + return this.pushStack( ret ); + }; +}); + +jQuery.extend({ + filter: function( expr, elems, not ) { + if ( not ) { + expr = ":not(" + expr + ")"; + } + + return elems.length === 1 ? + jQuery.find.matchesSelector(elems[0], expr) ? [ elems[0] ] : [] : + jQuery.find.matches(expr, elems); + }, + + dir: function( elem, dir, until ) { + var matched = [], + cur = elem[ dir ]; + + while ( cur && cur.nodeType !== 9 && (until === undefined || cur.nodeType !== 1 || !jQuery( cur ).is( until )) ) { + if ( cur.nodeType === 1 ) { + matched.push( cur ); + } + cur = cur[dir]; + } + return matched; + }, + + sibling: function( n, elem ) { + var r = []; + + for ( ; n; n = n.nextSibling ) { + if ( n.nodeType === 1 && n !== elem ) { + r.push( n ); + } + } + + return r; + } +}); + +// Implement the identical functionality for filter and not +function winnow( elements, qualifier, keep ) { + + // Can't pass null or undefined to indexOf in Firefox 4 + // Set to 0 to skip string check + qualifier = qualifier || 0; + + if ( jQuery.isFunction( qualifier ) ) { + return jQuery.grep(elements, function( elem, i ) { + var retVal = !!qualifier.call( elem, i, elem ); + return retVal === keep; + }); + + } else if ( qualifier.nodeType ) { + return jQuery.grep(elements, function( elem ) { + return ( elem === qualifier ) === keep; + }); + + } else if ( typeof qualifier === "string" ) { + var filtered = jQuery.grep(elements, function( elem ) { + return elem.nodeType === 1; + }); + + if ( isSimple.test( qualifier ) ) { + return jQuery.filter(qualifier, filtered, !keep); + } else { + qualifier = jQuery.filter( qualifier, filtered ); + } + } + + return jQuery.grep(elements, function( elem ) { + return ( jQuery.inArray( elem, qualifier ) >= 0 ) === keep; + }); +} +function createSafeFragment( document ) { + var list = nodeNames.split( "|" ), + safeFrag = document.createDocumentFragment(); + + if ( safeFrag.createElement ) { + while ( list.length ) { + safeFrag.createElement( + list.pop() + ); + } + } + return safeFrag; +} + +var nodeNames = "abbr|article|aside|audio|bdi|canvas|data|datalist|details|figcaption|figure|footer|" + + "header|hgroup|mark|meter|nav|output|progress|section|summary|time|video", + rinlinejQuery = / jQuery\d+="(?:null|\d+)"/g, + rnoshimcache = new RegExp("<(?:" + nodeNames + ")[\\s/>]", "i"), + rleadingWhitespace = /^\s+/, + rxhtmlTag = /<(?!area|br|col|embed|hr|img|input|link|meta|param)(([\w:]+)[^>]*)\/>/gi, + rtagName = /<([\w:]+)/, + rtbody = /\s*$/g, + + // We have to close these tags to support XHTML (#13200) + wrapMap = { + option: [ 1, "" ], + legend: [ 1, "
", "
" ], + area: [ 1, "", "" ], + param: [ 1, "", "" ], + thead: [ 1, "", "
" ], + tr: [ 2, "", "
" ], + col: [ 2, "", "
" ], + td: [ 3, "", "
" ], + + // IE6-8 can't serialize link, script, style, or any html5 (NoScope) tags, + // unless wrapped in a div with non-breaking characters in front of it. + _default: jQuery.support.htmlSerialize ? [ 0, "", "" ] : [ 1, "X
", "
" ] + }, + safeFragment = createSafeFragment( document ), + fragmentDiv = safeFragment.appendChild( document.createElement("div") ); + +wrapMap.optgroup = wrapMap.option; +wrapMap.tbody = wrapMap.tfoot = wrapMap.colgroup = wrapMap.caption = wrapMap.thead; +wrapMap.th = wrapMap.td; + +jQuery.fn.extend({ + text: function( value ) { + return jQuery.access( this, function( value ) { + return value === undefined ? + jQuery.text( this ) : + this.empty().append( ( this[0] && this[0].ownerDocument || document ).createTextNode( value ) ); + }, null, value, arguments.length ); + }, + + wrapAll: function( html ) { + if ( jQuery.isFunction( html ) ) { + return this.each(function(i) { + jQuery(this).wrapAll( html.call(this, i) ); + }); + } + + if ( this[0] ) { + // The elements to wrap the target around + var wrap = jQuery( html, this[0].ownerDocument ).eq(0).clone(true); + + if ( this[0].parentNode ) { + wrap.insertBefore( this[0] ); + } + + wrap.map(function() { + var elem = this; + + while ( elem.firstChild && elem.firstChild.nodeType === 1 ) { + elem = elem.firstChild; + } + + return elem; + }).append( this ); + } + + return this; + }, + + wrapInner: function( html ) { + if ( jQuery.isFunction( html ) ) { + return this.each(function(i) { + jQuery(this).wrapInner( html.call(this, i) ); + }); + } + + return this.each(function() { + var self = jQuery( this ), + contents = self.contents(); + + if ( contents.length ) { + contents.wrapAll( html ); + + } else { + self.append( html ); + } + }); + }, + + wrap: function( html ) { + var isFunction = jQuery.isFunction( html ); + + return this.each(function(i) { + jQuery( this ).wrapAll( isFunction ? html.call(this, i) : html ); + }); + }, + + unwrap: function() { + return this.parent().each(function() { + if ( !jQuery.nodeName( this, "body" ) ) { + jQuery( this ).replaceWith( this.childNodes ); + } + }).end(); + }, + + append: function() { + return this.domManip(arguments, true, function( elem ) { + if ( this.nodeType === 1 || this.nodeType === 11 || this.nodeType === 9 ) { + this.appendChild( elem ); + } + }); + }, + + prepend: function() { + return this.domManip(arguments, true, function( elem ) { + if ( this.nodeType === 1 || this.nodeType === 11 || this.nodeType === 9 ) { + this.insertBefore( elem, this.firstChild ); + } + }); + }, + + before: function() { + return this.domManip( arguments, false, function( elem ) { + if ( this.parentNode ) { + this.parentNode.insertBefore( elem, this ); + } + }); + }, + + after: function() { + return this.domManip( arguments, false, function( elem ) { + if ( this.parentNode ) { + this.parentNode.insertBefore( elem, this.nextSibling ); + } + }); + }, + + // keepData is for internal use only--do not document + remove: function( selector, keepData ) { + var elem, + i = 0; + + for ( ; (elem = this[i]) != null; i++ ) { + if ( !selector || jQuery.filter( selector, [ elem ] ).length > 0 ) { + if ( !keepData && elem.nodeType === 1 ) { + jQuery.cleanData( getAll( elem ) ); + } + + if ( elem.parentNode ) { + if ( keepData && jQuery.contains( elem.ownerDocument, elem ) ) { + setGlobalEval( getAll( elem, "script" ) ); + } + elem.parentNode.removeChild( elem ); + } + } + } + + return this; + }, + + empty: function() { + var elem, + i = 0; + + for ( ; (elem = this[i]) != null; i++ ) { + // Remove element nodes and prevent memory leaks + if ( elem.nodeType === 1 ) { + jQuery.cleanData( getAll( elem, false ) ); + } + + // Remove any remaining nodes + while ( elem.firstChild ) { + elem.removeChild( elem.firstChild ); + } + + // If this is a select, ensure that it displays empty (#12336) + // Support: IE<9 + if ( elem.options && jQuery.nodeName( elem, "select" ) ) { + elem.options.length = 0; + } + } + + return this; + }, + + clone: function( dataAndEvents, deepDataAndEvents ) { + dataAndEvents = dataAndEvents == null ? false : dataAndEvents; + deepDataAndEvents = deepDataAndEvents == null ? dataAndEvents : deepDataAndEvents; + + return this.map( function () { + return jQuery.clone( this, dataAndEvents, deepDataAndEvents ); + }); + }, + + html: function( value ) { + return jQuery.access( this, function( value ) { + var elem = this[0] || {}, + i = 0, + l = this.length; + + if ( value === undefined ) { + return elem.nodeType === 1 ? + elem.innerHTML.replace( rinlinejQuery, "" ) : + undefined; + } + + // See if we can take a shortcut and just use innerHTML + if ( typeof value === "string" && !rnoInnerhtml.test( value ) && + ( jQuery.support.htmlSerialize || !rnoshimcache.test( value ) ) && + ( jQuery.support.leadingWhitespace || !rleadingWhitespace.test( value ) ) && + !wrapMap[ ( rtagName.exec( value ) || ["", ""] )[1].toLowerCase() ] ) { + + value = value.replace( rxhtmlTag, "<$1>" ); + + try { + for (; i < l; i++ ) { + // Remove element nodes and prevent memory leaks + elem = this[i] || {}; + if ( elem.nodeType === 1 ) { + jQuery.cleanData( getAll( elem, false ) ); + elem.innerHTML = value; + } + } + + elem = 0; + + // If using innerHTML throws an exception, use the fallback method + } catch(e) {} + } + + if ( elem ) { + this.empty().append( value ); + } + }, null, value, arguments.length ); + }, + + replaceWith: function( value ) { + var isFunc = jQuery.isFunction( value ); + + // Make sure that the elements are removed from the DOM before they are inserted + // this can help fix replacing a parent with child elements + if ( !isFunc && typeof value !== "string" ) { + value = jQuery( value ).not( this ).detach(); + } + + return this.domManip( [ value ], true, function( elem ) { + var next = this.nextSibling, + parent = this.parentNode; + + if ( parent ) { + jQuery( this ).remove(); + parent.insertBefore( elem, next ); + } + }); + }, + + detach: function( selector ) { + return this.remove( selector, true ); + }, + + domManip: function( args, table, callback ) { + + // Flatten any nested arrays + args = core_concat.apply( [], args ); + + var first, node, hasScripts, + scripts, doc, fragment, + i = 0, + l = this.length, + set = this, + iNoClone = l - 1, + value = args[0], + isFunction = jQuery.isFunction( value ); + + // We can't cloneNode fragments that contain checked, in WebKit + if ( isFunction || !( l <= 1 || typeof value !== "string" || jQuery.support.checkClone || !rchecked.test( value ) ) ) { + return this.each(function( index ) { + var self = set.eq( index ); + if ( isFunction ) { + args[0] = value.call( this, index, table ? self.html() : undefined ); + } + self.domManip( args, table, callback ); + }); + } + + if ( l ) { + fragment = jQuery.buildFragment( args, this[ 0 ].ownerDocument, false, this ); + first = fragment.firstChild; + + if ( fragment.childNodes.length === 1 ) { + fragment = first; + } + + if ( first ) { + table = table && jQuery.nodeName( first, "tr" ); + scripts = jQuery.map( getAll( fragment, "script" ), disableScript ); + hasScripts = scripts.length; + + // Use the original fragment for the last item instead of the first because it can end up + // being emptied incorrectly in certain situations (#8070). + for ( ; i < l; i++ ) { + node = fragment; + + if ( i !== iNoClone ) { + node = jQuery.clone( node, true, true ); + + // Keep references to cloned scripts for later restoration + if ( hasScripts ) { + jQuery.merge( scripts, getAll( node, "script" ) ); + } + } + + callback.call( + table && jQuery.nodeName( this[i], "table" ) ? + findOrAppend( this[i], "tbody" ) : + this[i], + node, + i + ); + } + + if ( hasScripts ) { + doc = scripts[ scripts.length - 1 ].ownerDocument; + + // Reenable scripts + jQuery.map( scripts, restoreScript ); + + // Evaluate executable scripts on first document insertion + for ( i = 0; i < hasScripts; i++ ) { + node = scripts[ i ]; + if ( rscriptType.test( node.type || "" ) && + !jQuery._data( node, "globalEval" ) && jQuery.contains( doc, node ) ) { + + if ( node.src ) { + // Hope ajax is available... + jQuery.ajax({ + url: node.src, + type: "GET", + dataType: "script", + async: false, + global: false, + "throws": true + }); + } else { + jQuery.globalEval( ( node.text || node.textContent || node.innerHTML || "" ).replace( rcleanScript, "" ) ); + } + } + } + } + + // Fix #11809: Avoid leaking memory + fragment = first = null; + } + } + + return this; + } +}); + +function findOrAppend( elem, tag ) { + return elem.getElementsByTagName( tag )[0] || elem.appendChild( elem.ownerDocument.createElement( tag ) ); +} + +// Replace/restore the type attribute of script elements for safe DOM manipulation +function disableScript( elem ) { + var attr = elem.getAttributeNode("type"); + elem.type = ( attr && attr.specified ) + "/" + elem.type; + return elem; +} +function restoreScript( elem ) { + var match = rscriptTypeMasked.exec( elem.type ); + if ( match ) { + elem.type = match[1]; + } else { + elem.removeAttribute("type"); + } + return elem; +} + +// Mark scripts as having already been evaluated +function setGlobalEval( elems, refElements ) { + var elem, + i = 0; + for ( ; (elem = elems[i]) != null; i++ ) { + jQuery._data( elem, "globalEval", !refElements || jQuery._data( refElements[i], "globalEval" ) ); + } +} + +function cloneCopyEvent( src, dest ) { + + if ( dest.nodeType !== 1 || !jQuery.hasData( src ) ) { + return; + } + + var type, i, l, + oldData = jQuery._data( src ), + curData = jQuery._data( dest, oldData ), + events = oldData.events; + + if ( events ) { + delete curData.handle; + curData.events = {}; + + for ( type in events ) { + for ( i = 0, l = events[ type ].length; i < l; i++ ) { + jQuery.event.add( dest, type, events[ type ][ i ] ); + } + } + } + + // make the cloned public data object a copy from the original + if ( curData.data ) { + curData.data = jQuery.extend( {}, curData.data ); + } +} + +function fixCloneNodeIssues( src, dest ) { + var nodeName, e, data; + + // We do not need to do anything for non-Elements + if ( dest.nodeType !== 1 ) { + return; + } + + nodeName = dest.nodeName.toLowerCase(); + + // IE6-8 copies events bound via attachEvent when using cloneNode. + if ( !jQuery.support.noCloneEvent && dest[ jQuery.expando ] ) { + data = jQuery._data( dest ); + + for ( e in data.events ) { + jQuery.removeEvent( dest, e, data.handle ); + } + + // Event data gets referenced instead of copied if the expando gets copied too + dest.removeAttribute( jQuery.expando ); + } + + // IE blanks contents when cloning scripts, and tries to evaluate newly-set text + if ( nodeName === "script" && dest.text !== src.text ) { + disableScript( dest ).text = src.text; + restoreScript( dest ); + + // IE6-10 improperly clones children of object elements using classid. + // IE10 throws NoModificationAllowedError if parent is null, #12132. + } else if ( nodeName === "object" ) { + if ( dest.parentNode ) { + dest.outerHTML = src.outerHTML; + } + + // This path appears unavoidable for IE9. When cloning an object + // element in IE9, the outerHTML strategy above is not sufficient. + // If the src has innerHTML and the destination does not, + // copy the src.innerHTML into the dest.innerHTML. #10324 + if ( jQuery.support.html5Clone && ( src.innerHTML && !jQuery.trim(dest.innerHTML) ) ) { + dest.innerHTML = src.innerHTML; + } + + } else if ( nodeName === "input" && manipulation_rcheckableType.test( src.type ) ) { + // IE6-8 fails to persist the checked state of a cloned checkbox + // or radio button. Worse, IE6-7 fail to give the cloned element + // a checked appearance if the defaultChecked value isn't also set + + dest.defaultChecked = dest.checked = src.checked; + + // IE6-7 get confused and end up setting the value of a cloned + // checkbox/radio button to an empty string instead of "on" + if ( dest.value !== src.value ) { + dest.value = src.value; + } + + // IE6-8 fails to return the selected option to the default selected + // state when cloning options + } else if ( nodeName === "option" ) { + dest.defaultSelected = dest.selected = src.defaultSelected; + + // IE6-8 fails to set the defaultValue to the correct value when + // cloning other types of input fields + } else if ( nodeName === "input" || nodeName === "textarea" ) { + dest.defaultValue = src.defaultValue; + } +} + +jQuery.each({ + appendTo: "append", + prependTo: "prepend", + insertBefore: "before", + insertAfter: "after", + replaceAll: "replaceWith" +}, function( name, original ) { + jQuery.fn[ name ] = function( selector ) { + var elems, + i = 0, + ret = [], + insert = jQuery( selector ), + last = insert.length - 1; + + for ( ; i <= last; i++ ) { + elems = i === last ? this : this.clone(true); + jQuery( insert[i] )[ original ]( elems ); + + // Modern browsers can apply jQuery collections as arrays, but oldIE needs a .get() + core_push.apply( ret, elems.get() ); + } + + return this.pushStack( ret ); + }; +}); + +function getAll( context, tag ) { + var elems, elem, + i = 0, + found = typeof context.getElementsByTagName !== core_strundefined ? context.getElementsByTagName( tag || "*" ) : + typeof context.querySelectorAll !== core_strundefined ? context.querySelectorAll( tag || "*" ) : + undefined; + + if ( !found ) { + for ( found = [], elems = context.childNodes || context; (elem = elems[i]) != null; i++ ) { + if ( !tag || jQuery.nodeName( elem, tag ) ) { + found.push( elem ); + } else { + jQuery.merge( found, getAll( elem, tag ) ); + } + } + } + + return tag === undefined || tag && jQuery.nodeName( context, tag ) ? + jQuery.merge( [ context ], found ) : + found; +} + +// Used in buildFragment, fixes the defaultChecked property +function fixDefaultChecked( elem ) { + if ( manipulation_rcheckableType.test( elem.type ) ) { + elem.defaultChecked = elem.checked; + } +} + +jQuery.extend({ + clone: function( elem, dataAndEvents, deepDataAndEvents ) { + var destElements, node, clone, i, srcElements, + inPage = jQuery.contains( elem.ownerDocument, elem ); + + if ( jQuery.support.html5Clone || jQuery.isXMLDoc(elem) || !rnoshimcache.test( "<" + elem.nodeName + ">" ) ) { + clone = elem.cloneNode( true ); + + // IE<=8 does not properly clone detached, unknown element nodes + } else { + fragmentDiv.innerHTML = elem.outerHTML; + fragmentDiv.removeChild( clone = fragmentDiv.firstChild ); + } + + if ( (!jQuery.support.noCloneEvent || !jQuery.support.noCloneChecked) && + (elem.nodeType === 1 || elem.nodeType === 11) && !jQuery.isXMLDoc(elem) ) { + + // We eschew Sizzle here for performance reasons: http://jsperf.com/getall-vs-sizzle/2 + destElements = getAll( clone ); + srcElements = getAll( elem ); + + // Fix all IE cloning issues + for ( i = 0; (node = srcElements[i]) != null; ++i ) { + // Ensure that the destination node is not null; Fixes #9587 + if ( destElements[i] ) { + fixCloneNodeIssues( node, destElements[i] ); + } + } + } + + // Copy the events from the original to the clone + if ( dataAndEvents ) { + if ( deepDataAndEvents ) { + srcElements = srcElements || getAll( elem ); + destElements = destElements || getAll( clone ); + + for ( i = 0; (node = srcElements[i]) != null; i++ ) { + cloneCopyEvent( node, destElements[i] ); + } + } else { + cloneCopyEvent( elem, clone ); + } + } + + // Preserve script evaluation history + destElements = getAll( clone, "script" ); + if ( destElements.length > 0 ) { + setGlobalEval( destElements, !inPage && getAll( elem, "script" ) ); + } + + destElements = srcElements = node = null; + + // Return the cloned set + return clone; + }, + + buildFragment: function( elems, context, scripts, selection ) { + var j, elem, contains, + tmp, tag, tbody, wrap, + l = elems.length, + + // Ensure a safe fragment + safe = createSafeFragment( context ), + + nodes = [], + i = 0; + + for ( ; i < l; i++ ) { + elem = elems[ i ]; + + if ( elem || elem === 0 ) { + + // Add nodes directly + if ( jQuery.type( elem ) === "object" ) { + jQuery.merge( nodes, elem.nodeType ? [ elem ] : elem ); + + // Convert non-html into a text node + } else if ( !rhtml.test( elem ) ) { + nodes.push( context.createTextNode( elem ) ); + + // Convert html into DOM nodes + } else { + tmp = tmp || safe.appendChild( context.createElement("div") ); + + // Deserialize a standard representation + tag = ( rtagName.exec( elem ) || ["", ""] )[1].toLowerCase(); + wrap = wrapMap[ tag ] || wrapMap._default; + + tmp.innerHTML = wrap[1] + elem.replace( rxhtmlTag, "<$1>" ) + wrap[2]; + + // Descend through wrappers to the right content + j = wrap[0]; + while ( j-- ) { + tmp = tmp.lastChild; + } + + // Manually add leading whitespace removed by IE + if ( !jQuery.support.leadingWhitespace && rleadingWhitespace.test( elem ) ) { + nodes.push( context.createTextNode( rleadingWhitespace.exec( elem )[0] ) ); + } + + // Remove IE's autoinserted from table fragments + if ( !jQuery.support.tbody ) { + + // String was a , *may* have spurious + elem = tag === "table" && !rtbody.test( elem ) ? + tmp.firstChild : + + // String was a bare or + wrap[1] === "
" && !rtbody.test( elem ) ? + tmp : + 0; + + j = elem && elem.childNodes.length; + while ( j-- ) { + if ( jQuery.nodeName( (tbody = elem.childNodes[j]), "tbody" ) && !tbody.childNodes.length ) { + elem.removeChild( tbody ); + } + } + } + + jQuery.merge( nodes, tmp.childNodes ); + + // Fix #12392 for WebKit and IE > 9 + tmp.textContent = ""; + + // Fix #12392 for oldIE + while ( tmp.firstChild ) { + tmp.removeChild( tmp.firstChild ); + } + + // Remember the top-level container for proper cleanup + tmp = safe.lastChild; + } + } + } + + // Fix #11356: Clear elements from fragment + if ( tmp ) { + safe.removeChild( tmp ); + } + + // Reset defaultChecked for any radios and checkboxes + // about to be appended to the DOM in IE 6/7 (#8060) + if ( !jQuery.support.appendChecked ) { + jQuery.grep( getAll( nodes, "input" ), fixDefaultChecked ); + } + + i = 0; + while ( (elem = nodes[ i++ ]) ) { + + // #4087 - If origin and destination elements are the same, and this is + // that element, do not do anything + if ( selection && jQuery.inArray( elem, selection ) !== -1 ) { + continue; + } + + contains = jQuery.contains( elem.ownerDocument, elem ); + + // Append to fragment + tmp = getAll( safe.appendChild( elem ), "script" ); + + // Preserve script evaluation history + if ( contains ) { + setGlobalEval( tmp ); + } + + // Capture executables + if ( scripts ) { + j = 0; + while ( (elem = tmp[ j++ ]) ) { + if ( rscriptType.test( elem.type || "" ) ) { + scripts.push( elem ); + } + } + } + } + + tmp = null; + + return safe; + }, + + cleanData: function( elems, /* internal */ acceptData ) { + var elem, type, id, data, + i = 0, + internalKey = jQuery.expando, + cache = jQuery.cache, + deleteExpando = jQuery.support.deleteExpando, + special = jQuery.event.special; + + for ( ; (elem = elems[i]) != null; i++ ) { + + if ( acceptData || jQuery.acceptData( elem ) ) { + + id = elem[ internalKey ]; + data = id && cache[ id ]; + + if ( data ) { + if ( data.events ) { + for ( type in data.events ) { + if ( special[ type ] ) { + jQuery.event.remove( elem, type ); + + // This is a shortcut to avoid jQuery.event.remove's overhead + } else { + jQuery.removeEvent( elem, type, data.handle ); + } + } + } + + // Remove cache only if it was not already removed by jQuery.event.remove + if ( cache[ id ] ) { + + delete cache[ id ]; + + // IE does not allow us to delete expando properties from nodes, + // nor does it have a removeAttribute function on Document nodes; + // we must handle all of these cases + if ( deleteExpando ) { + delete elem[ internalKey ]; + + } else if ( typeof elem.removeAttribute !== core_strundefined ) { + elem.removeAttribute( internalKey ); + + } else { + elem[ internalKey ] = null; + } + + core_deletedIds.push( id ); + } + } + } + } + } +}); +var iframe, getStyles, curCSS, + ralpha = /alpha\([^)]*\)/i, + ropacity = /opacity\s*=\s*([^)]*)/, + rposition = /^(top|right|bottom|left)$/, + // swappable if display is none or starts with table except "table", "table-cell", or "table-caption" + // see here for display values: https://developer.mozilla.org/en-US/docs/CSS/display + rdisplayswap = /^(none|table(?!-c[ea]).+)/, + rmargin = /^margin/, + rnumsplit = new RegExp( "^(" + core_pnum + ")(.*)$", "i" ), + rnumnonpx = new RegExp( "^(" + core_pnum + ")(?!px)[a-z%]+$", "i" ), + rrelNum = new RegExp( "^([+-])=(" + core_pnum + ")", "i" ), + elemdisplay = { BODY: "block" }, + + cssShow = { position: "absolute", visibility: "hidden", display: "block" }, + cssNormalTransform = { + letterSpacing: 0, + fontWeight: 400 + }, + + cssExpand = [ "Top", "Right", "Bottom", "Left" ], + cssPrefixes = [ "Webkit", "O", "Moz", "ms" ]; + +// return a css property mapped to a potentially vendor prefixed property +function vendorPropName( style, name ) { + + // shortcut for names that are not vendor prefixed + if ( name in style ) { + return name; + } + + // check for vendor prefixed names + var capName = name.charAt(0).toUpperCase() + name.slice(1), + origName = name, + i = cssPrefixes.length; + + while ( i-- ) { + name = cssPrefixes[ i ] + capName; + if ( name in style ) { + return name; + } + } + + return origName; +} + +function isHidden( elem, el ) { + // isHidden might be called from jQuery#filter function; + // in that case, element will be second argument + elem = el || elem; + return jQuery.css( elem, "display" ) === "none" || !jQuery.contains( elem.ownerDocument, elem ); +} + +function showHide( elements, show ) { + var display, elem, hidden, + values = [], + index = 0, + length = elements.length; + + for ( ; index < length; index++ ) { + elem = elements[ index ]; + if ( !elem.style ) { + continue; + } + + values[ index ] = jQuery._data( elem, "olddisplay" ); + display = elem.style.display; + if ( show ) { + // Reset the inline display of this element to learn if it is + // being hidden by cascaded rules or not + if ( !values[ index ] && display === "none" ) { + elem.style.display = ""; + } + + // Set elements which have been overridden with display: none + // in a stylesheet to whatever the default browser style is + // for such an element + if ( elem.style.display === "" && isHidden( elem ) ) { + values[ index ] = jQuery._data( elem, "olddisplay", css_defaultDisplay(elem.nodeName) ); + } + } else { + + if ( !values[ index ] ) { + hidden = isHidden( elem ); + + if ( display && display !== "none" || !hidden ) { + jQuery._data( elem, "olddisplay", hidden ? display : jQuery.css( elem, "display" ) ); + } + } + } + } + + // Set the display of most of the elements in a second loop + // to avoid the constant reflow + for ( index = 0; index < length; index++ ) { + elem = elements[ index ]; + if ( !elem.style ) { + continue; + } + if ( !show || elem.style.display === "none" || elem.style.display === "" ) { + elem.style.display = show ? values[ index ] || "" : "none"; + } + } + + return elements; +} + +jQuery.fn.extend({ + css: function( name, value ) { + return jQuery.access( this, function( elem, name, value ) { + var len, styles, + map = {}, + i = 0; + + if ( jQuery.isArray( name ) ) { + styles = getStyles( elem ); + len = name.length; + + for ( ; i < len; i++ ) { + map[ name[ i ] ] = jQuery.css( elem, name[ i ], false, styles ); + } + + return map; + } + + return value !== undefined ? + jQuery.style( elem, name, value ) : + jQuery.css( elem, name ); + }, name, value, arguments.length > 1 ); + }, + show: function() { + return showHide( this, true ); + }, + hide: function() { + return showHide( this ); + }, + toggle: function( state ) { + var bool = typeof state === "boolean"; + + return this.each(function() { + if ( bool ? state : isHidden( this ) ) { + jQuery( this ).show(); + } else { + jQuery( this ).hide(); + } + }); + } +}); + +jQuery.extend({ + // Add in style property hooks for overriding the default + // behavior of getting and setting a style property + cssHooks: { + opacity: { + get: function( elem, computed ) { + if ( computed ) { + // We should always get a number back from opacity + var ret = curCSS( elem, "opacity" ); + return ret === "" ? "1" : ret; + } + } + } + }, + + // Exclude the following css properties to add px + cssNumber: { + "columnCount": true, + "fillOpacity": true, + "fontWeight": true, + "lineHeight": true, + "opacity": true, + "orphans": true, + "widows": true, + "zIndex": true, + "zoom": true + }, + + // Add in properties whose names you wish to fix before + // setting or getting the value + cssProps: { + // normalize float css property + "float": jQuery.support.cssFloat ? "cssFloat" : "styleFloat" + }, + + // Get and set the style property on a DOM Node + style: function( elem, name, value, extra ) { + // Don't set styles on text and comment nodes + if ( !elem || elem.nodeType === 3 || elem.nodeType === 8 || !elem.style ) { + return; + } + + // Make sure that we're working with the right name + var ret, type, hooks, + origName = jQuery.camelCase( name ), + style = elem.style; + + name = jQuery.cssProps[ origName ] || ( jQuery.cssProps[ origName ] = vendorPropName( style, origName ) ); + + // gets hook for the prefixed version + // followed by the unprefixed version + hooks = jQuery.cssHooks[ name ] || jQuery.cssHooks[ origName ]; + + // Check if we're setting a value + if ( value !== undefined ) { + type = typeof value; + + // convert relative number strings (+= or -=) to relative numbers. #7345 + if ( type === "string" && (ret = rrelNum.exec( value )) ) { + value = ( ret[1] + 1 ) * ret[2] + parseFloat( jQuery.css( elem, name ) ); + // Fixes bug #9237 + type = "number"; + } + + // Make sure that NaN and null values aren't set. See: #7116 + if ( value == null || type === "number" && isNaN( value ) ) { + return; + } + + // If a number was passed in, add 'px' to the (except for certain CSS properties) + if ( type === "number" && !jQuery.cssNumber[ origName ] ) { + value += "px"; + } + + // Fixes #8908, it can be done more correctly by specifing setters in cssHooks, + // but it would mean to define eight (for every problematic property) identical functions + if ( !jQuery.support.clearCloneStyle && value === "" && name.indexOf("background") === 0 ) { + style[ name ] = "inherit"; + } + + // If a hook was provided, use that value, otherwise just set the specified value + if ( !hooks || !("set" in hooks) || (value = hooks.set( elem, value, extra )) !== undefined ) { + + // Wrapped to prevent IE from throwing errors when 'invalid' values are provided + // Fixes bug #5509 + try { + style[ name ] = value; + } catch(e) {} + } + + } else { + // If a hook was provided get the non-computed value from there + if ( hooks && "get" in hooks && (ret = hooks.get( elem, false, extra )) !== undefined ) { + return ret; + } + + // Otherwise just get the value from the style object + return style[ name ]; + } + }, + + css: function( elem, name, extra, styles ) { + var num, val, hooks, + origName = jQuery.camelCase( name ); + + // Make sure that we're working with the right name + name = jQuery.cssProps[ origName ] || ( jQuery.cssProps[ origName ] = vendorPropName( elem.style, origName ) ); + + // gets hook for the prefixed version + // followed by the unprefixed version + hooks = jQuery.cssHooks[ name ] || jQuery.cssHooks[ origName ]; + + // If a hook was provided get the computed value from there + if ( hooks && "get" in hooks ) { + val = hooks.get( elem, true, extra ); + } + + // Otherwise, if a way to get the computed value exists, use that + if ( val === undefined ) { + val = curCSS( elem, name, styles ); + } + + //convert "normal" to computed value + if ( val === "normal" && name in cssNormalTransform ) { + val = cssNormalTransform[ name ]; + } + + // Return, converting to number if forced or a qualifier was provided and val looks numeric + if ( extra === "" || extra ) { + num = parseFloat( val ); + return extra === true || jQuery.isNumeric( num ) ? num || 0 : val; + } + return val; + }, + + // A method for quickly swapping in/out CSS properties to get correct calculations + swap: function( elem, options, callback, args ) { + var ret, name, + old = {}; + + // Remember the old values, and insert the new ones + for ( name in options ) { + old[ name ] = elem.style[ name ]; + elem.style[ name ] = options[ name ]; + } + + ret = callback.apply( elem, args || [] ); + + // Revert the old values + for ( name in options ) { + elem.style[ name ] = old[ name ]; + } + + return ret; + } +}); + +// NOTE: we've included the "window" in window.getComputedStyle +// because jsdom on node.js will break without it. +if ( window.getComputedStyle ) { + getStyles = function( elem ) { + return window.getComputedStyle( elem, null ); + }; + + curCSS = function( elem, name, _computed ) { + var width, minWidth, maxWidth, + computed = _computed || getStyles( elem ), + + // getPropertyValue is only needed for .css('filter') in IE9, see #12537 + ret = computed ? computed.getPropertyValue( name ) || computed[ name ] : undefined, + style = elem.style; + + if ( computed ) { + + if ( ret === "" && !jQuery.contains( elem.ownerDocument, elem ) ) { + ret = jQuery.style( elem, name ); + } + + // A tribute to the "awesome hack by Dean Edwards" + // Chrome < 17 and Safari 5.0 uses "computed value" instead of "used value" for margin-right + // Safari 5.1.7 (at least) returns percentage for a larger set of values, but width seems to be reliably pixels + // this is against the CSSOM draft spec: http://dev.w3.org/csswg/cssom/#resolved-values + if ( rnumnonpx.test( ret ) && rmargin.test( name ) ) { + + // Remember the original values + width = style.width; + minWidth = style.minWidth; + maxWidth = style.maxWidth; + + // Put in the new values to get a computed value out + style.minWidth = style.maxWidth = style.width = ret; + ret = computed.width; + + // Revert the changed values + style.width = width; + style.minWidth = minWidth; + style.maxWidth = maxWidth; + } + } + + return ret; + }; +} else if ( document.documentElement.currentStyle ) { + getStyles = function( elem ) { + return elem.currentStyle; + }; + + curCSS = function( elem, name, _computed ) { + var left, rs, rsLeft, + computed = _computed || getStyles( elem ), + ret = computed ? computed[ name ] : undefined, + style = elem.style; + + // Avoid setting ret to empty string here + // so we don't default to auto + if ( ret == null && style && style[ name ] ) { + ret = style[ name ]; + } + + // From the awesome hack by Dean Edwards + // http://erik.eae.net/archives/2007/07/27/18.54.15/#comment-102291 + + // If we're not dealing with a regular pixel number + // but a number that has a weird ending, we need to convert it to pixels + // but not position css attributes, as those are proportional to the parent element instead + // and we can't measure the parent instead because it might trigger a "stacking dolls" problem + if ( rnumnonpx.test( ret ) && !rposition.test( name ) ) { + + // Remember the original values + left = style.left; + rs = elem.runtimeStyle; + rsLeft = rs && rs.left; + + // Put in the new values to get a computed value out + if ( rsLeft ) { + rs.left = elem.currentStyle.left; + } + style.left = name === "fontSize" ? "1em" : ret; + ret = style.pixelLeft + "px"; + + // Revert the changed values + style.left = left; + if ( rsLeft ) { + rs.left = rsLeft; + } + } + + return ret === "" ? "auto" : ret; + }; +} + +function setPositiveNumber( elem, value, subtract ) { + var matches = rnumsplit.exec( value ); + return matches ? + // Guard against undefined "subtract", e.g., when used as in cssHooks + Math.max( 0, matches[ 1 ] - ( subtract || 0 ) ) + ( matches[ 2 ] || "px" ) : + value; +} + +function augmentWidthOrHeight( elem, name, extra, isBorderBox, styles ) { + var i = extra === ( isBorderBox ? "border" : "content" ) ? + // If we already have the right measurement, avoid augmentation + 4 : + // Otherwise initialize for horizontal or vertical properties + name === "width" ? 1 : 0, + + val = 0; + + for ( ; i < 4; i += 2 ) { + // both box models exclude margin, so add it if we want it + if ( extra === "margin" ) { + val += jQuery.css( elem, extra + cssExpand[ i ], true, styles ); + } + + if ( isBorderBox ) { + // border-box includes padding, so remove it if we want content + if ( extra === "content" ) { + val -= jQuery.css( elem, "padding" + cssExpand[ i ], true, styles ); + } + + // at this point, extra isn't border nor margin, so remove border + if ( extra !== "margin" ) { + val -= jQuery.css( elem, "border" + cssExpand[ i ] + "Width", true, styles ); + } + } else { + // at this point, extra isn't content, so add padding + val += jQuery.css( elem, "padding" + cssExpand[ i ], true, styles ); + + // at this point, extra isn't content nor padding, so add border + if ( extra !== "padding" ) { + val += jQuery.css( elem, "border" + cssExpand[ i ] + "Width", true, styles ); + } + } + } + + return val; +} + +function getWidthOrHeight( elem, name, extra ) { + + // Start with offset property, which is equivalent to the border-box value + var valueIsBorderBox = true, + val = name === "width" ? elem.offsetWidth : elem.offsetHeight, + styles = getStyles( elem ), + isBorderBox = jQuery.support.boxSizing && jQuery.css( elem, "boxSizing", false, styles ) === "border-box"; + + // some non-html elements return undefined for offsetWidth, so check for null/undefined + // svg - https://bugzilla.mozilla.org/show_bug.cgi?id=649285 + // MathML - https://bugzilla.mozilla.org/show_bug.cgi?id=491668 + if ( val <= 0 || val == null ) { + // Fall back to computed then uncomputed css if necessary + val = curCSS( elem, name, styles ); + if ( val < 0 || val == null ) { + val = elem.style[ name ]; + } + + // Computed unit is not pixels. Stop here and return. + if ( rnumnonpx.test(val) ) { + return val; + } + + // we need the check for style in case a browser which returns unreliable values + // for getComputedStyle silently falls back to the reliable elem.style + valueIsBorderBox = isBorderBox && ( jQuery.support.boxSizingReliable || val === elem.style[ name ] ); + + // Normalize "", auto, and prepare for extra + val = parseFloat( val ) || 0; + } + + // use the active box-sizing model to add/subtract irrelevant styles + return ( val + + augmentWidthOrHeight( + elem, + name, + extra || ( isBorderBox ? "border" : "content" ), + valueIsBorderBox, + styles + ) + ) + "px"; +} + +// Try to determine the default display value of an element +function css_defaultDisplay( nodeName ) { + var doc = document, + display = elemdisplay[ nodeName ]; + + if ( !display ) { + display = actualDisplay( nodeName, doc ); + + // If the simple way fails, read from inside an iframe + if ( display === "none" || !display ) { + // Use the already-created iframe if possible + iframe = ( iframe || + jQuery("' : ''); + inst._keyEvent = false; + return html; + }, + + /* Generate the month and year header. */ + _generateMonthYearHeader: function(inst, drawMonth, drawYear, minDate, maxDate, + secondary, monthNames, monthNamesShort) { + var changeMonth = this._get(inst, 'changeMonth'); + var changeYear = this._get(inst, 'changeYear'); + var showMonthAfterYear = this._get(inst, 'showMonthAfterYear'); + var html = '
'; + var monthHtml = ''; + // month selection + if (secondary || !changeMonth) + monthHtml += '' + monthNames[drawMonth] + ''; + else { + var inMinYear = (minDate && minDate.getFullYear() == drawYear); + var inMaxYear = (maxDate && maxDate.getFullYear() == drawYear); + monthHtml += ''; + } + if (!showMonthAfterYear) + html += monthHtml + (secondary || !(changeMonth && changeYear) ? ' ' : ''); + // year selection + if ( !inst.yearshtml ) { + inst.yearshtml = ''; + if (secondary || !changeYear) + html += '' + drawYear + ''; + else { + // determine range of years to display + var years = this._get(inst, 'yearRange').split(':'); + var thisYear = new Date().getFullYear(); + var determineYear = function(value) { + var year = (value.match(/c[+-].*/) ? drawYear + parseInt(value.substring(1), 10) : + (value.match(/[+-].*/) ? thisYear + parseInt(value, 10) : + parseInt(value, 10))); + return (isNaN(year) ? thisYear : year); + }; + var year = determineYear(years[0]); + var endYear = Math.max(year, determineYear(years[1] || '')); + year = (minDate ? Math.max(year, minDate.getFullYear()) : year); + endYear = (maxDate ? Math.min(endYear, maxDate.getFullYear()) : endYear); + inst.yearshtml += ''; + + html += inst.yearshtml; + inst.yearshtml = null; + } + } + html += this._get(inst, 'yearSuffix'); + if (showMonthAfterYear) + html += (secondary || !(changeMonth && changeYear) ? ' ' : '') + monthHtml; + html += '
'; // Close datepicker_header + return html; + }, + + /* Adjust one of the date sub-fields. */ + _adjustInstDate: function(inst, offset, period) { + var year = inst.drawYear + (period == 'Y' ? offset : 0); + var month = inst.drawMonth + (period == 'M' ? offset : 0); + var day = Math.min(inst.selectedDay, this._getDaysInMonth(year, month)) + + (period == 'D' ? offset : 0); + var date = this._restrictMinMax(inst, + this._daylightSavingAdjust(new Date(year, month, day))); + inst.selectedDay = date.getDate(); + inst.drawMonth = inst.selectedMonth = date.getMonth(); + inst.drawYear = inst.selectedYear = date.getFullYear(); + if (period == 'M' || period == 'Y') + this._notifyChange(inst); + }, + + /* Ensure a date is within any min/max bounds. */ + _restrictMinMax: function(inst, date) { + var minDate = this._getMinMaxDate(inst, 'min'); + var maxDate = this._getMinMaxDate(inst, 'max'); + var newDate = (minDate && date < minDate ? minDate : date); + newDate = (maxDate && newDate > maxDate ? maxDate : newDate); + return newDate; + }, + + /* Notify change of month/year. */ + _notifyChange: function(inst) { + var onChange = this._get(inst, 'onChangeMonthYear'); + if (onChange) + onChange.apply((inst.input ? inst.input[0] : null), + [inst.selectedYear, inst.selectedMonth + 1, inst]); + }, + + /* Determine the number of months to show. */ + _getNumberOfMonths: function(inst) { + var numMonths = this._get(inst, 'numberOfMonths'); + return (numMonths == null ? [1, 1] : (typeof numMonths == 'number' ? [1, numMonths] : numMonths)); + }, + + /* Determine the current maximum date - ensure no time components are set. */ + _getMinMaxDate: function(inst, minMax) { + return this._determineDate(inst, this._get(inst, minMax + 'Date'), null); + }, + + /* Find the number of days in a given month. */ + _getDaysInMonth: function(year, month) { + return 32 - this._daylightSavingAdjust(new Date(year, month, 32)).getDate(); + }, + + /* Find the day of the week of the first of a month. */ + _getFirstDayOfMonth: function(year, month) { + return new Date(year, month, 1).getDay(); + }, + + /* Determines if we should allow a "next/prev" month display change. */ + _canAdjustMonth: function(inst, offset, curYear, curMonth) { + var numMonths = this._getNumberOfMonths(inst); + var date = this._daylightSavingAdjust(new Date(curYear, + curMonth + (offset < 0 ? offset : numMonths[0] * numMonths[1]), 1)); + if (offset < 0) + date.setDate(this._getDaysInMonth(date.getFullYear(), date.getMonth())); + return this._isInRange(inst, date); + }, + + /* Is the given date in the accepted range? */ + _isInRange: function(inst, date) { + var minDate = this._getMinMaxDate(inst, 'min'); + var maxDate = this._getMinMaxDate(inst, 'max'); + return ((!minDate || date.getTime() >= minDate.getTime()) && + (!maxDate || date.getTime() <= maxDate.getTime())); + }, + + /* Provide the configuration settings for formatting/parsing. */ + _getFormatConfig: function(inst) { + var shortYearCutoff = this._get(inst, 'shortYearCutoff'); + shortYearCutoff = (typeof shortYearCutoff != 'string' ? shortYearCutoff : + new Date().getFullYear() % 100 + parseInt(shortYearCutoff, 10)); + return {shortYearCutoff: shortYearCutoff, + dayNamesShort: this._get(inst, 'dayNamesShort'), dayNames: this._get(inst, 'dayNames'), + monthNamesShort: this._get(inst, 'monthNamesShort'), monthNames: this._get(inst, 'monthNames')}; + }, + + /* Format the given date for display. */ + _formatDate: function(inst, day, month, year) { + if (!day) { + inst.currentDay = inst.selectedDay; + inst.currentMonth = inst.selectedMonth; + inst.currentYear = inst.selectedYear; + } + var date = (day ? (typeof day == 'object' ? day : + this._daylightSavingAdjust(new Date(year, month, day))) : + this._daylightSavingAdjust(new Date(inst.currentYear, inst.currentMonth, inst.currentDay))); + return this.formatDate(this._get(inst, 'dateFormat'), date, this._getFormatConfig(inst)); + } +}); + +/* + * Bind hover events for datepicker elements. + * Done via delegate so the binding only occurs once in the lifetime of the parent div. + * Global instActive, set by _updateDatepicker allows the handlers to find their way back to the active picker. + */ +function bindHover(dpDiv) { + var selector = 'button, .ui-datepicker-prev, .ui-datepicker-next, .ui-datepicker-calendar td a'; + return dpDiv.delegate(selector, 'mouseout', function() { + $(this).removeClass('ui-state-hover'); + if (this.className.indexOf('ui-datepicker-prev') != -1) $(this).removeClass('ui-datepicker-prev-hover'); + if (this.className.indexOf('ui-datepicker-next') != -1) $(this).removeClass('ui-datepicker-next-hover'); + }) + .delegate(selector, 'mouseover', function(){ + if (!$.datepicker._isDisabledDatepicker( instActive.inline ? dpDiv.parent()[0] : instActive.input[0])) { + $(this).parents('.ui-datepicker-calendar').find('a').removeClass('ui-state-hover'); + $(this).addClass('ui-state-hover'); + if (this.className.indexOf('ui-datepicker-prev') != -1) $(this).addClass('ui-datepicker-prev-hover'); + if (this.className.indexOf('ui-datepicker-next') != -1) $(this).addClass('ui-datepicker-next-hover'); + } + }); +} + +/* jQuery extend now ignores nulls! */ +function extendRemove(target, props) { + $.extend(target, props); + for (var name in props) + if (props[name] == null || props[name] == undefined) + target[name] = props[name]; + return target; +}; + +/* Invoke the datepicker functionality. + @param options string - a command, optionally followed by additional parameters or + Object - settings for attaching new datepicker functionality + @return jQuery object */ +$.fn.datepicker = function(options){ + + /* Verify an empty collection wasn't passed - Fixes #6976 */ + if ( !this.length ) { + return this; + } + + /* Initialise the date picker. */ + if (!$.datepicker.initialized) { + $(document).mousedown($.datepicker._checkExternalClick). + find(document.body).append($.datepicker.dpDiv); + $.datepicker.initialized = true; + } + + var otherArgs = Array.prototype.slice.call(arguments, 1); + if (typeof options == 'string' && (options == 'isDisabled' || options == 'getDate' || options == 'widget')) + return $.datepicker['_' + options + 'Datepicker']. + apply($.datepicker, [this[0]].concat(otherArgs)); + if (options == 'option' && arguments.length == 2 && typeof arguments[1] == 'string') + return $.datepicker['_' + options + 'Datepicker']. + apply($.datepicker, [this[0]].concat(otherArgs)); + return this.each(function() { + typeof options == 'string' ? + $.datepicker['_' + options + 'Datepicker']. + apply($.datepicker, [this].concat(otherArgs)) : + $.datepicker._attachDatepicker(this, options); + }); +}; + +$.datepicker = new Datepicker(); // singleton instance +$.datepicker.initialized = false; +$.datepicker.uuid = new Date().getTime(); +$.datepicker.version = "1.9.2"; + +// Workaround for #4055 +// Add another global to avoid noConflict issues with inline event handlers +window['DP_jQuery_' + dpuuid] = $; + +})(jQuery); + +(function( $, undefined ) { + +var uiDialogClasses = "ui-dialog ui-widget ui-widget-content ui-corner-all ", + sizeRelatedOptions = { + buttons: true, + height: true, + maxHeight: true, + maxWidth: true, + minHeight: true, + minWidth: true, + width: true + }, + resizableRelatedOptions = { + maxHeight: true, + maxWidth: true, + minHeight: true, + minWidth: true + }; + +$.widget("ui.dialog", { + version: "1.9.2", + options: { + autoOpen: true, + buttons: {}, + closeOnEscape: true, + closeText: "close", + dialogClass: "", + draggable: true, + hide: null, + height: "auto", + maxHeight: false, + maxWidth: false, + minHeight: 150, + minWidth: 150, + modal: false, + position: { + my: "center", + at: "center", + of: window, + collision: "fit", + // ensure that the titlebar is never outside the document + using: function( pos ) { + var topOffset = $( this ).css( pos ).offset().top; + if ( topOffset < 0 ) { + $( this ).css( "top", pos.top - topOffset ); + } + } + }, + resizable: true, + show: null, + stack: true, + title: "", + width: 300, + zIndex: 1000 + }, + + _create: function() { + this.originalTitle = this.element.attr( "title" ); + // #5742 - .attr() might return a DOMElement + if ( typeof this.originalTitle !== "string" ) { + this.originalTitle = ""; + } + this.oldPosition = { + parent: this.element.parent(), + index: this.element.parent().children().index( this.element ) + }; + this.options.title = this.options.title || this.originalTitle; + var that = this, + options = this.options, + + title = options.title || " ", + uiDialog, + uiDialogTitlebar, + uiDialogTitlebarClose, + uiDialogTitle, + uiDialogButtonPane; + + uiDialog = ( this.uiDialog = $( "
" ) ) + .addClass( uiDialogClasses + options.dialogClass ) + .css({ + display: "none", + outline: 0, // TODO: move to stylesheet + zIndex: options.zIndex + }) + // setting tabIndex makes the div focusable + .attr( "tabIndex", -1) + .keydown(function( event ) { + if ( options.closeOnEscape && !event.isDefaultPrevented() && event.keyCode && + event.keyCode === $.ui.keyCode.ESCAPE ) { + that.close( event ); + event.preventDefault(); + } + }) + .mousedown(function( event ) { + that.moveToTop( false, event ); + }) + .appendTo( "body" ); + + this.element + .show() + .removeAttr( "title" ) + .addClass( "ui-dialog-content ui-widget-content" ) + .appendTo( uiDialog ); + + uiDialogTitlebar = ( this.uiDialogTitlebar = $( "
" ) ) + .addClass( "ui-dialog-titlebar ui-widget-header " + + "ui-corner-all ui-helper-clearfix" ) + .bind( "mousedown", function() { + // Dialog isn't getting focus when dragging (#8063) + uiDialog.focus(); + }) + .prependTo( uiDialog ); + + uiDialogTitlebarClose = $( "" ) + .addClass( "ui-dialog-titlebar-close ui-corner-all" ) + .attr( "role", "button" ) + .click(function( event ) { + event.preventDefault(); + that.close( event ); + }) + .appendTo( uiDialogTitlebar ); + + ( this.uiDialogTitlebarCloseText = $( "" ) ) + .addClass( "ui-icon ui-icon-closethick" ) + .text( options.closeText ) + .appendTo( uiDialogTitlebarClose ); + + uiDialogTitle = $( "" ) + .uniqueId() + .addClass( "ui-dialog-title" ) + .html( title ) + .prependTo( uiDialogTitlebar ); + + uiDialogButtonPane = ( this.uiDialogButtonPane = $( "
" ) ) + .addClass( "ui-dialog-buttonpane ui-widget-content ui-helper-clearfix" ); + + ( this.uiButtonSet = $( "
" ) ) + .addClass( "ui-dialog-buttonset" ) + .appendTo( uiDialogButtonPane ); + + uiDialog.attr({ + role: "dialog", + "aria-labelledby": uiDialogTitle.attr( "id" ) + }); + + uiDialogTitlebar.find( "*" ).add( uiDialogTitlebar ).disableSelection(); + this._hoverable( uiDialogTitlebarClose ); + this._focusable( uiDialogTitlebarClose ); + + if ( options.draggable && $.fn.draggable ) { + this._makeDraggable(); + } + if ( options.resizable && $.fn.resizable ) { + this._makeResizable(); + } + + this._createButtons( options.buttons ); + this._isOpen = false; + + if ( $.fn.bgiframe ) { + uiDialog.bgiframe(); + } + + // prevent tabbing out of modal dialogs + this._on( uiDialog, { keydown: function( event ) { + if ( !options.modal || event.keyCode !== $.ui.keyCode.TAB ) { + return; + } + + var tabbables = $( ":tabbable", uiDialog ), + first = tabbables.filter( ":first" ), + last = tabbables.filter( ":last" ); + + if ( event.target === last[0] && !event.shiftKey ) { + first.focus( 1 ); + return false; + } else if ( event.target === first[0] && event.shiftKey ) { + last.focus( 1 ); + return false; + } + }}); + }, + + _init: function() { + if ( this.options.autoOpen ) { + this.open(); + } + }, + + _destroy: function() { + var next, + oldPosition = this.oldPosition; + + if ( this.overlay ) { + this.overlay.destroy(); + } + this.uiDialog.hide(); + this.element + .removeClass( "ui-dialog-content ui-widget-content" ) + .hide() + .appendTo( "body" ); + this.uiDialog.remove(); + + if ( this.originalTitle ) { + this.element.attr( "title", this.originalTitle ); + } + + next = oldPosition.parent.children().eq( oldPosition.index ); + // Don't try to place the dialog next to itself (#8613) + if ( next.length && next[ 0 ] !== this.element[ 0 ] ) { + next.before( this.element ); + } else { + oldPosition.parent.append( this.element ); + } + }, + + widget: function() { + return this.uiDialog; + }, + + close: function( event ) { + var that = this, + maxZ, thisZ; + + if ( !this._isOpen ) { + return; + } + + if ( false === this._trigger( "beforeClose", event ) ) { + return; + } + + this._isOpen = false; + + if ( this.overlay ) { + this.overlay.destroy(); + } + + if ( this.options.hide ) { + this._hide( this.uiDialog, this.options.hide, function() { + that._trigger( "close", event ); + }); + } else { + this.uiDialog.hide(); + this._trigger( "close", event ); + } + + $.ui.dialog.overlay.resize(); + + // adjust the maxZ to allow other modal dialogs to continue to work (see #4309) + if ( this.options.modal ) { + maxZ = 0; + $( ".ui-dialog" ).each(function() { + if ( this !== that.uiDialog[0] ) { + thisZ = $( this ).css( "z-index" ); + if ( !isNaN( thisZ ) ) { + maxZ = Math.max( maxZ, thisZ ); + } + } + }); + $.ui.dialog.maxZ = maxZ; + } + + return this; + }, + + isOpen: function() { + return this._isOpen; + }, + + // the force parameter allows us to move modal dialogs to their correct + // position on open + moveToTop: function( force, event ) { + var options = this.options, + saveScroll; + + if ( ( options.modal && !force ) || + ( !options.stack && !options.modal ) ) { + return this._trigger( "focus", event ); + } + + if ( options.zIndex > $.ui.dialog.maxZ ) { + $.ui.dialog.maxZ = options.zIndex; + } + if ( this.overlay ) { + $.ui.dialog.maxZ += 1; + $.ui.dialog.overlay.maxZ = $.ui.dialog.maxZ; + this.overlay.$el.css( "z-index", $.ui.dialog.overlay.maxZ ); + } + + // Save and then restore scroll + // Opera 9.5+ resets when parent z-index is changed. + // http://bugs.jqueryui.com/ticket/3193 + saveScroll = { + scrollTop: this.element.scrollTop(), + scrollLeft: this.element.scrollLeft() + }; + $.ui.dialog.maxZ += 1; + this.uiDialog.css( "z-index", $.ui.dialog.maxZ ); + this.element.attr( saveScroll ); + this._trigger( "focus", event ); + + return this; + }, + + open: function() { + if ( this._isOpen ) { + return; + } + + var hasFocus, + options = this.options, + uiDialog = this.uiDialog; + + this._size(); + this._position( options.position ); + uiDialog.show( options.show ); + this.overlay = options.modal ? new $.ui.dialog.overlay( this ) : null; + this.moveToTop( true ); + + // set focus to the first tabbable element in the content area or the first button + // if there are no tabbable elements, set focus on the dialog itself + hasFocus = this.element.find( ":tabbable" ); + if ( !hasFocus.length ) { + hasFocus = this.uiDialogButtonPane.find( ":tabbable" ); + if ( !hasFocus.length ) { + hasFocus = uiDialog; + } + } + hasFocus.eq( 0 ).focus(); + + this._isOpen = true; + this._trigger( "open" ); + + return this; + }, + + _createButtons: function( buttons ) { + var that = this, + hasButtons = false; + + // if we already have a button pane, remove it + this.uiDialogButtonPane.remove(); + this.uiButtonSet.empty(); + + if ( typeof buttons === "object" && buttons !== null ) { + $.each( buttons, function() { + return !(hasButtons = true); + }); + } + if ( hasButtons ) { + $.each( buttons, function( name, props ) { + var button, click; + props = $.isFunction( props ) ? + { click: props, text: name } : + props; + // Default to a non-submitting button + props = $.extend( { type: "button" }, props ); + // Change the context for the click callback to be the main element + click = props.click; + props.click = function() { + click.apply( that.element[0], arguments ); + }; + button = $( "", props ) + .appendTo( that.uiButtonSet ); + if ( $.fn.button ) { + button.button(); + } + }); + this.uiDialog.addClass( "ui-dialog-buttons" ); + this.uiDialogButtonPane.appendTo( this.uiDialog ); + } else { + this.uiDialog.removeClass( "ui-dialog-buttons" ); + } + }, + + _makeDraggable: function() { + var that = this, + options = this.options; + + function filteredUi( ui ) { + return { + position: ui.position, + offset: ui.offset + }; + } + + this.uiDialog.draggable({ + cancel: ".ui-dialog-content, .ui-dialog-titlebar-close", + handle: ".ui-dialog-titlebar", + containment: "document", + start: function( event, ui ) { + $( this ) + .addClass( "ui-dialog-dragging" ); + that._trigger( "dragStart", event, filteredUi( ui ) ); + }, + drag: function( event, ui ) { + that._trigger( "drag", event, filteredUi( ui ) ); + }, + stop: function( event, ui ) { + options.position = [ + ui.position.left - that.document.scrollLeft(), + ui.position.top - that.document.scrollTop() + ]; + $( this ) + .removeClass( "ui-dialog-dragging" ); + that._trigger( "dragStop", event, filteredUi( ui ) ); + $.ui.dialog.overlay.resize(); + } + }); + }, + + _makeResizable: function( handles ) { + handles = (handles === undefined ? this.options.resizable : handles); + var that = this, + options = this.options, + // .ui-resizable has position: relative defined in the stylesheet + // but dialogs have to use absolute or fixed positioning + position = this.uiDialog.css( "position" ), + resizeHandles = typeof handles === 'string' ? + handles : + "n,e,s,w,se,sw,ne,nw"; + + function filteredUi( ui ) { + return { + originalPosition: ui.originalPosition, + originalSize: ui.originalSize, + position: ui.position, + size: ui.size + }; + } + + this.uiDialog.resizable({ + cancel: ".ui-dialog-content", + containment: "document", + alsoResize: this.element, + maxWidth: options.maxWidth, + maxHeight: options.maxHeight, + minWidth: options.minWidth, + minHeight: this._minHeight(), + handles: resizeHandles, + start: function( event, ui ) { + $( this ).addClass( "ui-dialog-resizing" ); + that._trigger( "resizeStart", event, filteredUi( ui ) ); + }, + resize: function( event, ui ) { + that._trigger( "resize", event, filteredUi( ui ) ); + }, + stop: function( event, ui ) { + $( this ).removeClass( "ui-dialog-resizing" ); + options.height = $( this ).height(); + options.width = $( this ).width(); + that._trigger( "resizeStop", event, filteredUi( ui ) ); + $.ui.dialog.overlay.resize(); + } + }) + .css( "position", position ) + .find( ".ui-resizable-se" ) + .addClass( "ui-icon ui-icon-grip-diagonal-se" ); + }, + + _minHeight: function() { + var options = this.options; + + if ( options.height === "auto" ) { + return options.minHeight; + } else { + return Math.min( options.minHeight, options.height ); + } + }, + + _position: function( position ) { + var myAt = [], + offset = [ 0, 0 ], + isVisible; + + if ( position ) { + // deep extending converts arrays to objects in jQuery <= 1.3.2 :-( + // if (typeof position == 'string' || $.isArray(position)) { + // myAt = $.isArray(position) ? position : position.split(' '); + + if ( typeof position === "string" || (typeof position === "object" && "0" in position ) ) { + myAt = position.split ? position.split( " " ) : [ position[ 0 ], position[ 1 ] ]; + if ( myAt.length === 1 ) { + myAt[ 1 ] = myAt[ 0 ]; + } + + $.each( [ "left", "top" ], function( i, offsetPosition ) { + if ( +myAt[ i ] === myAt[ i ] ) { + offset[ i ] = myAt[ i ]; + myAt[ i ] = offsetPosition; + } + }); + + position = { + my: myAt[0] + (offset[0] < 0 ? offset[0] : "+" + offset[0]) + " " + + myAt[1] + (offset[1] < 0 ? offset[1] : "+" + offset[1]), + at: myAt.join( " " ) + }; + } + + position = $.extend( {}, $.ui.dialog.prototype.options.position, position ); + } else { + position = $.ui.dialog.prototype.options.position; + } + + // need to show the dialog to get the actual offset in the position plugin + isVisible = this.uiDialog.is( ":visible" ); + if ( !isVisible ) { + this.uiDialog.show(); + } + this.uiDialog.position( position ); + if ( !isVisible ) { + this.uiDialog.hide(); + } + }, + + _setOptions: function( options ) { + var that = this, + resizableOptions = {}, + resize = false; + + $.each( options, function( key, value ) { + that._setOption( key, value ); + + if ( key in sizeRelatedOptions ) { + resize = true; + } + if ( key in resizableRelatedOptions ) { + resizableOptions[ key ] = value; + } + }); + + if ( resize ) { + this._size(); + } + if ( this.uiDialog.is( ":data(resizable)" ) ) { + this.uiDialog.resizable( "option", resizableOptions ); + } + }, + + _setOption: function( key, value ) { + var isDraggable, isResizable, + uiDialog = this.uiDialog; + + switch ( key ) { + case "buttons": + this._createButtons( value ); + break; + case "closeText": + // ensure that we always pass a string + this.uiDialogTitlebarCloseText.text( "" + value ); + break; + case "dialogClass": + uiDialog + .removeClass( this.options.dialogClass ) + .addClass( uiDialogClasses + value ); + break; + case "disabled": + if ( value ) { + uiDialog.addClass( "ui-dialog-disabled" ); + } else { + uiDialog.removeClass( "ui-dialog-disabled" ); + } + break; + case "draggable": + isDraggable = uiDialog.is( ":data(draggable)" ); + if ( isDraggable && !value ) { + uiDialog.draggable( "destroy" ); + } + + if ( !isDraggable && value ) { + this._makeDraggable(); + } + break; + case "position": + this._position( value ); + break; + case "resizable": + // currently resizable, becoming non-resizable + isResizable = uiDialog.is( ":data(resizable)" ); + if ( isResizable && !value ) { + uiDialog.resizable( "destroy" ); + } + + // currently resizable, changing handles + if ( isResizable && typeof value === "string" ) { + uiDialog.resizable( "option", "handles", value ); + } + + // currently non-resizable, becoming resizable + if ( !isResizable && value !== false ) { + this._makeResizable( value ); + } + break; + case "title": + // convert whatever was passed in o a string, for html() to not throw up + $( ".ui-dialog-title", this.uiDialogTitlebar ) + .html( "" + ( value || " " ) ); + break; + } + + this._super( key, value ); + }, + + _size: function() { + /* If the user has resized the dialog, the .ui-dialog and .ui-dialog-content + * divs will both have width and height set, so we need to reset them + */ + var nonContentHeight, minContentHeight, autoHeight, + options = this.options, + isVisible = this.uiDialog.is( ":visible" ); + + // reset content sizing + this.element.show().css({ + width: "auto", + minHeight: 0, + height: 0 + }); + + if ( options.minWidth > options.width ) { + options.width = options.minWidth; + } + + // reset wrapper sizing + // determine the height of all the non-content elements + nonContentHeight = this.uiDialog.css({ + height: "auto", + width: options.width + }) + .outerHeight(); + minContentHeight = Math.max( 0, options.minHeight - nonContentHeight ); + + if ( options.height === "auto" ) { + // only needed for IE6 support + if ( $.support.minHeight ) { + this.element.css({ + minHeight: minContentHeight, + height: "auto" + }); + } else { + this.uiDialog.show(); + autoHeight = this.element.css( "height", "auto" ).height(); + if ( !isVisible ) { + this.uiDialog.hide(); + } + this.element.height( Math.max( autoHeight, minContentHeight ) ); + } + } else { + this.element.height( Math.max( options.height - nonContentHeight, 0 ) ); + } + + if (this.uiDialog.is( ":data(resizable)" ) ) { + this.uiDialog.resizable( "option", "minHeight", this._minHeight() ); + } + } +}); + +$.extend($.ui.dialog, { + uuid: 0, + maxZ: 0, + + getTitleId: function($el) { + var id = $el.attr( "id" ); + if ( !id ) { + this.uuid += 1; + id = this.uuid; + } + return "ui-dialog-title-" + id; + }, + + overlay: function( dialog ) { + this.$el = $.ui.dialog.overlay.create( dialog ); + } +}); + +$.extend( $.ui.dialog.overlay, { + instances: [], + // reuse old instances due to IE memory leak with alpha transparency (see #5185) + oldInstances: [], + maxZ: 0, + events: $.map( + "focus,mousedown,mouseup,keydown,keypress,click".split( "," ), + function( event ) { + return event + ".dialog-overlay"; + } + ).join( " " ), + create: function( dialog ) { + if ( this.instances.length === 0 ) { + // prevent use of anchors and inputs + // we use a setTimeout in case the overlay is created from an + // event that we're going to be cancelling (see #2804) + setTimeout(function() { + // handle $(el).dialog().dialog('close') (see #4065) + if ( $.ui.dialog.overlay.instances.length ) { + $( document ).bind( $.ui.dialog.overlay.events, function( event ) { + // stop events if the z-index of the target is < the z-index of the overlay + // we cannot return true when we don't want to cancel the event (#3523) + if ( $( event.target ).zIndex() < $.ui.dialog.overlay.maxZ ) { + return false; + } + }); + } + }, 1 ); + + // handle window resize + $( window ).bind( "resize.dialog-overlay", $.ui.dialog.overlay.resize ); + } + + var $el = ( this.oldInstances.pop() || $( "
" ).addClass( "ui-widget-overlay" ) ); + + // allow closing by pressing the escape key + $( document ).bind( "keydown.dialog-overlay", function( event ) { + var instances = $.ui.dialog.overlay.instances; + // only react to the event if we're the top overlay + if ( instances.length !== 0 && instances[ instances.length - 1 ] === $el && + dialog.options.closeOnEscape && !event.isDefaultPrevented() && event.keyCode && + event.keyCode === $.ui.keyCode.ESCAPE ) { + + dialog.close( event ); + event.preventDefault(); + } + }); + + $el.appendTo( document.body ).css({ + width: this.width(), + height: this.height() + }); + + if ( $.fn.bgiframe ) { + $el.bgiframe(); + } + + this.instances.push( $el ); + return $el; + }, + + destroy: function( $el ) { + var indexOf = $.inArray( $el, this.instances ), + maxZ = 0; + + if ( indexOf !== -1 ) { + this.oldInstances.push( this.instances.splice( indexOf, 1 )[ 0 ] ); + } + + if ( this.instances.length === 0 ) { + $( [ document, window ] ).unbind( ".dialog-overlay" ); + } + + $el.height( 0 ).width( 0 ).remove(); + + // adjust the maxZ to allow other modal dialogs to continue to work (see #4309) + $.each( this.instances, function() { + maxZ = Math.max( maxZ, this.css( "z-index" ) ); + }); + this.maxZ = maxZ; + }, + + height: function() { + var scrollHeight, + offsetHeight; + // handle IE + if ( $.ui.ie ) { + scrollHeight = Math.max( + document.documentElement.scrollHeight, + document.body.scrollHeight + ); + offsetHeight = Math.max( + document.documentElement.offsetHeight, + document.body.offsetHeight + ); + + if ( scrollHeight < offsetHeight ) { + return $( window ).height() + "px"; + } else { + return scrollHeight + "px"; + } + // handle "good" browsers + } else { + return $( document ).height() + "px"; + } + }, + + width: function() { + var scrollWidth, + offsetWidth; + // handle IE + if ( $.ui.ie ) { + scrollWidth = Math.max( + document.documentElement.scrollWidth, + document.body.scrollWidth + ); + offsetWidth = Math.max( + document.documentElement.offsetWidth, + document.body.offsetWidth + ); + + if ( scrollWidth < offsetWidth ) { + return $( window ).width() + "px"; + } else { + return scrollWidth + "px"; + } + // handle "good" browsers + } else { + return $( document ).width() + "px"; + } + }, + + resize: function() { + /* If the dialog is draggable and the user drags it past the + * right edge of the window, the document becomes wider so we + * need to stretch the overlay. If the user then drags the + * dialog back to the left, the document will become narrower, + * so we need to shrink the overlay to the appropriate size. + * This is handled by shrinking the overlay before setting it + * to the full document size. + */ + var $overlays = $( [] ); + $.each( $.ui.dialog.overlay.instances, function() { + $overlays = $overlays.add( this ); + }); + + $overlays.css({ + width: 0, + height: 0 + }).css({ + width: $.ui.dialog.overlay.width(), + height: $.ui.dialog.overlay.height() + }); + } +}); + +$.extend( $.ui.dialog.overlay.prototype, { + destroy: function() { + $.ui.dialog.overlay.destroy( this.$el ); + } +}); + +}( jQuery ) ); + +(function( $, undefined ) { + +var rvertical = /up|down|vertical/, + rpositivemotion = /up|left|vertical|horizontal/; + +$.effects.effect.blind = function( o, done ) { + // Create element + var el = $( this ), + props = [ "position", "top", "bottom", "left", "right", "height", "width" ], + mode = $.effects.setMode( el, o.mode || "hide" ), + direction = o.direction || "up", + vertical = rvertical.test( direction ), + ref = vertical ? "height" : "width", + ref2 = vertical ? "top" : "left", + motion = rpositivemotion.test( direction ), + animation = {}, + show = mode === "show", + wrapper, distance, margin; + + // if already wrapped, the wrapper's properties are my property. #6245 + if ( el.parent().is( ".ui-effects-wrapper" ) ) { + $.effects.save( el.parent(), props ); + } else { + $.effects.save( el, props ); + } + el.show(); + wrapper = $.effects.createWrapper( el ).css({ + overflow: "hidden" + }); + + distance = wrapper[ ref ](); + margin = parseFloat( wrapper.css( ref2 ) ) || 0; + + animation[ ref ] = show ? distance : 0; + if ( !motion ) { + el + .css( vertical ? "bottom" : "right", 0 ) + .css( vertical ? "top" : "left", "auto" ) + .css({ position: "absolute" }); + + animation[ ref2 ] = show ? margin : distance + margin; + } + + // start at 0 if we are showing + if ( show ) { + wrapper.css( ref, 0 ); + if ( ! motion ) { + wrapper.css( ref2, margin + distance ); + } + } + + // Animate + wrapper.animate( animation, { + duration: o.duration, + easing: o.easing, + queue: false, + complete: function() { + if ( mode === "hide" ) { + el.hide(); + } + $.effects.restore( el, props ); + $.effects.removeWrapper( el ); + done(); + } + }); + +}; + +})(jQuery); + +(function( $, undefined ) { + +$.effects.effect.bounce = function( o, done ) { + var el = $( this ), + props = [ "position", "top", "bottom", "left", "right", "height", "width" ], + + // defaults: + mode = $.effects.setMode( el, o.mode || "effect" ), + hide = mode === "hide", + show = mode === "show", + direction = o.direction || "up", + distance = o.distance, + times = o.times || 5, + + // number of internal animations + anims = times * 2 + ( show || hide ? 1 : 0 ), + speed = o.duration / anims, + easing = o.easing, + + // utility: + ref = ( direction === "up" || direction === "down" ) ? "top" : "left", + motion = ( direction === "up" || direction === "left" ), + i, + upAnim, + downAnim, + + // we will need to re-assemble the queue to stack our animations in place + queue = el.queue(), + queuelen = queue.length; + + // Avoid touching opacity to prevent clearType and PNG issues in IE + if ( show || hide ) { + props.push( "opacity" ); + } + + $.effects.save( el, props ); + el.show(); + $.effects.createWrapper( el ); // Create Wrapper + + // default distance for the BIGGEST bounce is the outer Distance / 3 + if ( !distance ) { + distance = el[ ref === "top" ? "outerHeight" : "outerWidth" ]() / 3; + } + + if ( show ) { + downAnim = { opacity: 1 }; + downAnim[ ref ] = 0; + + // if we are showing, force opacity 0 and set the initial position + // then do the "first" animation + el.css( "opacity", 0 ) + .css( ref, motion ? -distance * 2 : distance * 2 ) + .animate( downAnim, speed, easing ); + } + + // start at the smallest distance if we are hiding + if ( hide ) { + distance = distance / Math.pow( 2, times - 1 ); + } + + downAnim = {}; + downAnim[ ref ] = 0; + // Bounces up/down/left/right then back to 0 -- times * 2 animations happen here + for ( i = 0; i < times; i++ ) { + upAnim = {}; + upAnim[ ref ] = ( motion ? "-=" : "+=" ) + distance; + + el.animate( upAnim, speed, easing ) + .animate( downAnim, speed, easing ); + + distance = hide ? distance * 2 : distance / 2; + } + + // Last Bounce when Hiding + if ( hide ) { + upAnim = { opacity: 0 }; + upAnim[ ref ] = ( motion ? "-=" : "+=" ) + distance; + + el.animate( upAnim, speed, easing ); + } + + el.queue(function() { + if ( hide ) { + el.hide(); + } + $.effects.restore( el, props ); + $.effects.removeWrapper( el ); + done(); + }); + + // inject all the animations we just queued to be first in line (after "inprogress") + if ( queuelen > 1) { + queue.splice.apply( queue, + [ 1, 0 ].concat( queue.splice( queuelen, anims + 1 ) ) ); + } + el.dequeue(); + +}; + +})(jQuery); + +(function( $, undefined ) { + +$.effects.effect.clip = function( o, done ) { + // Create element + var el = $( this ), + props = [ "position", "top", "bottom", "left", "right", "height", "width" ], + mode = $.effects.setMode( el, o.mode || "hide" ), + show = mode === "show", + direction = o.direction || "vertical", + vert = direction === "vertical", + size = vert ? "height" : "width", + position = vert ? "top" : "left", + animation = {}, + wrapper, animate, distance; + + // Save & Show + $.effects.save( el, props ); + el.show(); + + // Create Wrapper + wrapper = $.effects.createWrapper( el ).css({ + overflow: "hidden" + }); + animate = ( el[0].tagName === "IMG" ) ? wrapper : el; + distance = animate[ size ](); + + // Shift + if ( show ) { + animate.css( size, 0 ); + animate.css( position, distance / 2 ); + } + + // Create Animation Object: + animation[ size ] = show ? distance : 0; + animation[ position ] = show ? 0 : distance / 2; + + // Animate + animate.animate( animation, { + queue: false, + duration: o.duration, + easing: o.easing, + complete: function() { + if ( !show ) { + el.hide(); + } + $.effects.restore( el, props ); + $.effects.removeWrapper( el ); + done(); + } + }); + +}; + +})(jQuery); + +(function( $, undefined ) { + +$.effects.effect.drop = function( o, done ) { + + var el = $( this ), + props = [ "position", "top", "bottom", "left", "right", "opacity", "height", "width" ], + mode = $.effects.setMode( el, o.mode || "hide" ), + show = mode === "show", + direction = o.direction || "left", + ref = ( direction === "up" || direction === "down" ) ? "top" : "left", + motion = ( direction === "up" || direction === "left" ) ? "pos" : "neg", + animation = { + opacity: show ? 1 : 0 + }, + distance; + + // Adjust + $.effects.save( el, props ); + el.show(); + $.effects.createWrapper( el ); + + distance = o.distance || el[ ref === "top" ? "outerHeight": "outerWidth" ]( true ) / 2; + + if ( show ) { + el + .css( "opacity", 0 ) + .css( ref, motion === "pos" ? -distance : distance ); + } + + // Animation + animation[ ref ] = ( show ? + ( motion === "pos" ? "+=" : "-=" ) : + ( motion === "pos" ? "-=" : "+=" ) ) + + distance; + + // Animate + el.animate( animation, { + queue: false, + duration: o.duration, + easing: o.easing, + complete: function() { + if ( mode === "hide" ) { + el.hide(); + } + $.effects.restore( el, props ); + $.effects.removeWrapper( el ); + done(); + } + }); +}; + +})(jQuery); + +(function( $, undefined ) { + +$.effects.effect.explode = function( o, done ) { + + var rows = o.pieces ? Math.round( Math.sqrt( o.pieces ) ) : 3, + cells = rows, + el = $( this ), + mode = $.effects.setMode( el, o.mode || "hide" ), + show = mode === "show", + + // show and then visibility:hidden the element before calculating offset + offset = el.show().css( "visibility", "hidden" ).offset(), + + // width and height of a piece + width = Math.ceil( el.outerWidth() / cells ), + height = Math.ceil( el.outerHeight() / rows ), + pieces = [], + + // loop + i, j, left, top, mx, my; + + // children animate complete: + function childComplete() { + pieces.push( this ); + if ( pieces.length === rows * cells ) { + animComplete(); + } + } + + // clone the element for each row and cell. + for( i = 0; i < rows ; i++ ) { // ===> + top = offset.top + i * height; + my = i - ( rows - 1 ) / 2 ; + + for( j = 0; j < cells ; j++ ) { // ||| + left = offset.left + j * width; + mx = j - ( cells - 1 ) / 2 ; + + // Create a clone of the now hidden main element that will be absolute positioned + // within a wrapper div off the -left and -top equal to size of our pieces + el + .clone() + .appendTo( "body" ) + .wrap( "
" ) + .css({ + position: "absolute", + visibility: "visible", + left: -j * width, + top: -i * height + }) + + // select the wrapper - make it overflow: hidden and absolute positioned based on + // where the original was located +left and +top equal to the size of pieces + .parent() + .addClass( "ui-effects-explode" ) + .css({ + position: "absolute", + overflow: "hidden", + width: width, + height: height, + left: left + ( show ? mx * width : 0 ), + top: top + ( show ? my * height : 0 ), + opacity: show ? 0 : 1 + }).animate({ + left: left + ( show ? 0 : mx * width ), + top: top + ( show ? 0 : my * height ), + opacity: show ? 1 : 0 + }, o.duration || 500, o.easing, childComplete ); + } + } + + function animComplete() { + el.css({ + visibility: "visible" + }); + $( pieces ).remove(); + if ( !show ) { + el.hide(); + } + done(); + } +}; + +})(jQuery); + +(function( $, undefined ) { + +$.effects.effect.fade = function( o, done ) { + var el = $( this ), + mode = $.effects.setMode( el, o.mode || "toggle" ); + + el.animate({ + opacity: mode + }, { + queue: false, + duration: o.duration, + easing: o.easing, + complete: done + }); +}; + +})( jQuery ); + +(function( $, undefined ) { + +$.effects.effect.fold = function( o, done ) { + + // Create element + var el = $( this ), + props = [ "position", "top", "bottom", "left", "right", "height", "width" ], + mode = $.effects.setMode( el, o.mode || "hide" ), + show = mode === "show", + hide = mode === "hide", + size = o.size || 15, + percent = /([0-9]+)%/.exec( size ), + horizFirst = !!o.horizFirst, + widthFirst = show !== horizFirst, + ref = widthFirst ? [ "width", "height" ] : [ "height", "width" ], + duration = o.duration / 2, + wrapper, distance, + animation1 = {}, + animation2 = {}; + + $.effects.save( el, props ); + el.show(); + + // Create Wrapper + wrapper = $.effects.createWrapper( el ).css({ + overflow: "hidden" + }); + distance = widthFirst ? + [ wrapper.width(), wrapper.height() ] : + [ wrapper.height(), wrapper.width() ]; + + if ( percent ) { + size = parseInt( percent[ 1 ], 10 ) / 100 * distance[ hide ? 0 : 1 ]; + } + if ( show ) { + wrapper.css( horizFirst ? { + height: 0, + width: size + } : { + height: size, + width: 0 + }); + } + + // Animation + animation1[ ref[ 0 ] ] = show ? distance[ 0 ] : size; + animation2[ ref[ 1 ] ] = show ? distance[ 1 ] : 0; + + // Animate + wrapper + .animate( animation1, duration, o.easing ) + .animate( animation2, duration, o.easing, function() { + if ( hide ) { + el.hide(); + } + $.effects.restore( el, props ); + $.effects.removeWrapper( el ); + done(); + }); + +}; + +})(jQuery); + +(function( $, undefined ) { + +$.effects.effect.highlight = function( o, done ) { + var elem = $( this ), + props = [ "backgroundImage", "backgroundColor", "opacity" ], + mode = $.effects.setMode( elem, o.mode || "show" ), + animation = { + backgroundColor: elem.css( "backgroundColor" ) + }; + + if (mode === "hide") { + animation.opacity = 0; + } + + $.effects.save( elem, props ); + + elem + .show() + .css({ + backgroundImage: "none", + backgroundColor: o.color || "#ffff99" + }) + .animate( animation, { + queue: false, + duration: o.duration, + easing: o.easing, + complete: function() { + if ( mode === "hide" ) { + elem.hide(); + } + $.effects.restore( elem, props ); + done(); + } + }); +}; + +})(jQuery); + +(function( $, undefined ) { + +$.effects.effect.pulsate = function( o, done ) { + var elem = $( this ), + mode = $.effects.setMode( elem, o.mode || "show" ), + show = mode === "show", + hide = mode === "hide", + showhide = ( show || mode === "hide" ), + + // showing or hiding leaves of the "last" animation + anims = ( ( o.times || 5 ) * 2 ) + ( showhide ? 1 : 0 ), + duration = o.duration / anims, + animateTo = 0, + queue = elem.queue(), + queuelen = queue.length, + i; + + if ( show || !elem.is(":visible")) { + elem.css( "opacity", 0 ).show(); + animateTo = 1; + } + + // anims - 1 opacity "toggles" + for ( i = 1; i < anims; i++ ) { + elem.animate({ + opacity: animateTo + }, duration, o.easing ); + animateTo = 1 - animateTo; + } + + elem.animate({ + opacity: animateTo + }, duration, o.easing); + + elem.queue(function() { + if ( hide ) { + elem.hide(); + } + done(); + }); + + // We just queued up "anims" animations, we need to put them next in the queue + if ( queuelen > 1 ) { + queue.splice.apply( queue, + [ 1, 0 ].concat( queue.splice( queuelen, anims + 1 ) ) ); + } + elem.dequeue(); +}; + +})(jQuery); + +(function( $, undefined ) { + +$.effects.effect.puff = function( o, done ) { + var elem = $( this ), + mode = $.effects.setMode( elem, o.mode || "hide" ), + hide = mode === "hide", + percent = parseInt( o.percent, 10 ) || 150, + factor = percent / 100, + original = { + height: elem.height(), + width: elem.width(), + outerHeight: elem.outerHeight(), + outerWidth: elem.outerWidth() + }; + + $.extend( o, { + effect: "scale", + queue: false, + fade: true, + mode: mode, + complete: done, + percent: hide ? percent : 100, + from: hide ? + original : + { + height: original.height * factor, + width: original.width * factor, + outerHeight: original.outerHeight * factor, + outerWidth: original.outerWidth * factor + } + }); + + elem.effect( o ); +}; + +$.effects.effect.scale = function( o, done ) { + + // Create element + var el = $( this ), + options = $.extend( true, {}, o ), + mode = $.effects.setMode( el, o.mode || "effect" ), + percent = parseInt( o.percent, 10 ) || + ( parseInt( o.percent, 10 ) === 0 ? 0 : ( mode === "hide" ? 0 : 100 ) ), + direction = o.direction || "both", + origin = o.origin, + original = { + height: el.height(), + width: el.width(), + outerHeight: el.outerHeight(), + outerWidth: el.outerWidth() + }, + factor = { + y: direction !== "horizontal" ? (percent / 100) : 1, + x: direction !== "vertical" ? (percent / 100) : 1 + }; + + // We are going to pass this effect to the size effect: + options.effect = "size"; + options.queue = false; + options.complete = done; + + // Set default origin and restore for show/hide + if ( mode !== "effect" ) { + options.origin = origin || ["middle","center"]; + options.restore = true; + } + + options.from = o.from || ( mode === "show" ? { + height: 0, + width: 0, + outerHeight: 0, + outerWidth: 0 + } : original ); + options.to = { + height: original.height * factor.y, + width: original.width * factor.x, + outerHeight: original.outerHeight * factor.y, + outerWidth: original.outerWidth * factor.x + }; + + // Fade option to support puff + if ( options.fade ) { + if ( mode === "show" ) { + options.from.opacity = 0; + options.to.opacity = 1; + } + if ( mode === "hide" ) { + options.from.opacity = 1; + options.to.opacity = 0; + } + } + + // Animate + el.effect( options ); + +}; + +$.effects.effect.size = function( o, done ) { + + // Create element + var original, baseline, factor, + el = $( this ), + props0 = [ "position", "top", "bottom", "left", "right", "width", "height", "overflow", "opacity" ], + + // Always restore + props1 = [ "position", "top", "bottom", "left", "right", "overflow", "opacity" ], + + // Copy for children + props2 = [ "width", "height", "overflow" ], + cProps = [ "fontSize" ], + vProps = [ "borderTopWidth", "borderBottomWidth", "paddingTop", "paddingBottom" ], + hProps = [ "borderLeftWidth", "borderRightWidth", "paddingLeft", "paddingRight" ], + + // Set options + mode = $.effects.setMode( el, o.mode || "effect" ), + restore = o.restore || mode !== "effect", + scale = o.scale || "both", + origin = o.origin || [ "middle", "center" ], + position = el.css( "position" ), + props = restore ? props0 : props1, + zero = { + height: 0, + width: 0, + outerHeight: 0, + outerWidth: 0 + }; + + if ( mode === "show" ) { + el.show(); + } + original = { + height: el.height(), + width: el.width(), + outerHeight: el.outerHeight(), + outerWidth: el.outerWidth() + }; + + if ( o.mode === "toggle" && mode === "show" ) { + el.from = o.to || zero; + el.to = o.from || original; + } else { + el.from = o.from || ( mode === "show" ? zero : original ); + el.to = o.to || ( mode === "hide" ? zero : original ); + } + + // Set scaling factor + factor = { + from: { + y: el.from.height / original.height, + x: el.from.width / original.width + }, + to: { + y: el.to.height / original.height, + x: el.to.width / original.width + } + }; + + // Scale the css box + if ( scale === "box" || scale === "both" ) { + + // Vertical props scaling + if ( factor.from.y !== factor.to.y ) { + props = props.concat( vProps ); + el.from = $.effects.setTransition( el, vProps, factor.from.y, el.from ); + el.to = $.effects.setTransition( el, vProps, factor.to.y, el.to ); + } + + // Horizontal props scaling + if ( factor.from.x !== factor.to.x ) { + props = props.concat( hProps ); + el.from = $.effects.setTransition( el, hProps, factor.from.x, el.from ); + el.to = $.effects.setTransition( el, hProps, factor.to.x, el.to ); + } + } + + // Scale the content + if ( scale === "content" || scale === "both" ) { + + // Vertical props scaling + if ( factor.from.y !== factor.to.y ) { + props = props.concat( cProps ).concat( props2 ); + el.from = $.effects.setTransition( el, cProps, factor.from.y, el.from ); + el.to = $.effects.setTransition( el, cProps, factor.to.y, el.to ); + } + } + + $.effects.save( el, props ); + el.show(); + $.effects.createWrapper( el ); + el.css( "overflow", "hidden" ).css( el.from ); + + // Adjust + if (origin) { // Calculate baseline shifts + baseline = $.effects.getBaseline( origin, original ); + el.from.top = ( original.outerHeight - el.outerHeight() ) * baseline.y; + el.from.left = ( original.outerWidth - el.outerWidth() ) * baseline.x; + el.to.top = ( original.outerHeight - el.to.outerHeight ) * baseline.y; + el.to.left = ( original.outerWidth - el.to.outerWidth ) * baseline.x; + } + el.css( el.from ); // set top & left + + // Animate + if ( scale === "content" || scale === "both" ) { // Scale the children + + // Add margins/font-size + vProps = vProps.concat([ "marginTop", "marginBottom" ]).concat(cProps); + hProps = hProps.concat([ "marginLeft", "marginRight" ]); + props2 = props0.concat(vProps).concat(hProps); + + el.find( "*[width]" ).each( function(){ + var child = $( this ), + c_original = { + height: child.height(), + width: child.width(), + outerHeight: child.outerHeight(), + outerWidth: child.outerWidth() + }; + if (restore) { + $.effects.save(child, props2); + } + + child.from = { + height: c_original.height * factor.from.y, + width: c_original.width * factor.from.x, + outerHeight: c_original.outerHeight * factor.from.y, + outerWidth: c_original.outerWidth * factor.from.x + }; + child.to = { + height: c_original.height * factor.to.y, + width: c_original.width * factor.to.x, + outerHeight: c_original.height * factor.to.y, + outerWidth: c_original.width * factor.to.x + }; + + // Vertical props scaling + if ( factor.from.y !== factor.to.y ) { + child.from = $.effects.setTransition( child, vProps, factor.from.y, child.from ); + child.to = $.effects.setTransition( child, vProps, factor.to.y, child.to ); + } + + // Horizontal props scaling + if ( factor.from.x !== factor.to.x ) { + child.from = $.effects.setTransition( child, hProps, factor.from.x, child.from ); + child.to = $.effects.setTransition( child, hProps, factor.to.x, child.to ); + } + + // Animate children + child.css( child.from ); + child.animate( child.to, o.duration, o.easing, function() { + + // Restore children + if ( restore ) { + $.effects.restore( child, props2 ); + } + }); + }); + } + + // Animate + el.animate( el.to, { + queue: false, + duration: o.duration, + easing: o.easing, + complete: function() { + if ( el.to.opacity === 0 ) { + el.css( "opacity", el.from.opacity ); + } + if( mode === "hide" ) { + el.hide(); + } + $.effects.restore( el, props ); + if ( !restore ) { + + // we need to calculate our new positioning based on the scaling + if ( position === "static" ) { + el.css({ + position: "relative", + top: el.to.top, + left: el.to.left + }); + } else { + $.each([ "top", "left" ], function( idx, pos ) { + el.css( pos, function( _, str ) { + var val = parseInt( str, 10 ), + toRef = idx ? el.to.left : el.to.top; + + // if original was "auto", recalculate the new value from wrapper + if ( str === "auto" ) { + return toRef + "px"; + } + + return val + toRef + "px"; + }); + }); + } + } + + $.effects.removeWrapper( el ); + done(); + } + }); + +}; + +})(jQuery); + +(function( $, undefined ) { + +$.effects.effect.shake = function( o, done ) { + + var el = $( this ), + props = [ "position", "top", "bottom", "left", "right", "height", "width" ], + mode = $.effects.setMode( el, o.mode || "effect" ), + direction = o.direction || "left", + distance = o.distance || 20, + times = o.times || 3, + anims = times * 2 + 1, + speed = Math.round(o.duration/anims), + ref = (direction === "up" || direction === "down") ? "top" : "left", + positiveMotion = (direction === "up" || direction === "left"), + animation = {}, + animation1 = {}, + animation2 = {}, + i, + + // we will need to re-assemble the queue to stack our animations in place + queue = el.queue(), + queuelen = queue.length; + + $.effects.save( el, props ); + el.show(); + $.effects.createWrapper( el ); + + // Animation + animation[ ref ] = ( positiveMotion ? "-=" : "+=" ) + distance; + animation1[ ref ] = ( positiveMotion ? "+=" : "-=" ) + distance * 2; + animation2[ ref ] = ( positiveMotion ? "-=" : "+=" ) + distance * 2; + + // Animate + el.animate( animation, speed, o.easing ); + + // Shakes + for ( i = 1; i < times; i++ ) { + el.animate( animation1, speed, o.easing ).animate( animation2, speed, o.easing ); + } + el + .animate( animation1, speed, o.easing ) + .animate( animation, speed / 2, o.easing ) + .queue(function() { + if ( mode === "hide" ) { + el.hide(); + } + $.effects.restore( el, props ); + $.effects.removeWrapper( el ); + done(); + }); + + // inject all the animations we just queued to be first in line (after "inprogress") + if ( queuelen > 1) { + queue.splice.apply( queue, + [ 1, 0 ].concat( queue.splice( queuelen, anims + 1 ) ) ); + } + el.dequeue(); + +}; + +})(jQuery); + +(function( $, undefined ) { + +$.effects.effect.slide = function( o, done ) { + + // Create element + var el = $( this ), + props = [ "position", "top", "bottom", "left", "right", "width", "height" ], + mode = $.effects.setMode( el, o.mode || "show" ), + show = mode === "show", + direction = o.direction || "left", + ref = (direction === "up" || direction === "down") ? "top" : "left", + positiveMotion = (direction === "up" || direction === "left"), + distance, + animation = {}; + + // Adjust + $.effects.save( el, props ); + el.show(); + distance = o.distance || el[ ref === "top" ? "outerHeight" : "outerWidth" ]( true ); + + $.effects.createWrapper( el ).css({ + overflow: "hidden" + }); + + if ( show ) { + el.css( ref, positiveMotion ? (isNaN(distance) ? "-" + distance : -distance) : distance ); + } + + // Animation + animation[ ref ] = ( show ? + ( positiveMotion ? "+=" : "-=") : + ( positiveMotion ? "-=" : "+=")) + + distance; + + // Animate + el.animate( animation, { + queue: false, + duration: o.duration, + easing: o.easing, + complete: function() { + if ( mode === "hide" ) { + el.hide(); + } + $.effects.restore( el, props ); + $.effects.removeWrapper( el ); + done(); + } + }); +}; + +})(jQuery); + +(function( $, undefined ) { + +$.effects.effect.transfer = function( o, done ) { + var elem = $( this ), + target = $( o.to ), + targetFixed = target.css( "position" ) === "fixed", + body = $("body"), + fixTop = targetFixed ? body.scrollTop() : 0, + fixLeft = targetFixed ? body.scrollLeft() : 0, + endPosition = target.offset(), + animation = { + top: endPosition.top - fixTop , + left: endPosition.left - fixLeft , + height: target.innerHeight(), + width: target.innerWidth() + }, + startPosition = elem.offset(), + transfer = $( '
' ) + .appendTo( document.body ) + .addClass( o.className ) + .css({ + top: startPosition.top - fixTop , + left: startPosition.left - fixLeft , + height: elem.innerHeight(), + width: elem.innerWidth(), + position: targetFixed ? "fixed" : "absolute" + }) + .animate( animation, o.duration, o.easing, function() { + transfer.remove(); + done(); + }); +}; + +})(jQuery); + +(function( $, undefined ) { + +var mouseHandled = false; + +$.widget( "ui.menu", { + version: "1.9.2", + defaultElement: "
    ", + delay: 300, + options: { + icons: { + submenu: "ui-icon-carat-1-e" + }, + menus: "ul", + position: { + my: "left top", + at: "right top" + }, + role: "menu", + + // callbacks + blur: null, + focus: null, + select: null + }, + + _create: function() { + this.activeMenu = this.element; + this.element + .uniqueId() + .addClass( "ui-menu ui-widget ui-widget-content ui-corner-all" ) + .toggleClass( "ui-menu-icons", !!this.element.find( ".ui-icon" ).length ) + .attr({ + role: this.options.role, + tabIndex: 0 + }) + // need to catch all clicks on disabled menu + // not possible through _on + .bind( "click" + this.eventNamespace, $.proxy(function( event ) { + if ( this.options.disabled ) { + event.preventDefault(); + } + }, this )); + + if ( this.options.disabled ) { + this.element + .addClass( "ui-state-disabled" ) + .attr( "aria-disabled", "true" ); + } + + this._on({ + // Prevent focus from sticking to links inside menu after clicking + // them (focus should always stay on UL during navigation). + "mousedown .ui-menu-item > a": function( event ) { + event.preventDefault(); + }, + "click .ui-state-disabled > a": function( event ) { + event.preventDefault(); + }, + "click .ui-menu-item:has(a)": function( event ) { + var target = $( event.target ).closest( ".ui-menu-item" ); + if ( !mouseHandled && target.not( ".ui-state-disabled" ).length ) { + mouseHandled = true; + + this.select( event ); + // Open submenu on click + if ( target.has( ".ui-menu" ).length ) { + this.expand( event ); + } else if ( !this.element.is( ":focus" ) ) { + // Redirect focus to the menu + this.element.trigger( "focus", [ true ] ); + + // If the active item is on the top level, let it stay active. + // Otherwise, blur the active item since it is no longer visible. + if ( this.active && this.active.parents( ".ui-menu" ).length === 1 ) { + clearTimeout( this.timer ); + } + } + } + }, + "mouseenter .ui-menu-item": function( event ) { + var target = $( event.currentTarget ); + // Remove ui-state-active class from siblings of the newly focused menu item + // to avoid a jump caused by adjacent elements both having a class with a border + target.siblings().children( ".ui-state-active" ).removeClass( "ui-state-active" ); + this.focus( event, target ); + }, + mouseleave: "collapseAll", + "mouseleave .ui-menu": "collapseAll", + focus: function( event, keepActiveItem ) { + // If there's already an active item, keep it active + // If not, activate the first item + var item = this.active || this.element.children( ".ui-menu-item" ).eq( 0 ); + + if ( !keepActiveItem ) { + this.focus( event, item ); + } + }, + blur: function( event ) { + this._delay(function() { + if ( !$.contains( this.element[0], this.document[0].activeElement ) ) { + this.collapseAll( event ); + } + }); + }, + keydown: "_keydown" + }); + + this.refresh(); + + // Clicks outside of a menu collapse any open menus + this._on( this.document, { + click: function( event ) { + if ( !$( event.target ).closest( ".ui-menu" ).length ) { + this.collapseAll( event ); + } + + // Reset the mouseHandled flag + mouseHandled = false; + } + }); + }, + + _destroy: function() { + // Destroy (sub)menus + this.element + .removeAttr( "aria-activedescendant" ) + .find( ".ui-menu" ).andSelf() + .removeClass( "ui-menu ui-widget ui-widget-content ui-corner-all ui-menu-icons" ) + .removeAttr( "role" ) + .removeAttr( "tabIndex" ) + .removeAttr( "aria-labelledby" ) + .removeAttr( "aria-expanded" ) + .removeAttr( "aria-hidden" ) + .removeAttr( "aria-disabled" ) + .removeUniqueId() + .show(); + + // Destroy menu items + this.element.find( ".ui-menu-item" ) + .removeClass( "ui-menu-item" ) + .removeAttr( "role" ) + .removeAttr( "aria-disabled" ) + .children( "a" ) + .removeUniqueId() + .removeClass( "ui-corner-all ui-state-hover" ) + .removeAttr( "tabIndex" ) + .removeAttr( "role" ) + .removeAttr( "aria-haspopup" ) + .children().each( function() { + var elem = $( this ); + if ( elem.data( "ui-menu-submenu-carat" ) ) { + elem.remove(); + } + }); + + // Destroy menu dividers + this.element.find( ".ui-menu-divider" ).removeClass( "ui-menu-divider ui-widget-content" ); + }, + + _keydown: function( event ) { + var match, prev, character, skip, regex, + preventDefault = true; + + function escape( value ) { + return value.replace( /[\-\[\]{}()*+?.,\\\^$|#\s]/g, "\\$&" ); + } + + switch ( event.keyCode ) { + case $.ui.keyCode.PAGE_UP: + this.previousPage( event ); + break; + case $.ui.keyCode.PAGE_DOWN: + this.nextPage( event ); + break; + case $.ui.keyCode.HOME: + this._move( "first", "first", event ); + break; + case $.ui.keyCode.END: + this._move( "last", "last", event ); + break; + case $.ui.keyCode.UP: + this.previous( event ); + break; + case $.ui.keyCode.DOWN: + this.next( event ); + break; + case $.ui.keyCode.LEFT: + this.collapse( event ); + break; + case $.ui.keyCode.RIGHT: + if ( this.active && !this.active.is( ".ui-state-disabled" ) ) { + this.expand( event ); + } + break; + case $.ui.keyCode.ENTER: + case $.ui.keyCode.SPACE: + this._activate( event ); + break; + case $.ui.keyCode.ESCAPE: + this.collapse( event ); + break; + default: + preventDefault = false; + prev = this.previousFilter || ""; + character = String.fromCharCode( event.keyCode ); + skip = false; + + clearTimeout( this.filterTimer ); + + if ( character === prev ) { + skip = true; + } else { + character = prev + character; + } + + regex = new RegExp( "^" + escape( character ), "i" ); + match = this.activeMenu.children( ".ui-menu-item" ).filter(function() { + return regex.test( $( this ).children( "a" ).text() ); + }); + match = skip && match.index( this.active.next() ) !== -1 ? + this.active.nextAll( ".ui-menu-item" ) : + match; + + // If no matches on the current filter, reset to the last character pressed + // to move down the menu to the first item that starts with that character + if ( !match.length ) { + character = String.fromCharCode( event.keyCode ); + regex = new RegExp( "^" + escape( character ), "i" ); + match = this.activeMenu.children( ".ui-menu-item" ).filter(function() { + return regex.test( $( this ).children( "a" ).text() ); + }); + } + + if ( match.length ) { + this.focus( event, match ); + if ( match.length > 1 ) { + this.previousFilter = character; + this.filterTimer = this._delay(function() { + delete this.previousFilter; + }, 1000 ); + } else { + delete this.previousFilter; + } + } else { + delete this.previousFilter; + } + } + + if ( preventDefault ) { + event.preventDefault(); + } + }, + + _activate: function( event ) { + if ( !this.active.is( ".ui-state-disabled" ) ) { + if ( this.active.children( "a[aria-haspopup='true']" ).length ) { + this.expand( event ); + } else { + this.select( event ); + } + } + }, + + refresh: function() { + var menus, + icon = this.options.icons.submenu, + submenus = this.element.find( this.options.menus ); + + // Initialize nested menus + submenus.filter( ":not(.ui-menu)" ) + .addClass( "ui-menu ui-widget ui-widget-content ui-corner-all" ) + .hide() + .attr({ + role: this.options.role, + "aria-hidden": "true", + "aria-expanded": "false" + }) + .each(function() { + var menu = $( this ), + item = menu.prev( "a" ), + submenuCarat = $( "" ) + .addClass( "ui-menu-icon ui-icon " + icon ) + .data( "ui-menu-submenu-carat", true ); + + item + .attr( "aria-haspopup", "true" ) + .prepend( submenuCarat ); + menu.attr( "aria-labelledby", item.attr( "id" ) ); + }); + + menus = submenus.add( this.element ); + + // Don't refresh list items that are already adapted + menus.children( ":not(.ui-menu-item):has(a)" ) + .addClass( "ui-menu-item" ) + .attr( "role", "presentation" ) + .children( "a" ) + .uniqueId() + .addClass( "ui-corner-all" ) + .attr({ + tabIndex: -1, + role: this._itemRole() + }); + + // Initialize unlinked menu-items containing spaces and/or dashes only as dividers + menus.children( ":not(.ui-menu-item)" ).each(function() { + var item = $( this ); + // hyphen, em dash, en dash + if ( !/[^\-—–\s]/.test( item.text() ) ) { + item.addClass( "ui-widget-content ui-menu-divider" ); + } + }); + + // Add aria-disabled attribute to any disabled menu item + menus.children( ".ui-state-disabled" ).attr( "aria-disabled", "true" ); + + // If the active item has been removed, blur the menu + if ( this.active && !$.contains( this.element[ 0 ], this.active[ 0 ] ) ) { + this.blur(); + } + }, + + _itemRole: function() { + return { + menu: "menuitem", + listbox: "option" + }[ this.options.role ]; + }, + + focus: function( event, item ) { + var nested, focused; + this.blur( event, event && event.type === "focus" ); + + this._scrollIntoView( item ); + + this.active = item.first(); + focused = this.active.children( "a" ).addClass( "ui-state-focus" ); + // Only update aria-activedescendant if there's a role + // otherwise we assume focus is managed elsewhere + if ( this.options.role ) { + this.element.attr( "aria-activedescendant", focused.attr( "id" ) ); + } + + // Highlight active parent menu item, if any + this.active + .parent() + .closest( ".ui-menu-item" ) + .children( "a:first" ) + .addClass( "ui-state-active" ); + + if ( event && event.type === "keydown" ) { + this._close(); + } else { + this.timer = this._delay(function() { + this._close(); + }, this.delay ); + } + + nested = item.children( ".ui-menu" ); + if ( nested.length && ( /^mouse/.test( event.type ) ) ) { + this._startOpening(nested); + } + this.activeMenu = item.parent(); + + this._trigger( "focus", event, { item: item } ); + }, + + _scrollIntoView: function( item ) { + var borderTop, paddingTop, offset, scroll, elementHeight, itemHeight; + if ( this._hasScroll() ) { + borderTop = parseFloat( $.css( this.activeMenu[0], "borderTopWidth" ) ) || 0; + paddingTop = parseFloat( $.css( this.activeMenu[0], "paddingTop" ) ) || 0; + offset = item.offset().top - this.activeMenu.offset().top - borderTop - paddingTop; + scroll = this.activeMenu.scrollTop(); + elementHeight = this.activeMenu.height(); + itemHeight = item.height(); + + if ( offset < 0 ) { + this.activeMenu.scrollTop( scroll + offset ); + } else if ( offset + itemHeight > elementHeight ) { + this.activeMenu.scrollTop( scroll + offset - elementHeight + itemHeight ); + } + } + }, + + blur: function( event, fromFocus ) { + if ( !fromFocus ) { + clearTimeout( this.timer ); + } + + if ( !this.active ) { + return; + } + + this.active.children( "a" ).removeClass( "ui-state-focus" ); + this.active = null; + + this._trigger( "blur", event, { item: this.active } ); + }, + + _startOpening: function( submenu ) { + clearTimeout( this.timer ); + + // Don't open if already open fixes a Firefox bug that caused a .5 pixel + // shift in the submenu position when mousing over the carat icon + if ( submenu.attr( "aria-hidden" ) !== "true" ) { + return; + } + + this.timer = this._delay(function() { + this._close(); + this._open( submenu ); + }, this.delay ); + }, + + _open: function( submenu ) { + var position = $.extend({ + of: this.active + }, this.options.position ); + + clearTimeout( this.timer ); + this.element.find( ".ui-menu" ).not( submenu.parents( ".ui-menu" ) ) + .hide() + .attr( "aria-hidden", "true" ); + + submenu + .show() + .removeAttr( "aria-hidden" ) + .attr( "aria-expanded", "true" ) + .position( position ); + }, + + collapseAll: function( event, all ) { + clearTimeout( this.timer ); + this.timer = this._delay(function() { + // If we were passed an event, look for the submenu that contains the event + var currentMenu = all ? this.element : + $( event && event.target ).closest( this.element.find( ".ui-menu" ) ); + + // If we found no valid submenu ancestor, use the main menu to close all sub menus anyway + if ( !currentMenu.length ) { + currentMenu = this.element; + } + + this._close( currentMenu ); + + this.blur( event ); + this.activeMenu = currentMenu; + }, this.delay ); + }, + + // With no arguments, closes the currently active menu - if nothing is active + // it closes all menus. If passed an argument, it will search for menus BELOW + _close: function( startMenu ) { + if ( !startMenu ) { + startMenu = this.active ? this.active.parent() : this.element; + } + + startMenu + .find( ".ui-menu" ) + .hide() + .attr( "aria-hidden", "true" ) + .attr( "aria-expanded", "false" ) + .end() + .find( "a.ui-state-active" ) + .removeClass( "ui-state-active" ); + }, + + collapse: function( event ) { + var newItem = this.active && + this.active.parent().closest( ".ui-menu-item", this.element ); + if ( newItem && newItem.length ) { + this._close(); + this.focus( event, newItem ); + } + }, + + expand: function( event ) { + var newItem = this.active && + this.active + .children( ".ui-menu " ) + .children( ".ui-menu-item" ) + .first(); + + if ( newItem && newItem.length ) { + this._open( newItem.parent() ); + + // Delay so Firefox will not hide activedescendant change in expanding submenu from AT + this._delay(function() { + this.focus( event, newItem ); + }); + } + }, + + next: function( event ) { + this._move( "next", "first", event ); + }, + + previous: function( event ) { + this._move( "prev", "last", event ); + }, + + isFirstItem: function() { + return this.active && !this.active.prevAll( ".ui-menu-item" ).length; + }, + + isLastItem: function() { + return this.active && !this.active.nextAll( ".ui-menu-item" ).length; + }, + + _move: function( direction, filter, event ) { + var next; + if ( this.active ) { + if ( direction === "first" || direction === "last" ) { + next = this.active + [ direction === "first" ? "prevAll" : "nextAll" ]( ".ui-menu-item" ) + .eq( -1 ); + } else { + next = this.active + [ direction + "All" ]( ".ui-menu-item" ) + .eq( 0 ); + } + } + if ( !next || !next.length || !this.active ) { + next = this.activeMenu.children( ".ui-menu-item" )[ filter ](); + } + + this.focus( event, next ); + }, + + nextPage: function( event ) { + var item, base, height; + + if ( !this.active ) { + this.next( event ); + return; + } + if ( this.isLastItem() ) { + return; + } + if ( this._hasScroll() ) { + base = this.active.offset().top; + height = this.element.height(); + this.active.nextAll( ".ui-menu-item" ).each(function() { + item = $( this ); + return item.offset().top - base - height < 0; + }); + + this.focus( event, item ); + } else { + this.focus( event, this.activeMenu.children( ".ui-menu-item" ) + [ !this.active ? "first" : "last" ]() ); + } + }, + + previousPage: function( event ) { + var item, base, height; + if ( !this.active ) { + this.next( event ); + return; + } + if ( this.isFirstItem() ) { + return; + } + if ( this._hasScroll() ) { + base = this.active.offset().top; + height = this.element.height(); + this.active.prevAll( ".ui-menu-item" ).each(function() { + item = $( this ); + return item.offset().top - base + height > 0; + }); + + this.focus( event, item ); + } else { + this.focus( event, this.activeMenu.children( ".ui-menu-item" ).first() ); + } + }, + + _hasScroll: function() { + return this.element.outerHeight() < this.element.prop( "scrollHeight" ); + }, + + select: function( event ) { + // TODO: It should never be possible to not have an active item at this + // point, but the tests don't trigger mouseenter before click. + this.active = this.active || $( event.target ).closest( ".ui-menu-item" ); + var ui = { item: this.active }; + if ( !this.active.has( ".ui-menu" ).length ) { + this.collapseAll( event, true ); + } + this._trigger( "select", event, ui ); + } +}); + +}( jQuery )); + +(function( $, undefined ) { + +$.ui = $.ui || {}; + +var cachedScrollbarWidth, + max = Math.max, + abs = Math.abs, + round = Math.round, + rhorizontal = /left|center|right/, + rvertical = /top|center|bottom/, + roffset = /[\+\-]\d+%?/, + rposition = /^\w+/, + rpercent = /%$/, + _position = $.fn.position; + +function getOffsets( offsets, width, height ) { + return [ + parseInt( offsets[ 0 ], 10 ) * ( rpercent.test( offsets[ 0 ] ) ? width / 100 : 1 ), + parseInt( offsets[ 1 ], 10 ) * ( rpercent.test( offsets[ 1 ] ) ? height / 100 : 1 ) + ]; +} +function parseCss( element, property ) { + return parseInt( $.css( element, property ), 10 ) || 0; +} + +$.position = { + scrollbarWidth: function() { + if ( cachedScrollbarWidth !== undefined ) { + return cachedScrollbarWidth; + } + var w1, w2, + div = $( "
    " ), + innerDiv = div.children()[0]; + + $( "body" ).append( div ); + w1 = innerDiv.offsetWidth; + div.css( "overflow", "scroll" ); + + w2 = innerDiv.offsetWidth; + + if ( w1 === w2 ) { + w2 = div[0].clientWidth; + } + + div.remove(); + + return (cachedScrollbarWidth = w1 - w2); + }, + getScrollInfo: function( within ) { + var overflowX = within.isWindow ? "" : within.element.css( "overflow-x" ), + overflowY = within.isWindow ? "" : within.element.css( "overflow-y" ), + hasOverflowX = overflowX === "scroll" || + ( overflowX === "auto" && within.width < within.element[0].scrollWidth ), + hasOverflowY = overflowY === "scroll" || + ( overflowY === "auto" && within.height < within.element[0].scrollHeight ); + return { + width: hasOverflowX ? $.position.scrollbarWidth() : 0, + height: hasOverflowY ? $.position.scrollbarWidth() : 0 + }; + }, + getWithinInfo: function( element ) { + var withinElement = $( element || window ), + isWindow = $.isWindow( withinElement[0] ); + return { + element: withinElement, + isWindow: isWindow, + offset: withinElement.offset() || { left: 0, top: 0 }, + scrollLeft: withinElement.scrollLeft(), + scrollTop: withinElement.scrollTop(), + width: isWindow ? withinElement.width() : withinElement.outerWidth(), + height: isWindow ? withinElement.height() : withinElement.outerHeight() + }; + } +}; + +$.fn.position = function( options ) { + if ( !options || !options.of ) { + return _position.apply( this, arguments ); + } + + // make a copy, we don't want to modify arguments + options = $.extend( {}, options ); + + var atOffset, targetWidth, targetHeight, targetOffset, basePosition, + target = $( options.of ), + within = $.position.getWithinInfo( options.within ), + scrollInfo = $.position.getScrollInfo( within ), + targetElem = target[0], + collision = ( options.collision || "flip" ).split( " " ), + offsets = {}; + + if ( targetElem.nodeType === 9 ) { + targetWidth = target.width(); + targetHeight = target.height(); + targetOffset = { top: 0, left: 0 }; + } else if ( $.isWindow( targetElem ) ) { + targetWidth = target.width(); + targetHeight = target.height(); + targetOffset = { top: target.scrollTop(), left: target.scrollLeft() }; + } else if ( targetElem.preventDefault ) { + // force left top to allow flipping + options.at = "left top"; + targetWidth = targetHeight = 0; + targetOffset = { top: targetElem.pageY, left: targetElem.pageX }; + } else { + targetWidth = target.outerWidth(); + targetHeight = target.outerHeight(); + targetOffset = target.offset(); + } + // clone to reuse original targetOffset later + basePosition = $.extend( {}, targetOffset ); + + // force my and at to have valid horizontal and vertical positions + // if a value is missing or invalid, it will be converted to center + $.each( [ "my", "at" ], function() { + var pos = ( options[ this ] || "" ).split( " " ), + horizontalOffset, + verticalOffset; + + if ( pos.length === 1) { + pos = rhorizontal.test( pos[ 0 ] ) ? + pos.concat( [ "center" ] ) : + rvertical.test( pos[ 0 ] ) ? + [ "center" ].concat( pos ) : + [ "center", "center" ]; + } + pos[ 0 ] = rhorizontal.test( pos[ 0 ] ) ? pos[ 0 ] : "center"; + pos[ 1 ] = rvertical.test( pos[ 1 ] ) ? pos[ 1 ] : "center"; + + // calculate offsets + horizontalOffset = roffset.exec( pos[ 0 ] ); + verticalOffset = roffset.exec( pos[ 1 ] ); + offsets[ this ] = [ + horizontalOffset ? horizontalOffset[ 0 ] : 0, + verticalOffset ? verticalOffset[ 0 ] : 0 + ]; + + // reduce to just the positions without the offsets + options[ this ] = [ + rposition.exec( pos[ 0 ] )[ 0 ], + rposition.exec( pos[ 1 ] )[ 0 ] + ]; + }); + + // normalize collision option + if ( collision.length === 1 ) { + collision[ 1 ] = collision[ 0 ]; + } + + if ( options.at[ 0 ] === "right" ) { + basePosition.left += targetWidth; + } else if ( options.at[ 0 ] === "center" ) { + basePosition.left += targetWidth / 2; + } + + if ( options.at[ 1 ] === "bottom" ) { + basePosition.top += targetHeight; + } else if ( options.at[ 1 ] === "center" ) { + basePosition.top += targetHeight / 2; + } + + atOffset = getOffsets( offsets.at, targetWidth, targetHeight ); + basePosition.left += atOffset[ 0 ]; + basePosition.top += atOffset[ 1 ]; + + return this.each(function() { + var collisionPosition, using, + elem = $( this ), + elemWidth = elem.outerWidth(), + elemHeight = elem.outerHeight(), + marginLeft = parseCss( this, "marginLeft" ), + marginTop = parseCss( this, "marginTop" ), + collisionWidth = elemWidth + marginLeft + parseCss( this, "marginRight" ) + scrollInfo.width, + collisionHeight = elemHeight + marginTop + parseCss( this, "marginBottom" ) + scrollInfo.height, + position = $.extend( {}, basePosition ), + myOffset = getOffsets( offsets.my, elem.outerWidth(), elem.outerHeight() ); + + if ( options.my[ 0 ] === "right" ) { + position.left -= elemWidth; + } else if ( options.my[ 0 ] === "center" ) { + position.left -= elemWidth / 2; + } + + if ( options.my[ 1 ] === "bottom" ) { + position.top -= elemHeight; + } else if ( options.my[ 1 ] === "center" ) { + position.top -= elemHeight / 2; + } + + position.left += myOffset[ 0 ]; + position.top += myOffset[ 1 ]; + + // if the browser doesn't support fractions, then round for consistent results + if ( !$.support.offsetFractions ) { + position.left = round( position.left ); + position.top = round( position.top ); + } + + collisionPosition = { + marginLeft: marginLeft, + marginTop: marginTop + }; + + $.each( [ "left", "top" ], function( i, dir ) { + if ( $.ui.position[ collision[ i ] ] ) { + $.ui.position[ collision[ i ] ][ dir ]( position, { + targetWidth: targetWidth, + targetHeight: targetHeight, + elemWidth: elemWidth, + elemHeight: elemHeight, + collisionPosition: collisionPosition, + collisionWidth: collisionWidth, + collisionHeight: collisionHeight, + offset: [ atOffset[ 0 ] + myOffset[ 0 ], atOffset [ 1 ] + myOffset[ 1 ] ], + my: options.my, + at: options.at, + within: within, + elem : elem + }); + } + }); + + if ( $.fn.bgiframe ) { + elem.bgiframe(); + } + + if ( options.using ) { + // adds feedback as second argument to using callback, if present + using = function( props ) { + var left = targetOffset.left - position.left, + right = left + targetWidth - elemWidth, + top = targetOffset.top - position.top, + bottom = top + targetHeight - elemHeight, + feedback = { + target: { + element: target, + left: targetOffset.left, + top: targetOffset.top, + width: targetWidth, + height: targetHeight + }, + element: { + element: elem, + left: position.left, + top: position.top, + width: elemWidth, + height: elemHeight + }, + horizontal: right < 0 ? "left" : left > 0 ? "right" : "center", + vertical: bottom < 0 ? "top" : top > 0 ? "bottom" : "middle" + }; + if ( targetWidth < elemWidth && abs( left + right ) < targetWidth ) { + feedback.horizontal = "center"; + } + if ( targetHeight < elemHeight && abs( top + bottom ) < targetHeight ) { + feedback.vertical = "middle"; + } + if ( max( abs( left ), abs( right ) ) > max( abs( top ), abs( bottom ) ) ) { + feedback.important = "horizontal"; + } else { + feedback.important = "vertical"; + } + options.using.call( this, props, feedback ); + }; + } + + elem.offset( $.extend( position, { using: using } ) ); + }); +}; + +$.ui.position = { + fit: { + left: function( position, data ) { + var within = data.within, + withinOffset = within.isWindow ? within.scrollLeft : within.offset.left, + outerWidth = within.width, + collisionPosLeft = position.left - data.collisionPosition.marginLeft, + overLeft = withinOffset - collisionPosLeft, + overRight = collisionPosLeft + data.collisionWidth - outerWidth - withinOffset, + newOverRight; + + // element is wider than within + if ( data.collisionWidth > outerWidth ) { + // element is initially over the left side of within + if ( overLeft > 0 && overRight <= 0 ) { + newOverRight = position.left + overLeft + data.collisionWidth - outerWidth - withinOffset; + position.left += overLeft - newOverRight; + // element is initially over right side of within + } else if ( overRight > 0 && overLeft <= 0 ) { + position.left = withinOffset; + // element is initially over both left and right sides of within + } else { + if ( overLeft > overRight ) { + position.left = withinOffset + outerWidth - data.collisionWidth; + } else { + position.left = withinOffset; + } + } + // too far left -> align with left edge + } else if ( overLeft > 0 ) { + position.left += overLeft; + // too far right -> align with right edge + } else if ( overRight > 0 ) { + position.left -= overRight; + // adjust based on position and margin + } else { + position.left = max( position.left - collisionPosLeft, position.left ); + } + }, + top: function( position, data ) { + var within = data.within, + withinOffset = within.isWindow ? within.scrollTop : within.offset.top, + outerHeight = data.within.height, + collisionPosTop = position.top - data.collisionPosition.marginTop, + overTop = withinOffset - collisionPosTop, + overBottom = collisionPosTop + data.collisionHeight - outerHeight - withinOffset, + newOverBottom; + + // element is taller than within + if ( data.collisionHeight > outerHeight ) { + // element is initially over the top of within + if ( overTop > 0 && overBottom <= 0 ) { + newOverBottom = position.top + overTop + data.collisionHeight - outerHeight - withinOffset; + position.top += overTop - newOverBottom; + // element is initially over bottom of within + } else if ( overBottom > 0 && overTop <= 0 ) { + position.top = withinOffset; + // element is initially over both top and bottom of within + } else { + if ( overTop > overBottom ) { + position.top = withinOffset + outerHeight - data.collisionHeight; + } else { + position.top = withinOffset; + } + } + // too far up -> align with top + } else if ( overTop > 0 ) { + position.top += overTop; + // too far down -> align with bottom edge + } else if ( overBottom > 0 ) { + position.top -= overBottom; + // adjust based on position and margin + } else { + position.top = max( position.top - collisionPosTop, position.top ); + } + } + }, + flip: { + left: function( position, data ) { + var within = data.within, + withinOffset = within.offset.left + within.scrollLeft, + outerWidth = within.width, + offsetLeft = within.isWindow ? within.scrollLeft : within.offset.left, + collisionPosLeft = position.left - data.collisionPosition.marginLeft, + overLeft = collisionPosLeft - offsetLeft, + overRight = collisionPosLeft + data.collisionWidth - outerWidth - offsetLeft, + myOffset = data.my[ 0 ] === "left" ? + -data.elemWidth : + data.my[ 0 ] === "right" ? + data.elemWidth : + 0, + atOffset = data.at[ 0 ] === "left" ? + data.targetWidth : + data.at[ 0 ] === "right" ? + -data.targetWidth : + 0, + offset = -2 * data.offset[ 0 ], + newOverRight, + newOverLeft; + + if ( overLeft < 0 ) { + newOverRight = position.left + myOffset + atOffset + offset + data.collisionWidth - outerWidth - withinOffset; + if ( newOverRight < 0 || newOverRight < abs( overLeft ) ) { + position.left += myOffset + atOffset + offset; + } + } + else if ( overRight > 0 ) { + newOverLeft = position.left - data.collisionPosition.marginLeft + myOffset + atOffset + offset - offsetLeft; + if ( newOverLeft > 0 || abs( newOverLeft ) < overRight ) { + position.left += myOffset + atOffset + offset; + } + } + }, + top: function( position, data ) { + var within = data.within, + withinOffset = within.offset.top + within.scrollTop, + outerHeight = within.height, + offsetTop = within.isWindow ? within.scrollTop : within.offset.top, + collisionPosTop = position.top - data.collisionPosition.marginTop, + overTop = collisionPosTop - offsetTop, + overBottom = collisionPosTop + data.collisionHeight - outerHeight - offsetTop, + top = data.my[ 1 ] === "top", + myOffset = top ? + -data.elemHeight : + data.my[ 1 ] === "bottom" ? + data.elemHeight : + 0, + atOffset = data.at[ 1 ] === "top" ? + data.targetHeight : + data.at[ 1 ] === "bottom" ? + -data.targetHeight : + 0, + offset = -2 * data.offset[ 1 ], + newOverTop, + newOverBottom; + if ( overTop < 0 ) { + newOverBottom = position.top + myOffset + atOffset + offset + data.collisionHeight - outerHeight - withinOffset; + if ( ( position.top + myOffset + atOffset + offset) > overTop && ( newOverBottom < 0 || newOverBottom < abs( overTop ) ) ) { + position.top += myOffset + atOffset + offset; + } + } + else if ( overBottom > 0 ) { + newOverTop = position.top - data.collisionPosition.marginTop + myOffset + atOffset + offset - offsetTop; + if ( ( position.top + myOffset + atOffset + offset) > overBottom && ( newOverTop > 0 || abs( newOverTop ) < overBottom ) ) { + position.top += myOffset + atOffset + offset; + } + } + } + }, + flipfit: { + left: function() { + $.ui.position.flip.left.apply( this, arguments ); + $.ui.position.fit.left.apply( this, arguments ); + }, + top: function() { + $.ui.position.flip.top.apply( this, arguments ); + $.ui.position.fit.top.apply( this, arguments ); + } + } +}; + +// fraction support test +(function () { + var testElement, testElementParent, testElementStyle, offsetLeft, i, + body = document.getElementsByTagName( "body" )[ 0 ], + div = document.createElement( "div" ); + + //Create a "fake body" for testing based on method used in jQuery.support + testElement = document.createElement( body ? "div" : "body" ); + testElementStyle = { + visibility: "hidden", + width: 0, + height: 0, + border: 0, + margin: 0, + background: "none" + }; + if ( body ) { + $.extend( testElementStyle, { + position: "absolute", + left: "-1000px", + top: "-1000px" + }); + } + for ( i in testElementStyle ) { + testElement.style[ i ] = testElementStyle[ i ]; + } + testElement.appendChild( div ); + testElementParent = body || document.documentElement; + testElementParent.insertBefore( testElement, testElementParent.firstChild ); + + div.style.cssText = "position: absolute; left: 10.7432222px;"; + + offsetLeft = $( div ).offset().left; + $.support.offsetFractions = offsetLeft > 10 && offsetLeft < 11; + + testElement.innerHTML = ""; + testElementParent.removeChild( testElement ); +})(); + +// DEPRECATED +if ( $.uiBackCompat !== false ) { + // offset option + (function( $ ) { + var _position = $.fn.position; + $.fn.position = function( options ) { + if ( !options || !options.offset ) { + return _position.call( this, options ); + } + var offset = options.offset.split( " " ), + at = options.at.split( " " ); + if ( offset.length === 1 ) { + offset[ 1 ] = offset[ 0 ]; + } + if ( /^\d/.test( offset[ 0 ] ) ) { + offset[ 0 ] = "+" + offset[ 0 ]; + } + if ( /^\d/.test( offset[ 1 ] ) ) { + offset[ 1 ] = "+" + offset[ 1 ]; + } + if ( at.length === 1 ) { + if ( /left|center|right/.test( at[ 0 ] ) ) { + at[ 1 ] = "center"; + } else { + at[ 1 ] = at[ 0 ]; + at[ 0 ] = "center"; + } + } + return _position.call( this, $.extend( options, { + at: at[ 0 ] + offset[ 0 ] + " " + at[ 1 ] + offset[ 1 ], + offset: undefined + } ) ); + }; + }( jQuery ) ); +} + +}( jQuery ) ); + +(function( $, undefined ) { + +$.widget( "ui.progressbar", { + version: "1.9.2", + options: { + value: 0, + max: 100 + }, + + min: 0, + + _create: function() { + this.element + .addClass( "ui-progressbar ui-widget ui-widget-content ui-corner-all" ) + .attr({ + role: "progressbar", + "aria-valuemin": this.min, + "aria-valuemax": this.options.max, + "aria-valuenow": this._value() + }); + + this.valueDiv = $( "
    " ) + .appendTo( this.element ); + + this.oldValue = this._value(); + this._refreshValue(); + }, + + _destroy: function() { + this.element + .removeClass( "ui-progressbar ui-widget ui-widget-content ui-corner-all" ) + .removeAttr( "role" ) + .removeAttr( "aria-valuemin" ) + .removeAttr( "aria-valuemax" ) + .removeAttr( "aria-valuenow" ); + + this.valueDiv.remove(); + }, + + value: function( newValue ) { + if ( newValue === undefined ) { + return this._value(); + } + + this._setOption( "value", newValue ); + return this; + }, + + _setOption: function( key, value ) { + if ( key === "value" ) { + this.options.value = value; + this._refreshValue(); + if ( this._value() === this.options.max ) { + this._trigger( "complete" ); + } + } + + this._super( key, value ); + }, + + _value: function() { + var val = this.options.value; + // normalize invalid value + if ( typeof val !== "number" ) { + val = 0; + } + return Math.min( this.options.max, Math.max( this.min, val ) ); + }, + + _percentage: function() { + return 100 * this._value() / this.options.max; + }, + + _refreshValue: function() { + var value = this.value(), + percentage = this._percentage(); + + if ( this.oldValue !== value ) { + this.oldValue = value; + this._trigger( "change" ); + } + + this.valueDiv + .toggle( value > this.min ) + .toggleClass( "ui-corner-right", value === this.options.max ) + .width( percentage.toFixed(0) + "%" ); + this.element.attr( "aria-valuenow", value ); + } +}); + +})( jQuery ); + +(function( $, undefined ) { + +// number of pages in a slider +// (how many times can you page up/down to go through the whole range) +var numPages = 5; + +$.widget( "ui.slider", $.ui.mouse, { + version: "1.9.2", + widgetEventPrefix: "slide", + + options: { + animate: false, + distance: 0, + max: 100, + min: 0, + orientation: "horizontal", + range: false, + step: 1, + value: 0, + values: null + }, + + _create: function() { + var i, handleCount, + o = this.options, + existingHandles = this.element.find( ".ui-slider-handle" ).addClass( "ui-state-default ui-corner-all" ), + handle = "", + handles = []; + + this._keySliding = false; + this._mouseSliding = false; + this._animateOff = true; + this._handleIndex = null; + this._detectOrientation(); + this._mouseInit(); + + this.element + .addClass( "ui-slider" + + " ui-slider-" + this.orientation + + " ui-widget" + + " ui-widget-content" + + " ui-corner-all" + + ( o.disabled ? " ui-slider-disabled ui-disabled" : "" ) ); + + this.range = $([]); + + if ( o.range ) { + if ( o.range === true ) { + if ( !o.values ) { + o.values = [ this._valueMin(), this._valueMin() ]; + } + if ( o.values.length && o.values.length !== 2 ) { + o.values = [ o.values[0], o.values[0] ]; + } + } + + this.range = $( "
    " ) + .appendTo( this.element ) + .addClass( "ui-slider-range" + + // note: this isn't the most fittingly semantic framework class for this element, + // but worked best visually with a variety of themes + " ui-widget-header" + + ( ( o.range === "min" || o.range === "max" ) ? " ui-slider-range-" + o.range : "" ) ); + } + + handleCount = ( o.values && o.values.length ) || 1; + + for ( i = existingHandles.length; i < handleCount; i++ ) { + handles.push( handle ); + } + + this.handles = existingHandles.add( $( handles.join( "" ) ).appendTo( this.element ) ); + + this.handle = this.handles.eq( 0 ); + + this.handles.add( this.range ).filter( "a" ) + .click(function( event ) { + event.preventDefault(); + }) + .mouseenter(function() { + if ( !o.disabled ) { + $( this ).addClass( "ui-state-hover" ); + } + }) + .mouseleave(function() { + $( this ).removeClass( "ui-state-hover" ); + }) + .focus(function() { + if ( !o.disabled ) { + $( ".ui-slider .ui-state-focus" ).removeClass( "ui-state-focus" ); + $( this ).addClass( "ui-state-focus" ); + } else { + $( this ).blur(); + } + }) + .blur(function() { + $( this ).removeClass( "ui-state-focus" ); + }); + + this.handles.each(function( i ) { + $( this ).data( "ui-slider-handle-index", i ); + }); + + this._on( this.handles, { + keydown: function( event ) { + var allowed, curVal, newVal, step, + index = $( event.target ).data( "ui-slider-handle-index" ); + + switch ( event.keyCode ) { + case $.ui.keyCode.HOME: + case $.ui.keyCode.END: + case $.ui.keyCode.PAGE_UP: + case $.ui.keyCode.PAGE_DOWN: + case $.ui.keyCode.UP: + case $.ui.keyCode.RIGHT: + case $.ui.keyCode.DOWN: + case $.ui.keyCode.LEFT: + event.preventDefault(); + if ( !this._keySliding ) { + this._keySliding = true; + $( event.target ).addClass( "ui-state-active" ); + allowed = this._start( event, index ); + if ( allowed === false ) { + return; + } + } + break; + } + + step = this.options.step; + if ( this.options.values && this.options.values.length ) { + curVal = newVal = this.values( index ); + } else { + curVal = newVal = this.value(); + } + + switch ( event.keyCode ) { + case $.ui.keyCode.HOME: + newVal = this._valueMin(); + break; + case $.ui.keyCode.END: + newVal = this._valueMax(); + break; + case $.ui.keyCode.PAGE_UP: + newVal = this._trimAlignValue( curVal + ( (this._valueMax() - this._valueMin()) / numPages ) ); + break; + case $.ui.keyCode.PAGE_DOWN: + newVal = this._trimAlignValue( curVal - ( (this._valueMax() - this._valueMin()) / numPages ) ); + break; + case $.ui.keyCode.UP: + case $.ui.keyCode.RIGHT: + if ( curVal === this._valueMax() ) { + return; + } + newVal = this._trimAlignValue( curVal + step ); + break; + case $.ui.keyCode.DOWN: + case $.ui.keyCode.LEFT: + if ( curVal === this._valueMin() ) { + return; + } + newVal = this._trimAlignValue( curVal - step ); + break; + } + + this._slide( event, index, newVal ); + }, + keyup: function( event ) { + var index = $( event.target ).data( "ui-slider-handle-index" ); + + if ( this._keySliding ) { + this._keySliding = false; + this._stop( event, index ); + this._change( event, index ); + $( event.target ).removeClass( "ui-state-active" ); + } + } + }); + + this._refreshValue(); + + this._animateOff = false; + }, + + _destroy: function() { + this.handles.remove(); + this.range.remove(); + + this.element + .removeClass( "ui-slider" + + " ui-slider-horizontal" + + " ui-slider-vertical" + + " ui-slider-disabled" + + " ui-widget" + + " ui-widget-content" + + " ui-corner-all" ); + + this._mouseDestroy(); + }, + + _mouseCapture: function( event ) { + var position, normValue, distance, closestHandle, index, allowed, offset, mouseOverHandle, + that = this, + o = this.options; + + if ( o.disabled ) { + return false; + } + + this.elementSize = { + width: this.element.outerWidth(), + height: this.element.outerHeight() + }; + this.elementOffset = this.element.offset(); + + position = { x: event.pageX, y: event.pageY }; + normValue = this._normValueFromMouse( position ); + distance = this._valueMax() - this._valueMin() + 1; + this.handles.each(function( i ) { + var thisDistance = Math.abs( normValue - that.values(i) ); + if ( distance > thisDistance ) { + distance = thisDistance; + closestHandle = $( this ); + index = i; + } + }); + + // workaround for bug #3736 (if both handles of a range are at 0, + // the first is always used as the one with least distance, + // and moving it is obviously prevented by preventing negative ranges) + if( o.range === true && this.values(1) === o.min ) { + index += 1; + closestHandle = $( this.handles[index] ); + } + + allowed = this._start( event, index ); + if ( allowed === false ) { + return false; + } + this._mouseSliding = true; + + this._handleIndex = index; + + closestHandle + .addClass( "ui-state-active" ) + .focus(); + + offset = closestHandle.offset(); + mouseOverHandle = !$( event.target ).parents().andSelf().is( ".ui-slider-handle" ); + this._clickOffset = mouseOverHandle ? { left: 0, top: 0 } : { + left: event.pageX - offset.left - ( closestHandle.width() / 2 ), + top: event.pageY - offset.top - + ( closestHandle.height() / 2 ) - + ( parseInt( closestHandle.css("borderTopWidth"), 10 ) || 0 ) - + ( parseInt( closestHandle.css("borderBottomWidth"), 10 ) || 0) + + ( parseInt( closestHandle.css("marginTop"), 10 ) || 0) + }; + + if ( !this.handles.hasClass( "ui-state-hover" ) ) { + this._slide( event, index, normValue ); + } + this._animateOff = true; + return true; + }, + + _mouseStart: function() { + return true; + }, + + _mouseDrag: function( event ) { + var position = { x: event.pageX, y: event.pageY }, + normValue = this._normValueFromMouse( position ); + + this._slide( event, this._handleIndex, normValue ); + + return false; + }, + + _mouseStop: function( event ) { + this.handles.removeClass( "ui-state-active" ); + this._mouseSliding = false; + + this._stop( event, this._handleIndex ); + this._change( event, this._handleIndex ); + + this._handleIndex = null; + this._clickOffset = null; + this._animateOff = false; + + return false; + }, + + _detectOrientation: function() { + this.orientation = ( this.options.orientation === "vertical" ) ? "vertical" : "horizontal"; + }, + + _normValueFromMouse: function( position ) { + var pixelTotal, + pixelMouse, + percentMouse, + valueTotal, + valueMouse; + + if ( this.orientation === "horizontal" ) { + pixelTotal = this.elementSize.width; + pixelMouse = position.x - this.elementOffset.left - ( this._clickOffset ? this._clickOffset.left : 0 ); + } else { + pixelTotal = this.elementSize.height; + pixelMouse = position.y - this.elementOffset.top - ( this._clickOffset ? this._clickOffset.top : 0 ); + } + + percentMouse = ( pixelMouse / pixelTotal ); + if ( percentMouse > 1 ) { + percentMouse = 1; + } + if ( percentMouse < 0 ) { + percentMouse = 0; + } + if ( this.orientation === "vertical" ) { + percentMouse = 1 - percentMouse; + } + + valueTotal = this._valueMax() - this._valueMin(); + valueMouse = this._valueMin() + percentMouse * valueTotal; + + return this._trimAlignValue( valueMouse ); + }, + + _start: function( event, index ) { + var uiHash = { + handle: this.handles[ index ], + value: this.value() + }; + if ( this.options.values && this.options.values.length ) { + uiHash.value = this.values( index ); + uiHash.values = this.values(); + } + return this._trigger( "start", event, uiHash ); + }, + + _slide: function( event, index, newVal ) { + var otherVal, + newValues, + allowed; + + if ( this.options.values && this.options.values.length ) { + otherVal = this.values( index ? 0 : 1 ); + + if ( ( this.options.values.length === 2 && this.options.range === true ) && + ( ( index === 0 && newVal > otherVal) || ( index === 1 && newVal < otherVal ) ) + ) { + newVal = otherVal; + } + + if ( newVal !== this.values( index ) ) { + newValues = this.values(); + newValues[ index ] = newVal; + // A slide can be canceled by returning false from the slide callback + allowed = this._trigger( "slide", event, { + handle: this.handles[ index ], + value: newVal, + values: newValues + } ); + otherVal = this.values( index ? 0 : 1 ); + if ( allowed !== false ) { + this.values( index, newVal, true ); + } + } + } else { + if ( newVal !== this.value() ) { + // A slide can be canceled by returning false from the slide callback + allowed = this._trigger( "slide", event, { + handle: this.handles[ index ], + value: newVal + } ); + if ( allowed !== false ) { + this.value( newVal ); + } + } + } + }, + + _stop: function( event, index ) { + var uiHash = { + handle: this.handles[ index ], + value: this.value() + }; + if ( this.options.values && this.options.values.length ) { + uiHash.value = this.values( index ); + uiHash.values = this.values(); + } + + this._trigger( "stop", event, uiHash ); + }, + + _change: function( event, index ) { + if ( !this._keySliding && !this._mouseSliding ) { + var uiHash = { + handle: this.handles[ index ], + value: this.value() + }; + if ( this.options.values && this.options.values.length ) { + uiHash.value = this.values( index ); + uiHash.values = this.values(); + } + + this._trigger( "change", event, uiHash ); + } + }, + + value: function( newValue ) { + if ( arguments.length ) { + this.options.value = this._trimAlignValue( newValue ); + this._refreshValue(); + this._change( null, 0 ); + return; + } + + return this._value(); + }, + + values: function( index, newValue ) { + var vals, + newValues, + i; + + if ( arguments.length > 1 ) { + this.options.values[ index ] = this._trimAlignValue( newValue ); + this._refreshValue(); + this._change( null, index ); + return; + } + + if ( arguments.length ) { + if ( $.isArray( arguments[ 0 ] ) ) { + vals = this.options.values; + newValues = arguments[ 0 ]; + for ( i = 0; i < vals.length; i += 1 ) { + vals[ i ] = this._trimAlignValue( newValues[ i ] ); + this._change( null, i ); + } + this._refreshValue(); + } else { + if ( this.options.values && this.options.values.length ) { + return this._values( index ); + } else { + return this.value(); + } + } + } else { + return this._values(); + } + }, + + _setOption: function( key, value ) { + var i, + valsLength = 0; + + if ( $.isArray( this.options.values ) ) { + valsLength = this.options.values.length; + } + + $.Widget.prototype._setOption.apply( this, arguments ); + + switch ( key ) { + case "disabled": + if ( value ) { + this.handles.filter( ".ui-state-focus" ).blur(); + this.handles.removeClass( "ui-state-hover" ); + this.handles.prop( "disabled", true ); + this.element.addClass( "ui-disabled" ); + } else { + this.handles.prop( "disabled", false ); + this.element.removeClass( "ui-disabled" ); + } + break; + case "orientation": + this._detectOrientation(); + this.element + .removeClass( "ui-slider-horizontal ui-slider-vertical" ) + .addClass( "ui-slider-" + this.orientation ); + this._refreshValue(); + break; + case "value": + this._animateOff = true; + this._refreshValue(); + this._change( null, 0 ); + this._animateOff = false; + break; + case "values": + this._animateOff = true; + this._refreshValue(); + for ( i = 0; i < valsLength; i += 1 ) { + this._change( null, i ); + } + this._animateOff = false; + break; + case "min": + case "max": + this._animateOff = true; + this._refreshValue(); + this._animateOff = false; + break; + } + }, + + //internal value getter + // _value() returns value trimmed by min and max, aligned by step + _value: function() { + var val = this.options.value; + val = this._trimAlignValue( val ); + + return val; + }, + + //internal values getter + // _values() returns array of values trimmed by min and max, aligned by step + // _values( index ) returns single value trimmed by min and max, aligned by step + _values: function( index ) { + var val, + vals, + i; + + if ( arguments.length ) { + val = this.options.values[ index ]; + val = this._trimAlignValue( val ); + + return val; + } else { + // .slice() creates a copy of the array + // this copy gets trimmed by min and max and then returned + vals = this.options.values.slice(); + for ( i = 0; i < vals.length; i+= 1) { + vals[ i ] = this._trimAlignValue( vals[ i ] ); + } + + return vals; + } + }, + + // returns the step-aligned value that val is closest to, between (inclusive) min and max + _trimAlignValue: function( val ) { + if ( val <= this._valueMin() ) { + return this._valueMin(); + } + if ( val >= this._valueMax() ) { + return this._valueMax(); + } + var step = ( this.options.step > 0 ) ? this.options.step : 1, + valModStep = (val - this._valueMin()) % step, + alignValue = val - valModStep; + + if ( Math.abs(valModStep) * 2 >= step ) { + alignValue += ( valModStep > 0 ) ? step : ( -step ); + } + + // Since JavaScript has problems with large floats, round + // the final value to 5 digits after the decimal point (see #4124) + return parseFloat( alignValue.toFixed(5) ); + }, + + _valueMin: function() { + return this.options.min; + }, + + _valueMax: function() { + return this.options.max; + }, + + _refreshValue: function() { + var lastValPercent, valPercent, value, valueMin, valueMax, + oRange = this.options.range, + o = this.options, + that = this, + animate = ( !this._animateOff ) ? o.animate : false, + _set = {}; + + if ( this.options.values && this.options.values.length ) { + this.handles.each(function( i ) { + valPercent = ( that.values(i) - that._valueMin() ) / ( that._valueMax() - that._valueMin() ) * 100; + _set[ that.orientation === "horizontal" ? "left" : "bottom" ] = valPercent + "%"; + $( this ).stop( 1, 1 )[ animate ? "animate" : "css" ]( _set, o.animate ); + if ( that.options.range === true ) { + if ( that.orientation === "horizontal" ) { + if ( i === 0 ) { + that.range.stop( 1, 1 )[ animate ? "animate" : "css" ]( { left: valPercent + "%" }, o.animate ); + } + if ( i === 1 ) { + that.range[ animate ? "animate" : "css" ]( { width: ( valPercent - lastValPercent ) + "%" }, { queue: false, duration: o.animate } ); + } + } else { + if ( i === 0 ) { + that.range.stop( 1, 1 )[ animate ? "animate" : "css" ]( { bottom: ( valPercent ) + "%" }, o.animate ); + } + if ( i === 1 ) { + that.range[ animate ? "animate" : "css" ]( { height: ( valPercent - lastValPercent ) + "%" }, { queue: false, duration: o.animate } ); + } + } + } + lastValPercent = valPercent; + }); + } else { + value = this.value(); + valueMin = this._valueMin(); + valueMax = this._valueMax(); + valPercent = ( valueMax !== valueMin ) ? + ( value - valueMin ) / ( valueMax - valueMin ) * 100 : + 0; + _set[ this.orientation === "horizontal" ? "left" : "bottom" ] = valPercent + "%"; + this.handle.stop( 1, 1 )[ animate ? "animate" : "css" ]( _set, o.animate ); + + if ( oRange === "min" && this.orientation === "horizontal" ) { + this.range.stop( 1, 1 )[ animate ? "animate" : "css" ]( { width: valPercent + "%" }, o.animate ); + } + if ( oRange === "max" && this.orientation === "horizontal" ) { + this.range[ animate ? "animate" : "css" ]( { width: ( 100 - valPercent ) + "%" }, { queue: false, duration: o.animate } ); + } + if ( oRange === "min" && this.orientation === "vertical" ) { + this.range.stop( 1, 1 )[ animate ? "animate" : "css" ]( { height: valPercent + "%" }, o.animate ); + } + if ( oRange === "max" && this.orientation === "vertical" ) { + this.range[ animate ? "animate" : "css" ]( { height: ( 100 - valPercent ) + "%" }, { queue: false, duration: o.animate } ); + } + } + } + +}); + +}(jQuery)); + +(function( $ ) { + +function modifier( fn ) { + return function() { + var previous = this.element.val(); + fn.apply( this, arguments ); + this._refresh(); + if ( previous !== this.element.val() ) { + this._trigger( "change" ); + } + }; +} + +$.widget( "ui.spinner", { + version: "1.9.2", + defaultElement: "", + widgetEventPrefix: "spin", + options: { + culture: null, + icons: { + down: "ui-icon-triangle-1-s", + up: "ui-icon-triangle-1-n" + }, + incremental: true, + max: null, + min: null, + numberFormat: null, + page: 10, + step: 1, + + change: null, + spin: null, + start: null, + stop: null + }, + + _create: function() { + // handle string values that need to be parsed + this._setOption( "max", this.options.max ); + this._setOption( "min", this.options.min ); + this._setOption( "step", this.options.step ); + + // format the value, but don't constrain + this._value( this.element.val(), true ); + + this._draw(); + this._on( this._events ); + this._refresh(); + + // turning off autocomplete prevents the browser from remembering the + // value when navigating through history, so we re-enable autocomplete + // if the page is unloaded before the widget is destroyed. #7790 + this._on( this.window, { + beforeunload: function() { + this.element.removeAttr( "autocomplete" ); + } + }); + }, + + _getCreateOptions: function() { + var options = {}, + element = this.element; + + $.each( [ "min", "max", "step" ], function( i, option ) { + var value = element.attr( option ); + if ( value !== undefined && value.length ) { + options[ option ] = value; + } + }); + + return options; + }, + + _events: { + keydown: function( event ) { + if ( this._start( event ) && this._keydown( event ) ) { + event.preventDefault(); + } + }, + keyup: "_stop", + focus: function() { + this.previous = this.element.val(); + }, + blur: function( event ) { + if ( this.cancelBlur ) { + delete this.cancelBlur; + return; + } + + this._refresh(); + if ( this.previous !== this.element.val() ) { + this._trigger( "change", event ); + } + }, + mousewheel: function( event, delta ) { + if ( !delta ) { + return; + } + if ( !this.spinning && !this._start( event ) ) { + return false; + } + + this._spin( (delta > 0 ? 1 : -1) * this.options.step, event ); + clearTimeout( this.mousewheelTimer ); + this.mousewheelTimer = this._delay(function() { + if ( this.spinning ) { + this._stop( event ); + } + }, 100 ); + event.preventDefault(); + }, + "mousedown .ui-spinner-button": function( event ) { + var previous; + + // We never want the buttons to have focus; whenever the user is + // interacting with the spinner, the focus should be on the input. + // If the input is focused then this.previous is properly set from + // when the input first received focus. If the input is not focused + // then we need to set this.previous based on the value before spinning. + previous = this.element[0] === this.document[0].activeElement ? + this.previous : this.element.val(); + function checkFocus() { + var isActive = this.element[0] === this.document[0].activeElement; + if ( !isActive ) { + this.element.focus(); + this.previous = previous; + // support: IE + // IE sets focus asynchronously, so we need to check if focus + // moved off of the input because the user clicked on the button. + this._delay(function() { + this.previous = previous; + }); + } + } + + // ensure focus is on (or stays on) the text field + event.preventDefault(); + checkFocus.call( this ); + + // support: IE + // IE doesn't prevent moving focus even with event.preventDefault() + // so we set a flag to know when we should ignore the blur event + // and check (again) if focus moved off of the input. + this.cancelBlur = true; + this._delay(function() { + delete this.cancelBlur; + checkFocus.call( this ); + }); + + if ( this._start( event ) === false ) { + return; + } + + this._repeat( null, $( event.currentTarget ).hasClass( "ui-spinner-up" ) ? 1 : -1, event ); + }, + "mouseup .ui-spinner-button": "_stop", + "mouseenter .ui-spinner-button": function( event ) { + // button will add ui-state-active if mouse was down while mouseleave and kept down + if ( !$( event.currentTarget ).hasClass( "ui-state-active" ) ) { + return; + } + + if ( this._start( event ) === false ) { + return false; + } + this._repeat( null, $( event.currentTarget ).hasClass( "ui-spinner-up" ) ? 1 : -1, event ); + }, + // TODO: do we really want to consider this a stop? + // shouldn't we just stop the repeater and wait until mouseup before + // we trigger the stop event? + "mouseleave .ui-spinner-button": "_stop" + }, + + _draw: function() { + var uiSpinner = this.uiSpinner = this.element + .addClass( "ui-spinner-input" ) + .attr( "autocomplete", "off" ) + .wrap( this._uiSpinnerHtml() ) + .parent() + // add buttons + .append( this._buttonHtml() ); + + this.element.attr( "role", "spinbutton" ); + + // button bindings + this.buttons = uiSpinner.find( ".ui-spinner-button" ) + .attr( "tabIndex", -1 ) + .button() + .removeClass( "ui-corner-all" ); + + // IE 6 doesn't understand height: 50% for the buttons + // unless the wrapper has an explicit height + if ( this.buttons.height() > Math.ceil( uiSpinner.height() * 0.5 ) && + uiSpinner.height() > 0 ) { + uiSpinner.height( uiSpinner.height() ); + } + + // disable spinner if element was already disabled + if ( this.options.disabled ) { + this.disable(); + } + }, + + _keydown: function( event ) { + var options = this.options, + keyCode = $.ui.keyCode; + + switch ( event.keyCode ) { + case keyCode.UP: + this._repeat( null, 1, event ); + return true; + case keyCode.DOWN: + this._repeat( null, -1, event ); + return true; + case keyCode.PAGE_UP: + this._repeat( null, options.page, event ); + return true; + case keyCode.PAGE_DOWN: + this._repeat( null, -options.page, event ); + return true; + } + + return false; + }, + + _uiSpinnerHtml: function() { + return ""; + }, + + _buttonHtml: function() { + return "" + + "" + + "" + + "" + + "" + + "" + + ""; + }, + + _start: function( event ) { + if ( !this.spinning && this._trigger( "start", event ) === false ) { + return false; + } + + if ( !this.counter ) { + this.counter = 1; + } + this.spinning = true; + return true; + }, + + _repeat: function( i, steps, event ) { + i = i || 500; + + clearTimeout( this.timer ); + this.timer = this._delay(function() { + this._repeat( 40, steps, event ); + }, i ); + + this._spin( steps * this.options.step, event ); + }, + + _spin: function( step, event ) { + var value = this.value() || 0; + + if ( !this.counter ) { + this.counter = 1; + } + + value = this._adjustValue( value + step * this._increment( this.counter ) ); + + if ( !this.spinning || this._trigger( "spin", event, { value: value } ) !== false) { + this._value( value ); + this.counter++; + } + }, + + _increment: function( i ) { + var incremental = this.options.incremental; + + if ( incremental ) { + return $.isFunction( incremental ) ? + incremental( i ) : + Math.floor( i*i*i/50000 - i*i/500 + 17*i/200 + 1 ); + } + + return 1; + }, + + _precision: function() { + var precision = this._precisionOf( this.options.step ); + if ( this.options.min !== null ) { + precision = Math.max( precision, this._precisionOf( this.options.min ) ); + } + return precision; + }, + + _precisionOf: function( num ) { + var str = num.toString(), + decimal = str.indexOf( "." ); + return decimal === -1 ? 0 : str.length - decimal - 1; + }, + + _adjustValue: function( value ) { + var base, aboveMin, + options = this.options; + + // make sure we're at a valid step + // - find out where we are relative to the base (min or 0) + base = options.min !== null ? options.min : 0; + aboveMin = value - base; + // - round to the nearest step + aboveMin = Math.round(aboveMin / options.step) * options.step; + // - rounding is based on 0, so adjust back to our base + value = base + aboveMin; + + // fix precision from bad JS floating point math + value = parseFloat( value.toFixed( this._precision() ) ); + + // clamp the value + if ( options.max !== null && value > options.max) { + return options.max; + } + if ( options.min !== null && value < options.min ) { + return options.min; + } + + return value; + }, + + _stop: function( event ) { + if ( !this.spinning ) { + return; + } + + clearTimeout( this.timer ); + clearTimeout( this.mousewheelTimer ); + this.counter = 0; + this.spinning = false; + this._trigger( "stop", event ); + }, + + _setOption: function( key, value ) { + if ( key === "culture" || key === "numberFormat" ) { + var prevValue = this._parse( this.element.val() ); + this.options[ key ] = value; + this.element.val( this._format( prevValue ) ); + return; + } + + if ( key === "max" || key === "min" || key === "step" ) { + if ( typeof value === "string" ) { + value = this._parse( value ); + } + } + + this._super( key, value ); + + if ( key === "disabled" ) { + if ( value ) { + this.element.prop( "disabled", true ); + this.buttons.button( "disable" ); + } else { + this.element.prop( "disabled", false ); + this.buttons.button( "enable" ); + } + } + }, + + _setOptions: modifier(function( options ) { + this._super( options ); + this._value( this.element.val() ); + }), + + _parse: function( val ) { + if ( typeof val === "string" && val !== "" ) { + val = window.Globalize && this.options.numberFormat ? + Globalize.parseFloat( val, 10, this.options.culture ) : +val; + } + return val === "" || isNaN( val ) ? null : val; + }, + + _format: function( value ) { + if ( value === "" ) { + return ""; + } + return window.Globalize && this.options.numberFormat ? + Globalize.format( value, this.options.numberFormat, this.options.culture ) : + value; + }, + + _refresh: function() { + this.element.attr({ + "aria-valuemin": this.options.min, + "aria-valuemax": this.options.max, + // TODO: what should we do with values that can't be parsed? + "aria-valuenow": this._parse( this.element.val() ) + }); + }, + + // update the value without triggering change + _value: function( value, allowAny ) { + var parsed; + if ( value !== "" ) { + parsed = this._parse( value ); + if ( parsed !== null ) { + if ( !allowAny ) { + parsed = this._adjustValue( parsed ); + } + value = this._format( parsed ); + } + } + this.element.val( value ); + this._refresh(); + }, + + _destroy: function() { + this.element + .removeClass( "ui-spinner-input" ) + .prop( "disabled", false ) + .removeAttr( "autocomplete" ) + .removeAttr( "role" ) + .removeAttr( "aria-valuemin" ) + .removeAttr( "aria-valuemax" ) + .removeAttr( "aria-valuenow" ); + this.uiSpinner.replaceWith( this.element ); + }, + + stepUp: modifier(function( steps ) { + this._stepUp( steps ); + }), + _stepUp: function( steps ) { + this._spin( (steps || 1) * this.options.step ); + }, + + stepDown: modifier(function( steps ) { + this._stepDown( steps ); + }), + _stepDown: function( steps ) { + this._spin( (steps || 1) * -this.options.step ); + }, + + pageUp: modifier(function( pages ) { + this._stepUp( (pages || 1) * this.options.page ); + }), + + pageDown: modifier(function( pages ) { + this._stepDown( (pages || 1) * this.options.page ); + }), + + value: function( newVal ) { + if ( !arguments.length ) { + return this._parse( this.element.val() ); + } + modifier( this._value ).call( this, newVal ); + }, + + widget: function() { + return this.uiSpinner; + } +}); + +}( jQuery ) ); + +(function( $, undefined ) { + +var tabId = 0, + rhash = /#.*$/; + +function getNextTabId() { + return ++tabId; +} + +function isLocal( anchor ) { + return anchor.hash.length > 1 && + anchor.href.replace( rhash, "" ) === + location.href.replace( rhash, "" ) + // support: Safari 5.1 + // Safari 5.1 doesn't encode spaces in window.location + // but it does encode spaces from anchors (#8777) + .replace( /\s/g, "%20" ); +} + +$.widget( "ui.tabs", { + version: "1.9.2", + delay: 300, + options: { + active: null, + collapsible: false, + event: "click", + heightStyle: "content", + hide: null, + show: null, + + // callbacks + activate: null, + beforeActivate: null, + beforeLoad: null, + load: null + }, + + _create: function() { + var that = this, + options = this.options, + active = options.active, + locationHash = location.hash.substring( 1 ); + + this.running = false; + + this.element + .addClass( "ui-tabs ui-widget ui-widget-content ui-corner-all" ) + .toggleClass( "ui-tabs-collapsible", options.collapsible ) + // Prevent users from focusing disabled tabs via click + .delegate( ".ui-tabs-nav > li", "mousedown" + this.eventNamespace, function( event ) { + if ( $( this ).is( ".ui-state-disabled" ) ) { + event.preventDefault(); + } + }) + // support: IE <9 + // Preventing the default action in mousedown doesn't prevent IE + // from focusing the element, so if the anchor gets focused, blur. + // We don't have to worry about focusing the previously focused + // element since clicking on a non-focusable element should focus + // the body anyway. + .delegate( ".ui-tabs-anchor", "focus" + this.eventNamespace, function() { + if ( $( this ).closest( "li" ).is( ".ui-state-disabled" ) ) { + this.blur(); + } + }); + + this._processTabs(); + + if ( active === null ) { + // check the fragment identifier in the URL + if ( locationHash ) { + this.tabs.each(function( i, tab ) { + if ( $( tab ).attr( "aria-controls" ) === locationHash ) { + active = i; + return false; + } + }); + } + + // check for a tab marked active via a class + if ( active === null ) { + active = this.tabs.index( this.tabs.filter( ".ui-tabs-active" ) ); + } + + // no active tab, set to false + if ( active === null || active === -1 ) { + active = this.tabs.length ? 0 : false; + } + } + + // handle numbers: negative, out of range + if ( active !== false ) { + active = this.tabs.index( this.tabs.eq( active ) ); + if ( active === -1 ) { + active = options.collapsible ? false : 0; + } + } + options.active = active; + + // don't allow collapsible: false and active: false + if ( !options.collapsible && options.active === false && this.anchors.length ) { + options.active = 0; + } + + // Take disabling tabs via class attribute from HTML + // into account and update option properly. + if ( $.isArray( options.disabled ) ) { + options.disabled = $.unique( options.disabled.concat( + $.map( this.tabs.filter( ".ui-state-disabled" ), function( li ) { + return that.tabs.index( li ); + }) + ) ).sort(); + } + + // check for length avoids error when initializing empty list + if ( this.options.active !== false && this.anchors.length ) { + this.active = this._findActive( this.options.active ); + } else { + this.active = $(); + } + + this._refresh(); + + if ( this.active.length ) { + this.load( options.active ); + } + }, + + _getCreateEventData: function() { + return { + tab: this.active, + panel: !this.active.length ? $() : this._getPanelForTab( this.active ) + }; + }, + + _tabKeydown: function( event ) { + var focusedTab = $( this.document[0].activeElement ).closest( "li" ), + selectedIndex = this.tabs.index( focusedTab ), + goingForward = true; + + if ( this._handlePageNav( event ) ) { + return; + } + + switch ( event.keyCode ) { + case $.ui.keyCode.RIGHT: + case $.ui.keyCode.DOWN: + selectedIndex++; + break; + case $.ui.keyCode.UP: + case $.ui.keyCode.LEFT: + goingForward = false; + selectedIndex--; + break; + case $.ui.keyCode.END: + selectedIndex = this.anchors.length - 1; + break; + case $.ui.keyCode.HOME: + selectedIndex = 0; + break; + case $.ui.keyCode.SPACE: + // Activate only, no collapsing + event.preventDefault(); + clearTimeout( this.activating ); + this._activate( selectedIndex ); + return; + case $.ui.keyCode.ENTER: + // Toggle (cancel delayed activation, allow collapsing) + event.preventDefault(); + clearTimeout( this.activating ); + // Determine if we should collapse or activate + this._activate( selectedIndex === this.options.active ? false : selectedIndex ); + return; + default: + return; + } + + // Focus the appropriate tab, based on which key was pressed + event.preventDefault(); + clearTimeout( this.activating ); + selectedIndex = this._focusNextTab( selectedIndex, goingForward ); + + // Navigating with control key will prevent automatic activation + if ( !event.ctrlKey ) { + // Update aria-selected immediately so that AT think the tab is already selected. + // Otherwise AT may confuse the user by stating that they need to activate the tab, + // but the tab will already be activated by the time the announcement finishes. + focusedTab.attr( "aria-selected", "false" ); + this.tabs.eq( selectedIndex ).attr( "aria-selected", "true" ); + + this.activating = this._delay(function() { + this.option( "active", selectedIndex ); + }, this.delay ); + } + }, + + _panelKeydown: function( event ) { + if ( this._handlePageNav( event ) ) { + return; + } + + // Ctrl+up moves focus to the current tab + if ( event.ctrlKey && event.keyCode === $.ui.keyCode.UP ) { + event.preventDefault(); + this.active.focus(); + } + }, + + // Alt+page up/down moves focus to the previous/next tab (and activates) + _handlePageNav: function( event ) { + if ( event.altKey && event.keyCode === $.ui.keyCode.PAGE_UP ) { + this._activate( this._focusNextTab( this.options.active - 1, false ) ); + return true; + } + if ( event.altKey && event.keyCode === $.ui.keyCode.PAGE_DOWN ) { + this._activate( this._focusNextTab( this.options.active + 1, true ) ); + return true; + } + }, + + _findNextTab: function( index, goingForward ) { + var lastTabIndex = this.tabs.length - 1; + + function constrain() { + if ( index > lastTabIndex ) { + index = 0; + } + if ( index < 0 ) { + index = lastTabIndex; + } + return index; + } + + while ( $.inArray( constrain(), this.options.disabled ) !== -1 ) { + index = goingForward ? index + 1 : index - 1; + } + + return index; + }, + + _focusNextTab: function( index, goingForward ) { + index = this._findNextTab( index, goingForward ); + this.tabs.eq( index ).focus(); + return index; + }, + + _setOption: function( key, value ) { + if ( key === "active" ) { + // _activate() will handle invalid values and update this.options + this._activate( value ); + return; + } + + if ( key === "disabled" ) { + // don't use the widget factory's disabled handling + this._setupDisabled( value ); + return; + } + + this._super( key, value); + + if ( key === "collapsible" ) { + this.element.toggleClass( "ui-tabs-collapsible", value ); + // Setting collapsible: false while collapsed; open first panel + if ( !value && this.options.active === false ) { + this._activate( 0 ); + } + } + + if ( key === "event" ) { + this._setupEvents( value ); + } + + if ( key === "heightStyle" ) { + this._setupHeightStyle( value ); + } + }, + + _tabId: function( tab ) { + return tab.attr( "aria-controls" ) || "ui-tabs-" + getNextTabId(); + }, + + _sanitizeSelector: function( hash ) { + return hash ? hash.replace( /[!"$%&'()*+,.\/:;<=>?@\[\]\^`{|}~]/g, "\\$&" ) : ""; + }, + + refresh: function() { + var options = this.options, + lis = this.tablist.children( ":has(a[href])" ); + + // get disabled tabs from class attribute from HTML + // this will get converted to a boolean if needed in _refresh() + options.disabled = $.map( lis.filter( ".ui-state-disabled" ), function( tab ) { + return lis.index( tab ); + }); + + this._processTabs(); + + // was collapsed or no tabs + if ( options.active === false || !this.anchors.length ) { + options.active = false; + this.active = $(); + // was active, but active tab is gone + } else if ( this.active.length && !$.contains( this.tablist[ 0 ], this.active[ 0 ] ) ) { + // all remaining tabs are disabled + if ( this.tabs.length === options.disabled.length ) { + options.active = false; + this.active = $(); + // activate previous tab + } else { + this._activate( this._findNextTab( Math.max( 0, options.active - 1 ), false ) ); + } + // was active, active tab still exists + } else { + // make sure active index is correct + options.active = this.tabs.index( this.active ); + } + + this._refresh(); + }, + + _refresh: function() { + this._setupDisabled( this.options.disabled ); + this._setupEvents( this.options.event ); + this._setupHeightStyle( this.options.heightStyle ); + + this.tabs.not( this.active ).attr({ + "aria-selected": "false", + tabIndex: -1 + }); + this.panels.not( this._getPanelForTab( this.active ) ) + .hide() + .attr({ + "aria-expanded": "false", + "aria-hidden": "true" + }); + + // Make sure one tab is in the tab order + if ( !this.active.length ) { + this.tabs.eq( 0 ).attr( "tabIndex", 0 ); + } else { + this.active + .addClass( "ui-tabs-active ui-state-active" ) + .attr({ + "aria-selected": "true", + tabIndex: 0 + }); + this._getPanelForTab( this.active ) + .show() + .attr({ + "aria-expanded": "true", + "aria-hidden": "false" + }); + } + }, + + _processTabs: function() { + var that = this; + + this.tablist = this._getList() + .addClass( "ui-tabs-nav ui-helper-reset ui-helper-clearfix ui-widget-header ui-corner-all" ) + .attr( "role", "tablist" ); + + this.tabs = this.tablist.find( "> li:has(a[href])" ) + .addClass( "ui-state-default ui-corner-top" ) + .attr({ + role: "tab", + tabIndex: -1 + }); + + this.anchors = this.tabs.map(function() { + return $( "a", this )[ 0 ]; + }) + .addClass( "ui-tabs-anchor" ) + .attr({ + role: "presentation", + tabIndex: -1 + }); + + this.panels = $(); + + this.anchors.each(function( i, anchor ) { + var selector, panel, panelId, + anchorId = $( anchor ).uniqueId().attr( "id" ), + tab = $( anchor ).closest( "li" ), + originalAriaControls = tab.attr( "aria-controls" ); + + // inline tab + if ( isLocal( anchor ) ) { + selector = anchor.hash; + panel = that.element.find( that._sanitizeSelector( selector ) ); + // remote tab + } else { + panelId = that._tabId( tab ); + selector = "#" + panelId; + panel = that.element.find( selector ); + if ( !panel.length ) { + panel = that._createPanel( panelId ); + panel.insertAfter( that.panels[ i - 1 ] || that.tablist ); + } + panel.attr( "aria-live", "polite" ); + } + + if ( panel.length) { + that.panels = that.panels.add( panel ); + } + if ( originalAriaControls ) { + tab.data( "ui-tabs-aria-controls", originalAriaControls ); + } + tab.attr({ + "aria-controls": selector.substring( 1 ), + "aria-labelledby": anchorId + }); + panel.attr( "aria-labelledby", anchorId ); + }); + + this.panels + .addClass( "ui-tabs-panel ui-widget-content ui-corner-bottom" ) + .attr( "role", "tabpanel" ); + }, + + // allow overriding how to find the list for rare usage scenarios (#7715) + _getList: function() { + return this.element.find( "ol,ul" ).eq( 0 ); + }, + + _createPanel: function( id ) { + return $( "
    " ) + .attr( "id", id ) + .addClass( "ui-tabs-panel ui-widget-content ui-corner-bottom" ) + .data( "ui-tabs-destroy", true ); + }, + + _setupDisabled: function( disabled ) { + if ( $.isArray( disabled ) ) { + if ( !disabled.length ) { + disabled = false; + } else if ( disabled.length === this.anchors.length ) { + disabled = true; + } + } + + // disable tabs + for ( var i = 0, li; ( li = this.tabs[ i ] ); i++ ) { + if ( disabled === true || $.inArray( i, disabled ) !== -1 ) { + $( li ) + .addClass( "ui-state-disabled" ) + .attr( "aria-disabled", "true" ); + } else { + $( li ) + .removeClass( "ui-state-disabled" ) + .removeAttr( "aria-disabled" ); + } + } + + this.options.disabled = disabled; + }, + + _setupEvents: function( event ) { + var events = { + click: function( event ) { + event.preventDefault(); + } + }; + if ( event ) { + $.each( event.split(" "), function( index, eventName ) { + events[ eventName ] = "_eventHandler"; + }); + } + + this._off( this.anchors.add( this.tabs ).add( this.panels ) ); + this._on( this.anchors, events ); + this._on( this.tabs, { keydown: "_tabKeydown" } ); + this._on( this.panels, { keydown: "_panelKeydown" } ); + + this._focusable( this.tabs ); + this._hoverable( this.tabs ); + }, + + _setupHeightStyle: function( heightStyle ) { + var maxHeight, overflow, + parent = this.element.parent(); + + if ( heightStyle === "fill" ) { + // IE 6 treats height like minHeight, so we need to turn off overflow + // in order to get a reliable height + // we use the minHeight support test because we assume that only + // browsers that don't support minHeight will treat height as minHeight + if ( !$.support.minHeight ) { + overflow = parent.css( "overflow" ); + parent.css( "overflow", "hidden"); + } + maxHeight = parent.height(); + this.element.siblings( ":visible" ).each(function() { + var elem = $( this ), + position = elem.css( "position" ); + + if ( position === "absolute" || position === "fixed" ) { + return; + } + maxHeight -= elem.outerHeight( true ); + }); + if ( overflow ) { + parent.css( "overflow", overflow ); + } + + this.element.children().not( this.panels ).each(function() { + maxHeight -= $( this ).outerHeight( true ); + }); + + this.panels.each(function() { + $( this ).height( Math.max( 0, maxHeight - + $( this ).innerHeight() + $( this ).height() ) ); + }) + .css( "overflow", "auto" ); + } else if ( heightStyle === "auto" ) { + maxHeight = 0; + this.panels.each(function() { + maxHeight = Math.max( maxHeight, $( this ).height( "" ).height() ); + }).height( maxHeight ); + } + }, + + _eventHandler: function( event ) { + var options = this.options, + active = this.active, + anchor = $( event.currentTarget ), + tab = anchor.closest( "li" ), + clickedIsActive = tab[ 0 ] === active[ 0 ], + collapsing = clickedIsActive && options.collapsible, + toShow = collapsing ? $() : this._getPanelForTab( tab ), + toHide = !active.length ? $() : this._getPanelForTab( active ), + eventData = { + oldTab: active, + oldPanel: toHide, + newTab: collapsing ? $() : tab, + newPanel: toShow + }; + + event.preventDefault(); + + if ( tab.hasClass( "ui-state-disabled" ) || + // tab is already loading + tab.hasClass( "ui-tabs-loading" ) || + // can't switch durning an animation + this.running || + // click on active header, but not collapsible + ( clickedIsActive && !options.collapsible ) || + // allow canceling activation + ( this._trigger( "beforeActivate", event, eventData ) === false ) ) { + return; + } + + options.active = collapsing ? false : this.tabs.index( tab ); + + this.active = clickedIsActive ? $() : tab; + if ( this.xhr ) { + this.xhr.abort(); + } + + if ( !toHide.length && !toShow.length ) { + $.error( "jQuery UI Tabs: Mismatching fragment identifier." ); + } + + if ( toShow.length ) { + this.load( this.tabs.index( tab ), event ); + } + this._toggle( event, eventData ); + }, + + // handles show/hide for selecting tabs + _toggle: function( event, eventData ) { + var that = this, + toShow = eventData.newPanel, + toHide = eventData.oldPanel; + + this.running = true; + + function complete() { + that.running = false; + that._trigger( "activate", event, eventData ); + } + + function show() { + eventData.newTab.closest( "li" ).addClass( "ui-tabs-active ui-state-active" ); + + if ( toShow.length && that.options.show ) { + that._show( toShow, that.options.show, complete ); + } else { + toShow.show(); + complete(); + } + } + + // start out by hiding, then showing, then completing + if ( toHide.length && this.options.hide ) { + this._hide( toHide, this.options.hide, function() { + eventData.oldTab.closest( "li" ).removeClass( "ui-tabs-active ui-state-active" ); + show(); + }); + } else { + eventData.oldTab.closest( "li" ).removeClass( "ui-tabs-active ui-state-active" ); + toHide.hide(); + show(); + } + + toHide.attr({ + "aria-expanded": "false", + "aria-hidden": "true" + }); + eventData.oldTab.attr( "aria-selected", "false" ); + // If we're switching tabs, remove the old tab from the tab order. + // If we're opening from collapsed state, remove the previous tab from the tab order. + // If we're collapsing, then keep the collapsing tab in the tab order. + if ( toShow.length && toHide.length ) { + eventData.oldTab.attr( "tabIndex", -1 ); + } else if ( toShow.length ) { + this.tabs.filter(function() { + return $( this ).attr( "tabIndex" ) === 0; + }) + .attr( "tabIndex", -1 ); + } + + toShow.attr({ + "aria-expanded": "true", + "aria-hidden": "false" + }); + eventData.newTab.attr({ + "aria-selected": "true", + tabIndex: 0 + }); + }, + + _activate: function( index ) { + var anchor, + active = this._findActive( index ); + + // trying to activate the already active panel + if ( active[ 0 ] === this.active[ 0 ] ) { + return; + } + + // trying to collapse, simulate a click on the current active header + if ( !active.length ) { + active = this.active; + } + + anchor = active.find( ".ui-tabs-anchor" )[ 0 ]; + this._eventHandler({ + target: anchor, + currentTarget: anchor, + preventDefault: $.noop + }); + }, + + _findActive: function( index ) { + return index === false ? $() : this.tabs.eq( index ); + }, + + _getIndex: function( index ) { + // meta-function to give users option to provide a href string instead of a numerical index. + if ( typeof index === "string" ) { + index = this.anchors.index( this.anchors.filter( "[href$='" + index + "']" ) ); + } + + return index; + }, + + _destroy: function() { + if ( this.xhr ) { + this.xhr.abort(); + } + + this.element.removeClass( "ui-tabs ui-widget ui-widget-content ui-corner-all ui-tabs-collapsible" ); + + this.tablist + .removeClass( "ui-tabs-nav ui-helper-reset ui-helper-clearfix ui-widget-header ui-corner-all" ) + .removeAttr( "role" ); + + this.anchors + .removeClass( "ui-tabs-anchor" ) + .removeAttr( "role" ) + .removeAttr( "tabIndex" ) + .removeData( "href.tabs" ) + .removeData( "load.tabs" ) + .removeUniqueId(); + + this.tabs.add( this.panels ).each(function() { + if ( $.data( this, "ui-tabs-destroy" ) ) { + $( this ).remove(); + } else { + $( this ) + .removeClass( "ui-state-default ui-state-active ui-state-disabled " + + "ui-corner-top ui-corner-bottom ui-widget-content ui-tabs-active ui-tabs-panel" ) + .removeAttr( "tabIndex" ) + .removeAttr( "aria-live" ) + .removeAttr( "aria-busy" ) + .removeAttr( "aria-selected" ) + .removeAttr( "aria-labelledby" ) + .removeAttr( "aria-hidden" ) + .removeAttr( "aria-expanded" ) + .removeAttr( "role" ); + } + }); + + this.tabs.each(function() { + var li = $( this ), + prev = li.data( "ui-tabs-aria-controls" ); + if ( prev ) { + li.attr( "aria-controls", prev ); + } else { + li.removeAttr( "aria-controls" ); + } + }); + + this.panels.show(); + + if ( this.options.heightStyle !== "content" ) { + this.panels.css( "height", "" ); + } + }, + + enable: function( index ) { + var disabled = this.options.disabled; + if ( disabled === false ) { + return; + } + + if ( index === undefined ) { + disabled = false; + } else { + index = this._getIndex( index ); + if ( $.isArray( disabled ) ) { + disabled = $.map( disabled, function( num ) { + return num !== index ? num : null; + }); + } else { + disabled = $.map( this.tabs, function( li, num ) { + return num !== index ? num : null; + }); + } + } + this._setupDisabled( disabled ); + }, + + disable: function( index ) { + var disabled = this.options.disabled; + if ( disabled === true ) { + return; + } + + if ( index === undefined ) { + disabled = true; + } else { + index = this._getIndex( index ); + if ( $.inArray( index, disabled ) !== -1 ) { + return; + } + if ( $.isArray( disabled ) ) { + disabled = $.merge( [ index ], disabled ).sort(); + } else { + disabled = [ index ]; + } + } + this._setupDisabled( disabled ); + }, + + load: function( index, event ) { + index = this._getIndex( index ); + var that = this, + tab = this.tabs.eq( index ), + anchor = tab.find( ".ui-tabs-anchor" ), + panel = this._getPanelForTab( tab ), + eventData = { + tab: tab, + panel: panel + }; + + // not remote + if ( isLocal( anchor[ 0 ] ) ) { + return; + } + + this.xhr = $.ajax( this._ajaxSettings( anchor, event, eventData ) ); + + // support: jQuery <1.8 + // jQuery <1.8 returns false if the request is canceled in beforeSend, + // but as of 1.8, $.ajax() always returns a jqXHR object. + if ( this.xhr && this.xhr.statusText !== "canceled" ) { + tab.addClass( "ui-tabs-loading" ); + panel.attr( "aria-busy", "true" ); + + this.xhr + .success(function( response ) { + // support: jQuery <1.8 + // http://bugs.jquery.com/ticket/11778 + setTimeout(function() { + panel.html( response ); + that._trigger( "load", event, eventData ); + }, 1 ); + }) + .complete(function( jqXHR, status ) { + // support: jQuery <1.8 + // http://bugs.jquery.com/ticket/11778 + setTimeout(function() { + if ( status === "abort" ) { + that.panels.stop( false, true ); + } + + tab.removeClass( "ui-tabs-loading" ); + panel.removeAttr( "aria-busy" ); + + if ( jqXHR === that.xhr ) { + delete that.xhr; + } + }, 1 ); + }); + } + }, + + // TODO: Remove this function in 1.10 when ajaxOptions is removed + _ajaxSettings: function( anchor, event, eventData ) { + var that = this; + return { + url: anchor.attr( "href" ), + beforeSend: function( jqXHR, settings ) { + return that._trigger( "beforeLoad", event, + $.extend( { jqXHR : jqXHR, ajaxSettings: settings }, eventData ) ); + } + }; + }, + + _getPanelForTab: function( tab ) { + var id = $( tab ).attr( "aria-controls" ); + return this.element.find( this._sanitizeSelector( "#" + id ) ); + } +}); + +// DEPRECATED +if ( $.uiBackCompat !== false ) { + + // helper method for a lot of the back compat extensions + $.ui.tabs.prototype._ui = function( tab, panel ) { + return { + tab: tab, + panel: panel, + index: this.anchors.index( tab ) + }; + }; + + // url method + $.widget( "ui.tabs", $.ui.tabs, { + url: function( index, url ) { + this.anchors.eq( index ).attr( "href", url ); + } + }); + + // TODO: Remove _ajaxSettings() method when removing this extension + // ajaxOptions and cache options + $.widget( "ui.tabs", $.ui.tabs, { + options: { + ajaxOptions: null, + cache: false + }, + + _create: function() { + this._super(); + + var that = this; + + this._on({ tabsbeforeload: function( event, ui ) { + // tab is already cached + if ( $.data( ui.tab[ 0 ], "cache.tabs" ) ) { + event.preventDefault(); + return; + } + + ui.jqXHR.success(function() { + if ( that.options.cache ) { + $.data( ui.tab[ 0 ], "cache.tabs", true ); + } + }); + }}); + }, + + _ajaxSettings: function( anchor, event, ui ) { + var ajaxOptions = this.options.ajaxOptions; + return $.extend( {}, ajaxOptions, { + error: function( xhr, status ) { + try { + // Passing index avoid a race condition when this method is + // called after the user has selected another tab. + // Pass the anchor that initiated this request allows + // loadError to manipulate the tab content panel via $(a.hash) + ajaxOptions.error( + xhr, status, ui.tab.closest( "li" ).index(), ui.tab[ 0 ] ); + } + catch ( error ) {} + } + }, this._superApply( arguments ) ); + }, + + _setOption: function( key, value ) { + // reset cache if switching from cached to not cached + if ( key === "cache" && value === false ) { + this.anchors.removeData( "cache.tabs" ); + } + this._super( key, value ); + }, + + _destroy: function() { + this.anchors.removeData( "cache.tabs" ); + this._super(); + }, + + url: function( index ){ + this.anchors.eq( index ).removeData( "cache.tabs" ); + this._superApply( arguments ); + } + }); + + // abort method + $.widget( "ui.tabs", $.ui.tabs, { + abort: function() { + if ( this.xhr ) { + this.xhr.abort(); + } + } + }); + + // spinner + $.widget( "ui.tabs", $.ui.tabs, { + options: { + spinner: "Loading…" + }, + _create: function() { + this._super(); + this._on({ + tabsbeforeload: function( event, ui ) { + // Don't react to nested tabs or tabs that don't use a spinner + if ( event.target !== this.element[ 0 ] || + !this.options.spinner ) { + return; + } + + var span = ui.tab.find( "span" ), + html = span.html(); + span.html( this.options.spinner ); + ui.jqXHR.complete(function() { + span.html( html ); + }); + } + }); + } + }); + + // enable/disable events + $.widget( "ui.tabs", $.ui.tabs, { + options: { + enable: null, + disable: null + }, + + enable: function( index ) { + var options = this.options, + trigger; + + if ( index && options.disabled === true || + ( $.isArray( options.disabled ) && $.inArray( index, options.disabled ) !== -1 ) ) { + trigger = true; + } + + this._superApply( arguments ); + + if ( trigger ) { + this._trigger( "enable", null, this._ui( this.anchors[ index ], this.panels[ index ] ) ); + } + }, + + disable: function( index ) { + var options = this.options, + trigger; + + if ( index && options.disabled === false || + ( $.isArray( options.disabled ) && $.inArray( index, options.disabled ) === -1 ) ) { + trigger = true; + } + + this._superApply( arguments ); + + if ( trigger ) { + this._trigger( "disable", null, this._ui( this.anchors[ index ], this.panels[ index ] ) ); + } + } + }); + + // add/remove methods and events + $.widget( "ui.tabs", $.ui.tabs, { + options: { + add: null, + remove: null, + tabTemplate: "
  • #{label}
  • " + }, + + add: function( url, label, index ) { + if ( index === undefined ) { + index = this.anchors.length; + } + + var doInsertAfter, panel, + options = this.options, + li = $( options.tabTemplate + .replace( /#\{href\}/g, url ) + .replace( /#\{label\}/g, label ) ), + id = !url.indexOf( "#" ) ? + url.replace( "#", "" ) : + this._tabId( li ); + + li.addClass( "ui-state-default ui-corner-top" ).data( "ui-tabs-destroy", true ); + li.attr( "aria-controls", id ); + + doInsertAfter = index >= this.tabs.length; + + // try to find an existing element before creating a new one + panel = this.element.find( "#" + id ); + if ( !panel.length ) { + panel = this._createPanel( id ); + if ( doInsertAfter ) { + if ( index > 0 ) { + panel.insertAfter( this.panels.eq( -1 ) ); + } else { + panel.appendTo( this.element ); + } + } else { + panel.insertBefore( this.panels[ index ] ); + } + } + panel.addClass( "ui-tabs-panel ui-widget-content ui-corner-bottom" ).hide(); + + if ( doInsertAfter ) { + li.appendTo( this.tablist ); + } else { + li.insertBefore( this.tabs[ index ] ); + } + + options.disabled = $.map( options.disabled, function( n ) { + return n >= index ? ++n : n; + }); + + this.refresh(); + if ( this.tabs.length === 1 && options.active === false ) { + this.option( "active", 0 ); + } + + this._trigger( "add", null, this._ui( this.anchors[ index ], this.panels[ index ] ) ); + return this; + }, + + remove: function( index ) { + index = this._getIndex( index ); + var options = this.options, + tab = this.tabs.eq( index ).remove(), + panel = this._getPanelForTab( tab ).remove(); + + // If selected tab was removed focus tab to the right or + // in case the last tab was removed the tab to the left. + // We check for more than 2 tabs, because if there are only 2, + // then when we remove this tab, there will only be one tab left + // so we don't need to detect which tab to activate. + if ( tab.hasClass( "ui-tabs-active" ) && this.anchors.length > 2 ) { + this._activate( index + ( index + 1 < this.anchors.length ? 1 : -1 ) ); + } + + options.disabled = $.map( + $.grep( options.disabled, function( n ) { + return n !== index; + }), + function( n ) { + return n >= index ? --n : n; + }); + + this.refresh(); + + this._trigger( "remove", null, this._ui( tab.find( "a" )[ 0 ], panel[ 0 ] ) ); + return this; + } + }); + + // length method + $.widget( "ui.tabs", $.ui.tabs, { + length: function() { + return this.anchors.length; + } + }); + + // panel ids (idPrefix option + title attribute) + $.widget( "ui.tabs", $.ui.tabs, { + options: { + idPrefix: "ui-tabs-" + }, + + _tabId: function( tab ) { + var a = tab.is( "li" ) ? tab.find( "a[href]" ) : tab; + a = a[0]; + return $( a ).closest( "li" ).attr( "aria-controls" ) || + a.title && a.title.replace( /\s/g, "_" ).replace( /[^\w\u00c0-\uFFFF\-]/g, "" ) || + this.options.idPrefix + getNextTabId(); + } + }); + + // _createPanel method + $.widget( "ui.tabs", $.ui.tabs, { + options: { + panelTemplate: "
    " + }, + + _createPanel: function( id ) { + return $( this.options.panelTemplate ) + .attr( "id", id ) + .addClass( "ui-tabs-panel ui-widget-content ui-corner-bottom" ) + .data( "ui-tabs-destroy", true ); + } + }); + + // selected option + $.widget( "ui.tabs", $.ui.tabs, { + _create: function() { + var options = this.options; + if ( options.active === null && options.selected !== undefined ) { + options.active = options.selected === -1 ? false : options.selected; + } + this._super(); + options.selected = options.active; + if ( options.selected === false ) { + options.selected = -1; + } + }, + + _setOption: function( key, value ) { + if ( key !== "selected" ) { + return this._super( key, value ); + } + + var options = this.options; + this._super( "active", value === -1 ? false : value ); + options.selected = options.active; + if ( options.selected === false ) { + options.selected = -1; + } + }, + + _eventHandler: function() { + this._superApply( arguments ); + this.options.selected = this.options.active; + if ( this.options.selected === false ) { + this.options.selected = -1; + } + } + }); + + // show and select event + $.widget( "ui.tabs", $.ui.tabs, { + options: { + show: null, + select: null + }, + _create: function() { + this._super(); + if ( this.options.active !== false ) { + this._trigger( "show", null, this._ui( + this.active.find( ".ui-tabs-anchor" )[ 0 ], + this._getPanelForTab( this.active )[ 0 ] ) ); + } + }, + _trigger: function( type, event, data ) { + var tab, panel, + ret = this._superApply( arguments ); + + if ( !ret ) { + return false; + } + + if ( type === "beforeActivate" ) { + tab = data.newTab.length ? data.newTab : data.oldTab; + panel = data.newPanel.length ? data.newPanel : data.oldPanel; + ret = this._super( "select", event, { + tab: tab.find( ".ui-tabs-anchor" )[ 0], + panel: panel[ 0 ], + index: tab.closest( "li" ).index() + }); + } else if ( type === "activate" && data.newTab.length ) { + ret = this._super( "show", event, { + tab: data.newTab.find( ".ui-tabs-anchor" )[ 0 ], + panel: data.newPanel[ 0 ], + index: data.newTab.closest( "li" ).index() + }); + } + return ret; + } + }); + + // select method + $.widget( "ui.tabs", $.ui.tabs, { + select: function( index ) { + index = this._getIndex( index ); + if ( index === -1 ) { + if ( this.options.collapsible && this.options.selected !== -1 ) { + index = this.options.selected; + } else { + return; + } + } + this.anchors.eq( index ).trigger( this.options.event + this.eventNamespace ); + } + }); + + // cookie option + (function() { + + var listId = 0; + + $.widget( "ui.tabs", $.ui.tabs, { + options: { + cookie: null // e.g. { expires: 7, path: '/', domain: 'jquery.com', secure: true } + }, + _create: function() { + var options = this.options, + active; + if ( options.active == null && options.cookie ) { + active = parseInt( this._cookie(), 10 ); + if ( active === -1 ) { + active = false; + } + options.active = active; + } + this._super(); + }, + _cookie: function( active ) { + var cookie = [ this.cookie || + ( this.cookie = this.options.cookie.name || "ui-tabs-" + (++listId) ) ]; + if ( arguments.length ) { + cookie.push( active === false ? -1 : active ); + cookie.push( this.options.cookie ); + } + return $.cookie.apply( null, cookie ); + }, + _refresh: function() { + this._super(); + if ( this.options.cookie ) { + this._cookie( this.options.active, this.options.cookie ); + } + }, + _eventHandler: function() { + this._superApply( arguments ); + if ( this.options.cookie ) { + this._cookie( this.options.active, this.options.cookie ); + } + }, + _destroy: function() { + this._super(); + if ( this.options.cookie ) { + this._cookie( null, this.options.cookie ); + } + } + }); + + })(); + + // load event + $.widget( "ui.tabs", $.ui.tabs, { + _trigger: function( type, event, data ) { + var _data = $.extend( {}, data ); + if ( type === "load" ) { + _data.panel = _data.panel[ 0 ]; + _data.tab = _data.tab.find( ".ui-tabs-anchor" )[ 0 ]; + } + return this._super( type, event, _data ); + } + }); + + // fx option + // The new animation options (show, hide) conflict with the old show callback. + // The old fx option wins over show/hide anyway (always favor back-compat). + // If a user wants to use the new animation API, they must give up the old API. + $.widget( "ui.tabs", $.ui.tabs, { + options: { + fx: null // e.g. { height: "toggle", opacity: "toggle", duration: 200 } + }, + + _getFx: function() { + var hide, show, + fx = this.options.fx; + + if ( fx ) { + if ( $.isArray( fx ) ) { + hide = fx[ 0 ]; + show = fx[ 1 ]; + } else { + hide = show = fx; + } + } + + return fx ? { show: show, hide: hide } : null; + }, + + _toggle: function( event, eventData ) { + var that = this, + toShow = eventData.newPanel, + toHide = eventData.oldPanel, + fx = this._getFx(); + + if ( !fx ) { + return this._super( event, eventData ); + } + + that.running = true; + + function complete() { + that.running = false; + that._trigger( "activate", event, eventData ); + } + + function show() { + eventData.newTab.closest( "li" ).addClass( "ui-tabs-active ui-state-active" ); + + if ( toShow.length && fx.show ) { + toShow + .animate( fx.show, fx.show.duration, function() { + complete(); + }); + } else { + toShow.show(); + complete(); + } + } + + // start out by hiding, then showing, then completing + if ( toHide.length && fx.hide ) { + toHide.animate( fx.hide, fx.hide.duration, function() { + eventData.oldTab.closest( "li" ).removeClass( "ui-tabs-active ui-state-active" ); + show(); + }); + } else { + eventData.oldTab.closest( "li" ).removeClass( "ui-tabs-active ui-state-active" ); + toHide.hide(); + show(); + } + } + }); +} + +})( jQuery ); + +(function( $ ) { + +var increments = 0; + +function addDescribedBy( elem, id ) { + var describedby = (elem.attr( "aria-describedby" ) || "").split( /\s+/ ); + describedby.push( id ); + elem + .data( "ui-tooltip-id", id ) + .attr( "aria-describedby", $.trim( describedby.join( " " ) ) ); +} + +function removeDescribedBy( elem ) { + var id = elem.data( "ui-tooltip-id" ), + describedby = (elem.attr( "aria-describedby" ) || "").split( /\s+/ ), + index = $.inArray( id, describedby ); + if ( index !== -1 ) { + describedby.splice( index, 1 ); + } + + elem.removeData( "ui-tooltip-id" ); + describedby = $.trim( describedby.join( " " ) ); + if ( describedby ) { + elem.attr( "aria-describedby", describedby ); + } else { + elem.removeAttr( "aria-describedby" ); + } +} + +$.widget( "ui.tooltip", { + version: "1.9.2", + options: { + content: function() { + return $( this ).attr( "title" ); + }, + hide: true, + // Disabled elements have inconsistent behavior across browsers (#8661) + items: "[title]:not([disabled])", + position: { + my: "left top+15", + at: "left bottom", + collision: "flipfit flip" + }, + show: true, + tooltipClass: null, + track: false, + + // callbacks + close: null, + open: null + }, + + _create: function() { + this._on({ + mouseover: "open", + focusin: "open" + }); + + // IDs of generated tooltips, needed for destroy + this.tooltips = {}; + // IDs of parent tooltips where we removed the title attribute + this.parents = {}; + + if ( this.options.disabled ) { + this._disable(); + } + }, + + _setOption: function( key, value ) { + var that = this; + + if ( key === "disabled" ) { + this[ value ? "_disable" : "_enable" ](); + this.options[ key ] = value; + // disable element style changes + return; + } + + this._super( key, value ); + + if ( key === "content" ) { + $.each( this.tooltips, function( id, element ) { + that._updateContent( element ); + }); + } + }, + + _disable: function() { + var that = this; + + // close open tooltips + $.each( this.tooltips, function( id, element ) { + var event = $.Event( "blur" ); + event.target = event.currentTarget = element[0]; + that.close( event, true ); + }); + + // remove title attributes to prevent native tooltips + this.element.find( this.options.items ).andSelf().each(function() { + var element = $( this ); + if ( element.is( "[title]" ) ) { + element + .data( "ui-tooltip-title", element.attr( "title" ) ) + .attr( "title", "" ); + } + }); + }, + + _enable: function() { + // restore title attributes + this.element.find( this.options.items ).andSelf().each(function() { + var element = $( this ); + if ( element.data( "ui-tooltip-title" ) ) { + element.attr( "title", element.data( "ui-tooltip-title" ) ); + } + }); + }, + + open: function( event ) { + var that = this, + target = $( event ? event.target : this.element ) + // we need closest here due to mouseover bubbling, + // but always pointing at the same event target + .closest( this.options.items ); + + // No element to show a tooltip for or the tooltip is already open + if ( !target.length || target.data( "ui-tooltip-id" ) ) { + return; + } + + if ( target.attr( "title" ) ) { + target.data( "ui-tooltip-title", target.attr( "title" ) ); + } + + target.data( "ui-tooltip-open", true ); + + // kill parent tooltips, custom or native, for hover + if ( event && event.type === "mouseover" ) { + target.parents().each(function() { + var parent = $( this ), + blurEvent; + if ( parent.data( "ui-tooltip-open" ) ) { + blurEvent = $.Event( "blur" ); + blurEvent.target = blurEvent.currentTarget = this; + that.close( blurEvent, true ); + } + if ( parent.attr( "title" ) ) { + parent.uniqueId(); + that.parents[ this.id ] = { + element: this, + title: parent.attr( "title" ) + }; + parent.attr( "title", "" ); + } + }); + } + + this._updateContent( target, event ); + }, + + _updateContent: function( target, event ) { + var content, + contentOption = this.options.content, + that = this, + eventType = event ? event.type : null; + + if ( typeof contentOption === "string" ) { + return this._open( event, target, contentOption ); + } + + content = contentOption.call( target[0], function( response ) { + // ignore async response if tooltip was closed already + if ( !target.data( "ui-tooltip-open" ) ) { + return; + } + // IE may instantly serve a cached response for ajax requests + // delay this call to _open so the other call to _open runs first + that._delay(function() { + // jQuery creates a special event for focusin when it doesn't + // exist natively. To improve performance, the native event + // object is reused and the type is changed. Therefore, we can't + // rely on the type being correct after the event finished + // bubbling, so we set it back to the previous value. (#8740) + if ( event ) { + event.type = eventType; + } + this._open( event, target, response ); + }); + }); + if ( content ) { + this._open( event, target, content ); + } + }, + + _open: function( event, target, content ) { + var tooltip, events, delayedShow, + positionOption = $.extend( {}, this.options.position ); + + if ( !content ) { + return; + } + + // Content can be updated multiple times. If the tooltip already + // exists, then just update the content and bail. + tooltip = this._find( target ); + if ( tooltip.length ) { + tooltip.find( ".ui-tooltip-content" ).html( content ); + return; + } + + // if we have a title, clear it to prevent the native tooltip + // we have to check first to avoid defining a title if none exists + // (we don't want to cause an element to start matching [title]) + // + // We use removeAttr only for key events, to allow IE to export the correct + // accessible attributes. For mouse events, set to empty string to avoid + // native tooltip showing up (happens only when removing inside mouseover). + if ( target.is( "[title]" ) ) { + if ( event && event.type === "mouseover" ) { + target.attr( "title", "" ); + } else { + target.removeAttr( "title" ); + } + } + + tooltip = this._tooltip( target ); + addDescribedBy( target, tooltip.attr( "id" ) ); + tooltip.find( ".ui-tooltip-content" ).html( content ); + + function position( event ) { + positionOption.of = event; + if ( tooltip.is( ":hidden" ) ) { + return; + } + tooltip.position( positionOption ); + } + if ( this.options.track && event && /^mouse/.test( event.type ) ) { + this._on( this.document, { + mousemove: position + }); + // trigger once to override element-relative positioning + position( event ); + } else { + tooltip.position( $.extend({ + of: target + }, this.options.position ) ); + } + + tooltip.hide(); + + this._show( tooltip, this.options.show ); + // Handle tracking tooltips that are shown with a delay (#8644). As soon + // as the tooltip is visible, position the tooltip using the most recent + // event. + if ( this.options.show && this.options.show.delay ) { + delayedShow = setInterval(function() { + if ( tooltip.is( ":visible" ) ) { + position( positionOption.of ); + clearInterval( delayedShow ); + } + }, $.fx.interval ); + } + + this._trigger( "open", event, { tooltip: tooltip } ); + + events = { + keyup: function( event ) { + if ( event.keyCode === $.ui.keyCode.ESCAPE ) { + var fakeEvent = $.Event(event); + fakeEvent.currentTarget = target[0]; + this.close( fakeEvent, true ); + } + }, + remove: function() { + this._removeTooltip( tooltip ); + } + }; + if ( !event || event.type === "mouseover" ) { + events.mouseleave = "close"; + } + if ( !event || event.type === "focusin" ) { + events.focusout = "close"; + } + this._on( true, target, events ); + }, + + close: function( event ) { + var that = this, + target = $( event ? event.currentTarget : this.element ), + tooltip = this._find( target ); + + // disabling closes the tooltip, so we need to track when we're closing + // to avoid an infinite loop in case the tooltip becomes disabled on close + if ( this.closing ) { + return; + } + + // only set title if we had one before (see comment in _open()) + if ( target.data( "ui-tooltip-title" ) ) { + target.attr( "title", target.data( "ui-tooltip-title" ) ); + } + + removeDescribedBy( target ); + + tooltip.stop( true ); + this._hide( tooltip, this.options.hide, function() { + that._removeTooltip( $( this ) ); + }); + + target.removeData( "ui-tooltip-open" ); + this._off( target, "mouseleave focusout keyup" ); + // Remove 'remove' binding only on delegated targets + if ( target[0] !== this.element[0] ) { + this._off( target, "remove" ); + } + this._off( this.document, "mousemove" ); + + if ( event && event.type === "mouseleave" ) { + $.each( this.parents, function( id, parent ) { + $( parent.element ).attr( "title", parent.title ); + delete that.parents[ id ]; + }); + } + + this.closing = true; + this._trigger( "close", event, { tooltip: tooltip } ); + this.closing = false; + }, + + _tooltip: function( element ) { + var id = "ui-tooltip-" + increments++, + tooltip = $( "
    " ) + .attr({ + id: id, + role: "tooltip" + }) + .addClass( "ui-tooltip ui-widget ui-corner-all ui-widget-content " + + ( this.options.tooltipClass || "" ) ); + $( "
    " ) + .addClass( "ui-tooltip-content" ) + .appendTo( tooltip ); + tooltip.appendTo( this.document[0].body ); + if ( $.fn.bgiframe ) { + tooltip.bgiframe(); + } + this.tooltips[ id ] = element; + return tooltip; + }, + + _find: function( target ) { + var id = target.data( "ui-tooltip-id" ); + return id ? $( "#" + id ) : $(); + }, + + _removeTooltip: function( tooltip ) { + tooltip.remove(); + delete this.tooltips[ tooltip.attr( "id" ) ]; + }, + + _destroy: function() { + var that = this; + + // close open tooltips + $.each( this.tooltips, function( id, element ) { + // Delegate to close method to handle common cleanup + var event = $.Event( "blur" ); + event.target = event.currentTarget = element[0]; + that.close( event, true ); + + // Remove immediately; destroying an open tooltip doesn't use the + // hide animation + $( "#" + id ).remove(); + + // Restore the title + if ( element.data( "ui-tooltip-title" ) ) { + element.attr( "title", element.data( "ui-tooltip-title" ) ); + element.removeData( "ui-tooltip-title" ); + } + }); + } +}); + +}( jQuery ) ); diff --git a/t/embedded_files/result-light.css b/t/embedded_files/result-light.css new file mode 100644 index 0000000000..e69de29bb2 diff --git a/t/embedded_files/saved_resource.html b/t/embedded_files/saved_resource.html new file mode 100644 index 0000000000..2c6c5158d0 --- /dev/null +++ b/t/embedded_files/saved_resource.html @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + enter matrix2 by mgage + + + + + + + + + + + + + + + +
    + + +
    + + [p1p2x3x4x5x6Pb + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +] + +
    + + + + + + + +
    \ No newline at end of file From f6ca7fa27505ba8b4ade766cdfa39b5374b69d2f Mon Sep 17 00:00:00 2001 From: Michael Gage Date: Sat, 7 Oct 2017 21:18:11 -0400 Subject: [PATCH 03/30] Fix typo in POD docs --- macros/PGmatrixmacros.pl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/macros/PGmatrixmacros.pl b/macros/PGmatrixmacros.pl index c40d183b74..b34fd8b47f 100644 --- a/macros/PGmatrixmacros.pl +++ b/macros/PGmatrixmacros.pl @@ -650,7 +650,8 @@ sub ra_flatten_matrix{ \@array; } -= head4 apl_matrix_mult() + +=head4 apl_matrix_mult() # This subroutine is probably obsolete and not generally useful. # It was patterned after the APL From ec2eb882215d88f1da519f366b7ceca694d0daea Mon Sep 17 00:00:00 2001 From: Michael Gage Date: Sun, 8 Oct 2017 12:20:17 -0400 Subject: [PATCH 04/30] commit test files and quickMatrixEntry --- macros/quickMatrixEntry.pl | 120 +++++++++++++++++++++++++ t/matrix_tableau_tests/matrix_test3.pg | 74 +++++++++++++++ t/matrix_tableau_tests/matrix_test4.pg | 78 ++++++++++++++++ 3 files changed, 272 insertions(+) create mode 100755 macros/quickMatrixEntry.pl create mode 100644 t/matrix_tableau_tests/matrix_test3.pg create mode 100644 t/matrix_tableau_tests/matrix_test4.pg diff --git a/macros/quickMatrixEntry.pl b/macros/quickMatrixEntry.pl new file mode 100755 index 0000000000..10150f50c9 --- /dev/null +++ b/macros/quickMatrixEntry.pl @@ -0,0 +1,120 @@ +#!/usr/bin/perl -w + +################################### +# quick matrix entry package +################################### + +sub _quickMatrixEntry_init {}; # don't reload this file + +sub INITIALIZE_QUICK_MATRIX_ENTRY { + main::HEADER_TEXT($quick_entry_javascript); + main::TEXT($quick_entry_form); + return ''; +} + +# +sub MATRIX_ENTRY_BUTTON { + my $answer_number = shift; + # warn(" input reference is ". ref($answer_number)); + my ($rows,$columns) = @_; + if (ref($answer_number)=~/Matrix/i) { # (handed a MathObject matrix) + my $matrix = $answer_number; + ($rows,$columns) = $matrix->dimensions(); + $answer_number = $main::PG->{unlabeled_answer_blank_count} +1; + # the +1 assumes that the quick entry button comes before (above) the matrix answer blanks. + } + $rows=$rows//1; + $columns=$columns//5; + my $answer_name = "AnSwEr".sprintf('%04d',$answer_number); + # warn("answer number $answer_name rows $rows columns $columns"); + return qq! + $PAR + + $PAR!; +} + +our $quick_entry_javascript = <<'END_JS'; + +END_JS + +our $quick_entry_form = <<'END_TEXT'; +
    + + +
    +END_TEXT + +INITIALIZE_QUICK_MATRIX_ENTRY(); # only need the javascript to be entered once. +1; \ No newline at end of file diff --git a/t/matrix_tableau_tests/matrix_test3.pg b/t/matrix_tableau_tests/matrix_test3.pg new file mode 100644 index 0000000000..4c8892b662 --- /dev/null +++ b/t/matrix_tableau_tests/matrix_test3.pg @@ -0,0 +1,74 @@ +############################################## +DOCUMENT(); + +loadMacros( + "PGstandard.pl", # Standard macros for PG language + "MathObjects.pl", + "parserLinearInequality.pl", + "PGML.pl", + "quickMatrixEntry.pl", + "tableau.pl", + "PGmatrixmacros.pl", + "LinearProgramming.pl", + #"source.pl", # allows code to be displayed on certain sites. + "PGcourse.pl", +); + +############################################## + +Context("Matrix"); # need Matrix context to allow string input into Matrix. +$zeroLevelTol = 1E-03; +$zeroLevel = 1E-03; +$tolType = 'relative', # "absolute"; #relative +$tolerance = .001; + +Context()->flags->set( + tolType => $tolType, + tolerance =>$tolerance, + zeroLevel=>$zeroLevel, + zeroLevelTol=>$zeroLevelTol +); +our $context=Context(); + + +BEGIN_PGML +Test to see if the tolerance flags are being respected. +zeroLevelTol = [$zeroLevelTol] +zeroLevel = [$zeroLevel] +tolType = [$tolType] +tolerance = [$tolerance] +END_PGML +#Construct a small test matrix. +$m = Matrix("[[3,6,7],[2,0,8],[4,6,21],[-6,4,0]]"); +$m2 = Matrix("[[3,6,7],[1.9999,.0001,8],[4,6,21],[-6,4,1E-5]]"); + +BEGIN_PGML +m is [$m] +and m2 is [$m2] + +Enter [$m2] +[@MATRIX_ENTRY_BUTTON($m2)@]* +[@ ANS($m2->cmp), $m2->ans_array(15) @]* + + +Are these tolerance leveals being ignored? +m and m2 are equal = [@ $m == $m2 @] + +END_PGML + +BEGIN_TEXT +This is in the BEGIN_TEXT/END_TEXT section: $BR + +m is $m $BR +and m2 is $m2 $BR + +Enter $m +\{ MATRIX_ENTRY_BUTTON($m) \} +\{ ANS($m->cmp), $m->ans_array \} +$PAR +FIXME -- these tolerance leveals are being ignored +m and m2 are equal = \{ $m == $m2 \} + +END_TEXT + +ENDDOCUMENT(); \ No newline at end of file diff --git a/t/matrix_tableau_tests/matrix_test4.pg b/t/matrix_tableau_tests/matrix_test4.pg new file mode 100644 index 0000000000..82109a548f --- /dev/null +++ b/t/matrix_tableau_tests/matrix_test4.pg @@ -0,0 +1,78 @@ +############################################## +DOCUMENT(); + +loadMacros( + "PGstandard.pl", # Standard macros for PG language + "MathObjects.pl", + "parserLinearInequality.pl", + "PGML.pl", + "quickMatrixEntry.pl", + "tableau.pl", + "PGmatrixmacros.pl", + "LinearProgramming.pl", + #"source.pl", # allows code to be displayed on certain sites. + "PGcourse.pl", +); + +############################################## + +Context("Matrix"); # need Matrix context to allow string input into Matrix. +$zeroLevelTol = 1E-03; +$zeroLevel = 1E-03; +$tolType = 'relative'; # "absolute"; #relative +$tolerance = .001; + +Context("Matrix"); + +Context()->flags->set( + tolType => $tolType, + tolerance =>$tolerance, + zeroLevel=>$zeroLevel, + zeroLevelTol=>$zeroLevelTol +); + + +BEGIN_PGML +Test to see if the tolerance flags are being respected +when inserted into answer checkers +zeroLevelTol = [$zeroLevelTol] +zeroLevel = [$zeroLevel] +tolType = [$tolType] +tolerance = [$tolerance] +END_PGML + +#Construct a small test matrix. + +$m = Matrix("[[3,6,7],[2,0,8],[4,6,21],[-6,4,0]]"); +$m2 = Matrix("[[3,6,7],[1.9999,.0001,8],[4,6,21],[-6,4,1E-5]]"); + +BEGIN_PGML +m is [$m] +and m2 is [$m2] + +Enter [$m2] +[@MATRIX_ENTRY_BUTTON($m2)@]* +[@ ANS($m2->cmp), $m2->ans_array(15) @]* + + +Are these tolerance leveals being ignored? +m and m2 are equal = [@ $m == $m2 @] + +END_PGML + +BEGIN_TEXT +This is in the BEGIN_TEXT/END_TEXT section: $BR + +m is $m $BR +and m2 is $m2 $BR + +Enter $m +\{ MATRIX_ENTRY_BUTTON($m) \} +\{ ANS($m->cmp), $m->ans_array \} +$PAR +Are these tolerance levels are being ignored? +m and m2 are equal = \{ $m == $m2 \} + +END_TEXT + +ENDDOCUMENT(); \ No newline at end of file From 3aeecbe96b3696bdbf82a5ff8532e7908dc9c1d3 Mon Sep 17 00:00:00 2001 From: Michael Gage Date: Sun, 8 Oct 2017 12:24:27 -0400 Subject: [PATCH 05/30] Commit merge from develop_tableau --- lib/PGcore.pm | 2 +- lib/WeBWorK/PG/IO.pm | 64 +++++++++++++++++++++++++++++------- lib/WeBWorK/PG/Translator.pm | 2 +- macros/LinearProgramming.pl | 2 +- macros/PGbasicmacros.pl | 10 ++++-- macros/scaffold.pl | 4 +-- 6 files changed, 65 insertions(+), 19 deletions(-) diff --git a/lib/PGcore.pm b/lib/PGcore.pm index cc041d9fe1..7db209cad4 100755 --- a/lib/PGcore.pm +++ b/lib/PGcore.pm @@ -527,7 +527,7 @@ sub store_persistent_data { # will store strings only (so far) my $self = shift; my $label = shift; my @content = @_; - $self->internal_debug_message("PGcore::store_persistent_data: storing $label in PERSISTENCE_HASH"); + # $self->internal_debug_message("PGcore::store_persistent_data: storing $label in PERSISTENCE_HASH"); if (defined($self->{PERSISTENCE_HASH}->{$label}) ) { warn "can' overwrite $label in persistent data"; } else { diff --git a/lib/WeBWorK/PG/IO.pm b/lib/WeBWorK/PG/IO.pm index 0990606be6..8eb8c018c4 100644 --- a/lib/WeBWorK/PG/IO.pm +++ b/lib/WeBWorK/PG/IO.pm @@ -260,8 +260,12 @@ sub path_is_course_subdir { # sub query_sage_server { my ($python, $url, $accepted_tos, $setSeed, $webworkfunc, $debug, $curlCommand)=@_; - my $sagecall = qq{$curlCommand -i -k -sS -L --data-urlencode "accepted_tos=${accepted_tos}"}. +# my $sagecall = qq{$curlCommand -i -k -sS -L --http1.1 --data-urlencode "accepted_tos=${accepted_tos}"}. qq{ --data-urlencode 'user_expressions={"WEBWORK":"_webwork_safe_json(WEBWORK)"}' --data-urlencode "code=${setSeed}${webworkfunc}$python" $url}; + my $sagecall = qq{$curlCommand -i -k -sS -L --data-urlencode "accepted_tos=${accepted_tos}"}. + qq{ --data-urlencode 'user_expressions={"WEBWORK":"_webwork_safe_json(WEBWORK)"}' --data-urlencode "code=${setSeed}${webworkfunc}$python" $url}; + + my $output =`$sagecall`; if ($debug) { warn "debug is turned on in IO.pm. "; @@ -279,13 +283,44 @@ sub query_sage_server { # Access-Control-Allow-Origin: * # Content-Type: application/json; charset=UTF-8 # $content: Either error message about terms of service or output from sage - my ($continue, $header, @content) = split("\r\n\r\n",$output); - my $content = join("\r\n\r\n",@content); # handle case where there were blank lines in the content - # warn "output list is ", join("|||\n|||",($continue, $header, @content)); - # warn "header is $header =" , $header =~/200 OK\r\n/; + # find the header + # expecting something like + # HTTP/1.1 100 Continue + + # HTTP/1.1 200 OK + # Date: Wed, 20 Sep 2017 14:54:03 GMT + # ...... + # two blank lines + # content + + # or (notice that here there is no continue response) + # HTTP/2 200 + # date: Wed, 20 Sep 2017 16:06:03 GMT + # ...... + # two blank lines + # content + + my ($continue, $header, @content) = split("\r\n\r\n",$output); + #my $content = join("\r\n\r\n",@content); # handle case where there were blank lines in the content + my @lines = split("\r\n\r\n", $output); + $continue=0; + my $header_ok =0; + while (@lines) { + my $header_block = shift(@lines); + warn "checking for header: $header_block" if $debug; + next unless $header_block=~/\S/; #skip empty lines; + next if $header_block=~/HTTP/ and $header_block=~/100/; # skip continue line + if ($header_block=~/200/) { # 200 return is ok + $header_ok=1; + last; + } + } + my $content = join("|||\n|||",@lines) ; #headers have been removed. + #warn "output list is ", $content; # join("|||\n|||",($continue, $header, $content)); + #warn "header_ok is $header_ok"; my $result; - if ($header =~/200 OK\r\n/) { #success - $result = $content; + if ($header_ok) { #success put any extraneous splits back together + $result = join("\r\n\r\n",@lines); } else { warn "ERROR in contacting sage server. Did you accept the terms of service by setting {accepted_tos=>'true'} in the askSage options?\n $content\n"; @@ -343,7 +378,9 @@ END # has something been returned? not_null($output) or die "Unable to make a sage call to $url."; warn "IO::askSage: We have some kind of value |$output| returned from sage" if $output and $debug; - + if ($output =~ /"success":\s*true/ and $debug){ + warn '"success": true is present in the output'; + } my $decoded = decode_json($output); not_null($decoded) or die "Unable to decode sage output"; if ($debug and defined $decoded ) { @@ -351,16 +388,19 @@ END foreach my $key (keys %$decoded) {$warning_string .= "$key=".$decoded->{$key}.", ";} $warning_string .= ' end decoded contents'; #warn "\n$warning_string" if $debug; - warn " decoded contents \n", PGUtil::pretty_print($decoded, 'text'), "end decoded contents"; + warn " decoded contents \n", PGUtil::pretty_print($decoded, 'text'), "end decoded contents" if $debug; } # was there a Sage/python syntax Error # is the returned something text from stdout (deprecated) # have objects been returned in a WEBWORK variable? - my $success = $decoded->{success} if defined $decoded; + my $success = 0; + $success = $decoded->{success} if defined $decoded and $decoded->{success}; warn "success is $success" if $debug; # the decoding process seems to change the string "true" to "1" sometimes -- we could enforce this $success = 1 if defined $success and $success eq 'true'; - if ($decoded->{success}==1) { + $success = 1 if $decoded->{execute_reply}->{status} eq 'ok'; + warn "now success is $success because status was ok" if $debug; + if ($success) { my $WEBWORK_variable_non_empty=0; my $sage_WEBWORK_data = $decoded->{execute_reply}{user_expressions}{WEBWORK}{data}{'text/plain'}; warn "sage_WEBWORK_data $sage_WEBWORK_data" if $debug; @@ -387,7 +427,7 @@ END } else { die "Error receiving JSON output from sage: \n$output\n "; } - } elsif ($decoded->{success} == 0 ) { # this might be a syntax error + } elsif ($success == 0 ) { # this might be a syntax error $ret->{error_message} = $decoded->{execute_reply}; # this is a hash. # need a better pretty print method warn ( "IO.pm: Perhaps there was syntax error.", join(" ",%{ $decoded->{execute_reply}})); } else { diff --git a/lib/WeBWorK/PG/Translator.pm b/lib/WeBWorK/PG/Translator.pm index c3028b3b9f..180e038d2f 100644 --- a/lib/WeBWorK/PG/Translator.pm +++ b/lib/WeBWorK/PG/Translator.pm @@ -1334,7 +1334,7 @@ sub process_answers{ if defined($new_rh_ans_evaluation_result) && ref($new_rh_ans_evaluation_result) && defined($new_rh_ans_evaluation_result->error_flag()); } else { - $PG->warning_message(" The evaluated answer is not an answer hash $new_rh_ans_evaluation_result: |".ref($new_rh_ans_evaluation_result)."|."); + $PG->warning_message(" The evaluated answer is not an answer hash ".($new_rh_ans_evaluation_result//'').': |'.ref($new_rh_ans_evaluation_result)."|."); } # $PG->debug_message( $self->{envir}->{'probFileName'} ." new_temp_ans and temp_ans don't agree: ". # ref($new_temp_ans)." $new_temp_ans ". ref($temp_ans). " $temp_ans".length($new_temp_ans).length($temp_ans)) diff --git a/macros/LinearProgramming.pl b/macros/LinearProgramming.pl index 4456f6266a..2efe29866c 100644 --- a/macros/LinearProgramming.pl +++ b/macros/LinearProgramming.pl @@ -290,7 +290,7 @@ sub lp_current_value { if (defined &Fraction) { return Fraction(0); # MathObjects version } else { - return new Fraction(0); # old style Function module version + return Fraction->new(0); # old style Function module version } } else { return 0; diff --git a/macros/PGbasicmacros.pl b/macros/PGbasicmacros.pl index 8edbcce3d9..20727e9271 100644 --- a/macros/PGbasicmacros.pl +++ b/macros/PGbasicmacros.pl @@ -457,11 +457,17 @@ sub NAMED_ANS_RULE_EXTENSION { } else { $label = generate_aria_label($name); } - # this is the name of the parent answer group + # $answer_group_name is the name of the parent answer group + # the group name is usually the same as the answer blank name + # when there is only one answer blank. + + + my $answer_group_name = $options{answer_group_name}//''; unless ($answer_group_name) { WARN_MESSAGE("Error in NAMED_ANSWER_RULE_EXTENSION: every call to this subroutine needs - to have \$options{answer_group_name} defined. Answer blank name: $name"); + to have \$options{answer_group_name} defined. For a single answer blank this is + usually the same as the answer blank name. Answer blank name: $name"); } # warn "from named answer rule extension in PGbasic answer_group_name: |$answer_group_name|"; my $answer_value = ''; diff --git a/macros/scaffold.pl b/macros/scaffold.pl index 0327763d57..da86915214 100644 --- a/macros/scaffold.pl +++ b/macros/scaffold.pl @@ -483,8 +483,8 @@ package Section; # # Shortcuts for Scaffold data # -my $PG_ANSWERS_HASH = $Scaffold::PG_ANSWERS_HASH; -my $PG_OUTPUT = $Scaffold::PG_OUTPUT; +$PG_ANSWERS_HASH = $Scaffold::PG_ANSWERS_HASH; +$PG_OUTPUT = $Scaffold::PG_OUTPUT; # From 9e0a93ce079f4838f2e0517cc5bdaba9085e983b Mon Sep 17 00:00:00 2001 From: Michael Gage Date: Sun, 8 Oct 2017 12:27:13 -0400 Subject: [PATCH 06/30] Commit unit test files --- t/embedded.html | 242 +++++++++++++++++++ t/linebreak_at_commas_example.pg | 169 +++++++++++++ t/matrix_tableau_tests/linebreaks.pg | 168 +++++++++++++ t/matrix_tableau_tests/matrix_test2.pg | 70 ++++++ t/matrix_tableau_tests/print_tableau_Test.pg | 69 ++++++ t/matrix_tableau_tests/tableau_javascript.pg | 133 ++++++++++ t/matrix_tableau_tests/tableau_test2.pg | 79 ++++++ t/test_find_file_in_directories.pl | 43 ++++ 8 files changed, 973 insertions(+) create mode 100644 t/embedded.html create mode 100644 t/linebreak_at_commas_example.pg create mode 100644 t/matrix_tableau_tests/linebreaks.pg create mode 100644 t/matrix_tableau_tests/matrix_test2.pg create mode 100644 t/matrix_tableau_tests/print_tableau_Test.pg create mode 100644 t/matrix_tableau_tests/tableau_javascript.pg create mode 100644 t/matrix_tableau_tests/tableau_test2.pg create mode 100644 t/test_find_file_in_directories.pl diff --git a/t/embedded.html b/t/embedded.html new file mode 100644 index 0000000000..a9229cf777 --- /dev/null +++ b/t/embedded.html @@ -0,0 +1,242 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    + +
    +

    Edit in JSFiddle

    + +
    + +
    +
    +
    + +
    + + + +
             $(function() {
    +            $( "#dialog-1" ).dialog({
    +               autoOpen: false,  
    +            });
    +
    +            var name;
    +            //= $("#dialog-1").attr("name");
    +            
    +            $( ".opener" ).click(function() {
    +               console.log(this.name );
    +               name = this.name
    +               //$("#matrix_input").value= "7 5 4 3 2\n 1 2 3 4 5";
    +               $("textarea#matrix_input").val("7 5 4 3 2\n 1 2 3 4 5");
    +               $( "#dialog-1" ).dialog( "open" );
    +               
    +            });
    +            //console.log("name is " + name );
    +            //console.log("rows="+$("#dialog-1").attr("rows")+ " columns= "+$("#dialog-1").attr("columns"));
    +            var insert_value = function(name, i,j,entry) {
    +            	 var pos = "#MaTrIx_"+name+"_"+i+"_"+j;
    +               if (i==0 && j==0 ) {
    +               	pos= "#"+name;
    +               }  //MaTrIx_AnSwEr0007_0_3
    +								 //console.log($(pos).val());
    +	     				$(pos).val(entry); //instead of 4000
    +  					}
    +    
    +            $( "#closer" ).click(function() {
    +               //var name="AnSwEr0007";
    +               var mat=$("textarea#matrix_input").val();
    +               var mat2=mat.split(/\n/);
    +               var mat3=[];
    +               for (i=0; i<mat2.length; i++) {
    +               		mat3.push( mat2[i].split(/\s+/) );
    +               }
    +               for (i=0; i<mat3.length; i++) {
    +               	  for(j=0; j<mat3[i].length; j++){
    +                  	insert_value(name,i,j,mat3[i][j]);
    +                  }		
    +               }
    +               $( "#dialog-1" ).dialog( "close" );
    +            });
    +         });
    +// dialog is attached to div with id="dialog-1"
    +// it is opened when button with id #opener is clicked
    +// now to add the entry mechanism
    + + + + + +
     <div id="dialog-1" name="AnSwEr0007" title="Enter 5 by 10 matrix"
    + rows="5" columns="10">
    +  <textarea id = "matrix_input" rows="5" columns = "10" value="foobar"> this is supposed to be updated
    +  </textarea>
    +  <button id="closer">Enter
    +  </button>
    + </div>
    +<button class="opener" id="007" name="AnSwEr0007">Quick matrix entry</button>
    +<div>
    +
    +<button class="opener" id="008" name="AnSwEr0008">Quick matrix entry</button>
    +<div>
    +<span class="ans_array" style="display:inline-block;vertical-align:.5ex">
    + <span class="ans_array_open" style="display:inline-block; vertical-align:middle; margin-right:4px"><span class="MathJax_Preview" style="color: inherit;"></span><span class="MathJax" id="MathJax-Element-10-Frame" tabindex="0" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mrow><mo>[</mo><mpadded width=&quot;0pt&quot; height=&quot;6em&quot; depth=&quot;0pt&quot;><mrow /></mpadded><mo fence=&quot;true&quot; stretchy=&quot;true&quot;></mo></mrow></math>" role="presentation" style="position: relative;"><nobr aria-hidden="true"><span class="math" id="MathJax-Span-89" role="math" style="width: 0.598em; display: inline-block;"><span style="display: inline-block; position: relative; width: 0.479em; height: 0px; font-size: 120%;"><span style="position: absolute; clip: rect(2.027em 1000.48em 15.36em -999.997em); top: -8.926em; left: 0.003em;"><span class="mrow" id="MathJax-Span-90"><span class="mrow" id="MathJax-Span-91"><span class="mo" id="MathJax-Span-92" style="vertical-align: 6.729em;"><span style="display: inline-block; position: relative; width: 0.479em; height: 0px;"><span style="position: absolute; font-family: STIXSizeOneSym; top: -3.33em; left: 0.003em;">⎡<span style="display: inline-block; width: 0px; height: 3.991em;"></span></span><span style="position: absolute; font-family: STIXSizeOneSym; top: 8.693em; left: 0.003em;">⎣<span style="display: inline-block; width: 0px; height: 3.991em;"></span></span><span style="font-family: STIXSizeOneSym; position: absolute; top: -2.378em; left: 0.003em;">⎢<span style="display: inline-block; width: 0px; height: 3.991em;"></span></span><span style="font-family: STIXSizeOneSym; position: absolute; top: -1.485em; left: 0.003em;">⎢<span style="display: inline-block; width: 0px; height: 3.991em;"></span></span><span style="font-family: STIXSizeOneSym; position: absolute; top: -0.533em; left: 0.003em;">⎢<span style="display: inline-block; width: 0px; height: 3.991em;"></span></span><span style="font-family: STIXSizeOneSym; position: absolute; top: 0.36em; left: 0.003em;">⎢<span style="display: inline-block; width: 0px; height: 3.991em;"></span></span><span style="font-family: STIXSizeOneSym; position: absolute; top: 1.313em; left: 0.003em;">⎢<span style="display: inline-block; width: 0px; height: 3.991em;"></span></span><span style="font-family: STIXSizeOneSym; position: absolute; top: 2.265em; left: 0.003em;">⎢<span style="display: inline-block; width: 0px; height: 3.991em;"></span></span><span style="font-family: STIXSizeOneSym; position: absolute; top: 3.158em; left: 0.003em;">⎢<span style="display: inline-block; width: 0px; height: 3.991em;"></span></span><span style="font-family: STIXSizeOneSym; position: absolute; top: 4.11em; left: 0.003em;">⎢<span style="display: inline-block; width: 0px; height: 3.991em;"></span></span><span style="font-family: STIXSizeOneSym; position: absolute; top: 5.003em; left: 0.003em;">⎢<span style="display: inline-block; width: 0px; height: 3.991em;"></span></span><span style="font-family: STIXSizeOneSym; position: absolute; top: 5.955em; left: 0.003em;">⎢<span style="display: inline-block; width: 0px; height: 3.991em;"></span></span><span style="font-family: STIXSizeOneSym; position: absolute; top: 6.848em; left: 0.003em;">⎢<span style="display: inline-block; width: 0px; height: 3.991em;"></span></span><span style="font-family: STIXSizeOneSym; position: absolute; top: 7.801em; left: 0.003em;">⎢<span style="display: inline-block; width: 0px; height: 3.991em;"></span></span></span></span><span class="mpadded" id="MathJax-Span-93"><span style="display: inline-block; position: relative; width: 0.003em; height: 0px;"><span style="position: absolute; clip: rect(3.812em 1000em 4.17em -999.997em); top: -3.985em; left: 0.003em;"><span class="mrow" id="MathJax-Span-94"><span class="mrow" id="MathJax-Span-95"></span></span><span style="display: inline-block; width: 0px; height: 3.991em;"></span></span></span></span><span class="mo" id="MathJax-Span-96"></span></span></span><span style="display: inline-block; width: 0px; height: 8.932em;"></span></span></span><span style="display: inline-block; overflow: hidden; vertical-align: -7.568em; border-left: 0px solid; width: 0px; height: 15.718em;"></span></span></nobr><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo>[</mo><mpadded width="0pt" height="6em" depth="0pt"><mrow></mrow></mpadded><mo fence="true" stretchy="true"></mo></mrow></math></span></span><script type="math/tex" id="MathJax-Element-10">\left[\Rule{0pt}{6em}{0pt}\right.</script></span><span class="ans_array_table" style="display:inline-table; vertical-align:middle"><span style="display:table-row"><span class="ans_array_cell" style="display:table-cell;vertical-align:middle;padding:4px 0;">p1</span><span class="ans_array_sep" style="display:table-cell;vertical-align:middle;width:8px"></span><span class="ans_array_cell" style="display:table-cell;vertical-align:middle;padding:4px 0;">p2</span><span class="ans_array_sep" style="display:table-cell;vertical-align:middle;width:8px"></span><span class="ans_array_cell" style="display:table-cell;vertical-align:middle;padding:4px 0;">x3</span><span class="ans_array_sep" style="display:table-cell;vertical-align:middle;width:8px"></span><span class="ans_array_cell" style="display:table-cell;vertical-align:middle;padding:4px 0;">x4</span><span class="ans_array_sep" style="display:table-cell;vertical-align:middle;width:8px"></span><span class="ans_array_cell" style="display:table-cell;vertical-align:middle;padding:4px 0;">x5</span><span class="ans_array_sep" style="display:table-cell;vertical-align:middle;width:8px"></span><span class="ans_array_cell" style="display:table-cell;vertical-align:middle;padding:4px 0;">x6</span><span class="ans_array_sep" style="display:table-cell;vertical-align:middle;width:8px"></span><span class="ans_array_cell" style="display:table-cell;vertical-align:middle;padding:4px 0;">P</span><span class="ans_array_sep" style="display:table-cell;vertical-align:middle;width:8px"></span><span class="ans_array_cell" style="display:table-cell;vertical-align:middle;padding:4px 0;">b</span></span><span style="display:table-row"><span class="ans_array_cell" style="display:table-cell;vertical-align:middle;padding:4px 0;"><input type="text" class="codeshard" size="6" name="AnSwEr0007" id="AnSwEr0007" aria-label="answer 7 row 1 column 1 " value="0">
    +<input type="hidden" name="previous_AnSwEr0007" value="0">
    +</span><span class="ans_array_sep" style="display:table-cell;vertical-align:middle;width:8px"></span><span class="ans_array_cell" style="display:table-cell;vertical-align:middle;padding:4px 0;"><input type="TEXT" size="6" name="MaTrIx_AnSwEr0007_0_1" id="MaTrIx_AnSwEr0007_0_1" class="codeshard" aria-label="answer 7 row 1 column 2 " value="4000">
    +</span><span class="ans_array_sep" style="display:table-cell;vertical-align:middle;width:8px"></span><span class="ans_array_cell" style="display:table-cell;vertical-align:middle;padding:4px 0;"><input type="TEXT" size="6" name="MaTrIx_AnSwEr0007_0_2" id="MaTrIx_AnSwEr0007_0_2" class="codeshard" aria-label="answer 7 row 1 column 3 " value="1">
    +</span><span class="ans_array_sep" style="display:table-cell;vertical-align:middle;width:8px"></span><span class="ans_array_cell" style="display:table-cell;vertical-align:middle;padding:4px 0;"><input type="TEXT" size="6" name="MaTrIx_AnSwEr0007_0_3" id="MaTrIx_AnSwEr0007_0_3" class="codeshard" aria-label="answer 7 row 1 column 4 " value="0">
    +</span><span class="ans_array_sep" style="display:table-cell;vertical-align:middle;width:8px"></span><span class="ans_array_cell" style="display:table-cell;vertical-align:middle;padding:4px 0;"><input type="TEXT" size="6" name="MaTrIx_AnSwEr0007_0_4" id="MaTrIx_AnSwEr0007_0_4" class="codeshard" aria-label="answer 7 row 1 column 5 " value="-5000">
    +</span><span class="ans_array_sep" style="display:table-cell;vertical-align:middle;width:8px"></span><span class="ans_array_cell" style="display:table-cell;vertical-align:middle;padding:4px 0;"><input type="TEXT" size="6" name="MaTrIx_AnSwEr0007_0_5" id="MaTrIx_AnSwEr0007_0_5" class="codeshard" aria-label="answer 7 row 1 column 6 " value="0">
    +</span><span class="ans_array_sep" style="display:table-cell;vertical-align:middle;width:8px"></span><span class="ans_array_cell" style="display:table-cell;vertical-align:middle;padding:4px 0;"><input type="TEXT" size="6" name="MaTrIx_AnSwEr0007_0_6" id="MaTrIx_AnSwEr0007_0_6" class="codeshard" aria-label="answer 7 row 1 column 7 " value="0">
    +</span><span class="ans_array_sep" style="display:table-cell;vertical-align:middle;width:8px"></span><span class="ans_array_cell" style="display:table-cell;vertical-align:middle;padding:4px 0;"><input type="TEXT" size="6" name="MaTrIx_AnSwEr0007_0_7" id="MaTrIx_AnSwEr0007_0_7" class="codeshard" aria-label="answer 7 row 1 column 8 " value="1000">
    +</span></span><span style="display:table-row"><span class="ans_array_cell" style="display:table-cell;vertical-align:middle;padding:4px 0;"><input type="TEXT" size="6" name="MaTrIx_AnSwEr0007_1_0" id="MaTrIx_AnSwEr0007_1_0" class="codeshard" aria-label="answer 7 row 2 column 1 " value="0">
    +</span><span class="ans_array_sep" style="display:table-cell;vertical-align:middle;width:8px"></span><span class="ans_array_cell" style="display:table-cell;vertical-align:middle;padding:4px 0;"><input type="TEXT" size="6" name="MaTrIx_AnSwEr0007_1_1" id="MaTrIx_AnSwEr0007_1_1" class="codeshard" aria-label="answer 7 row 2 column 2 " value="500">
    +</span><span class="ans_array_sep" style="display:table-cell;vertical-align:middle;width:8px"></span><span class="ans_array_cell" style="display:table-cell;vertical-align:middle;padding:4px 0;"><input type="TEXT" size="6" name="MaTrIx_AnSwEr0007_1_2" id="MaTrIx_AnSwEr0007_1_2" class="codeshard" aria-label="answer 7 row 2 column 3 " value="0">
    +</span><span class="ans_array_sep" style="display:table-cell;vertical-align:middle;width:8px"></span><span class="ans_array_cell" style="display:table-cell;vertical-align:middle;padding:4px 0;"><input type="TEXT" size="6" name="MaTrIx_AnSwEr0007_1_3" id="MaTrIx_AnSwEr0007_1_3" class="codeshard" aria-label="answer 7 row 2 column 4 " value="1">
    +</span><span class="ans_array_sep" style="display:table-cell;vertical-align:middle;width:8px"></span><span class="ans_array_cell" style="display:table-cell;vertical-align:middle;padding:4px 0;"><input type="TEXT" size="6" name="MaTrIx_AnSwEr0007_1_4" id="MaTrIx_AnSwEr0007_1_4" class="codeshard" aria-label="answer 7 row 2 column 5 " value="-400">
    +</span><span class="ans_array_sep" style="display:table-cell;vertical-align:middle;width:8px"></span><span class="ans_array_cell" style="display:table-cell;vertical-align:middle;padding:4px 0;"><input type="TEXT" size="6" name="MaTrIx_AnSwEr0007_1_5" id="MaTrIx_AnSwEr0007_1_5" class="codeshard" aria-label="answer 7 row 2 column 6 " value="0">
    +</span><span class="ans_array_sep" style="display:table-cell;vertical-align:middle;width:8px"></span><span class="ans_array_cell" style="display:table-cell;vertical-align:middle;padding:4px 0;"><input type="TEXT" size="6" name="MaTrIx_AnSwEr0007_1_6" id="MaTrIx_AnSwEr0007_1_6" class="codeshard" aria-label="answer 7 row 2 column 7 " value="0">
    +</span><span class="ans_array_sep" style="display:table-cell;vertical-align:middle;width:8px"></span><span class="ans_array_cell" style="display:table-cell;vertical-align:middle;padding:4px 0;"><input type="TEXT" size="6" name="MaTrIx_AnSwEr0007_1_7" id="MaTrIx_AnSwEr0007_1_7" class="codeshard" aria-label="answer 7 row 2 column 8 " value="200">
    +</span></span><span style="display:table-row"><span class="ans_array_cell" style="display:table-cell;vertical-align:middle;padding:4px 0;"><input type="TEXT" size="6" name="MaTrIx_AnSwEr0007_2_0" id="MaTrIx_AnSwEr0007_2_0" class="codeshard" aria-label="answer 7 row 3 column 1 " value="1">
    +</span><span class="ans_array_sep" style="display:table-cell;vertical-align:middle;width:8px"></span><span class="ans_array_cell" style="display:table-cell;vertical-align:middle;padding:4px 0;"><input type="TEXT" size="6" name="MaTrIx_AnSwEr0007_2_1" id="MaTrIx_AnSwEr0007_2_1" class="codeshard" aria-label="answer 7 row 3 column 2 " value="0">
    +</span><span class="ans_array_sep" style="display:table-cell;vertical-align:middle;width:8px"></span><span class="ans_array_cell" style="display:table-cell;vertical-align:middle;padding:4px 0;"><input type="TEXT" size="6" name="MaTrIx_AnSwEr0007_2_2" id="MaTrIx_AnSwEr0007_2_2" class="codeshard" aria-label="answer 7 row 3 column 3 " value="0">
    +</span><span class="ans_array_sep" style="display:table-cell;vertical-align:middle;width:8px"></span><span class="ans_array_cell" style="display:table-cell;vertical-align:middle;padding:4px 0;"><input type="TEXT" size="6" name="MaTrIx_AnSwEr0007_2_3" id="MaTrIx_AnSwEr0007_2_3" class="codeshard" aria-label="answer 7 row 3 column 4 " value="0">
    +</span><span class="ans_array_sep" style="display:table-cell;vertical-align:middle;width:8px"></span><span class="ans_array_cell" style="display:table-cell;vertical-align:middle;padding:4px 0;"><input type="TEXT" size="6" name="MaTrIx_AnSwEr0007_2_4" id="MaTrIx_AnSwEr0007_2_4" class="codeshard" aria-label="answer 7 row 3 column 5 " value="1">
    +</span><span class="ans_array_sep" style="display:table-cell;vertical-align:middle;width:8px"></span><span class="ans_array_cell" style="display:table-cell;vertical-align:middle;padding:4px 0;"><input type="TEXT" size="6" name="MaTrIx_AnSwEr0007_2_5" id="MaTrIx_AnSwEr0007_2_5" class="codeshard" aria-label="answer 7 row 3 column 6 " value="0">
    +</span><span class="ans_array_sep" style="display:table-cell;vertical-align:middle;width:8px"></span><span class="ans_array_cell" style="display:table-cell;vertical-align:middle;padding:4px 0;"><input type="TEXT" size="6" name="MaTrIx_AnSwEr0007_2_6" id="MaTrIx_AnSwEr0007_2_6" class="codeshard" aria-label="answer 7 row 3 column 7 " value="0">
    +</span><span class="ans_array_sep" style="display:table-cell;vertical-align:middle;width:8px"></span><span class="ans_array_cell" style="display:table-cell;vertical-align:middle;padding:4px 0;"><input type="TEXT" size="6" name="MaTrIx_AnSwEr0007_2_7" id="MaTrIx_AnSwEr0007_2_7" class="codeshard" aria-label="answer 7 row 3 column 8 " value="1">
    +</span></span><span style="display:table-row"><span class="ans_array_cell" style="display:table-cell;vertical-align:middle;padding:4px 0;"><input type="TEXT" size="6" name="MaTrIx_AnSwEr0007_3_0" id="MaTrIx_AnSwEr0007_3_0" class="codeshard" aria-label="answer 7 row 4 column 1 " value="0">
    +</span><span class="ans_array_sep" style="display:table-cell;vertical-align:middle;width:8px"></span><span class="ans_array_cell" style="display:table-cell;vertical-align:middle;padding:4px 0;"><input type="TEXT" size="6" name="MaTrIx_AnSwEr0007_3_1" id="MaTrIx_AnSwEr0007_3_1" class="codeshard" aria-label="answer 7 row 4 column 2 " value="1">
    +</span><span class="ans_array_sep" style="display:table-cell;vertical-align:middle;width:8px"></span><span class="ans_array_cell" style="display:table-cell;vertical-align:middle;padding:4px 0;"><input type="TEXT" size="6" name="MaTrIx_AnSwEr0007_3_2" id="MaTrIx_AnSwEr0007_3_2" class="codeshard" aria-label="answer 7 row 4 column 3 " value="0">
    +</span><span class="ans_array_sep" style="display:table-cell;vertical-align:middle;width:8px"></span><span class="ans_array_cell" style="display:table-cell;vertical-align:middle;padding:4px 0;"><input type="TEXT" size="6" name="MaTrIx_AnSwEr0007_3_3" id="MaTrIx_AnSwEr0007_3_3" class="codeshard" aria-label="answer 7 row 4 column 4 " value="0">
    +</span><span class="ans_array_sep" style="display:table-cell;vertical-align:middle;width:8px"></span><span class="ans_array_cell" style="display:table-cell;vertical-align:middle;padding:4px 0;"><input type="TEXT" size="6" name="MaTrIx_AnSwEr0007_3_4" id="MaTrIx_AnSwEr0007_3_4" class="codeshard" aria-label="answer 7 row 4 column 5 " value="0">
    +</span><span class="ans_array_sep" style="display:table-cell;vertical-align:middle;width:8px"></span><span class="ans_array_cell" style="display:table-cell;vertical-align:middle;padding:4px 0;"><input type="TEXT" size="6" name="MaTrIx_AnSwEr0007_3_5" id="MaTrIx_AnSwEr0007_3_5" class="codeshard" aria-label="answer 7 row 4 column 6 " value="1">
    +</span><span class="ans_array_sep" style="display:table-cell;vertical-align:middle;width:8px"></span><span class="ans_array_cell" style="display:table-cell;vertical-align:middle;padding:4px 0;"><input type="TEXT" size="6" name="MaTrIx_AnSwEr0007_3_6" id="MaTrIx_AnSwEr0007_3_6" class="codeshard" aria-label="answer 7 row 4 column 7 " value="0">
    +</span><span class="ans_array_sep" style="display:table-cell;vertical-align:middle;width:8px"></span><span class="ans_array_cell" style="display:table-cell;vertical-align:middle;padding:4px 0;"><input type="TEXT" size="6" name="MaTrIx_AnSwEr0007_3_7" id="MaTrIx_AnSwEr0007_3_7" class="codeshard" aria-label="answer 7 row 4 column 8 " value="1">
    +</span></span><span style="display:table-row"><span class="ans_array_cell" style="display:table-cell;vertical-align:middle;padding:4px 0;"><input type="TEXT" size="6" name="MaTrIx_AnSwEr0007_4_0" id="MaTrIx_AnSwEr0007_4_0" class="codeshard" aria-label="answer 7 row 5 column 1 " value="0">
    +</span><span class="ans_array_sep" style="display:table-cell;vertical-align:middle;width:8px"></span><span class="ans_array_cell" style="display:table-cell;vertical-align:middle;padding:4px 0;"><input type="TEXT" size="6" name="MaTrIx_AnSwEr0007_4_1" id="MaTrIx_AnSwEr0007_4_1" class="codeshard" aria-label="answer 7 row 5 column 2 " value="-4500">
    +</span><span class="ans_array_sep" style="display:table-cell;vertical-align:middle;width:8px"></span><span class="ans_array_cell" style="display:table-cell;vertical-align:middle;padding:4px 0;"><input type="TEXT" size="6" name="MaTrIx_AnSwEr0007_4_2" id="MaTrIx_AnSwEr0007_4_2" class="codeshard" aria-label="answer 7 row 5 column 3 " value="0">
    +</span><span class="ans_array_sep" style="display:table-cell;vertical-align:middle;width:8px"></span><span class="ans_array_cell" style="display:table-cell;vertical-align:middle;padding:4px 0;"><input type="TEXT" size="6" name="MaTrIx_AnSwEr0007_4_3" id="MaTrIx_AnSwEr0007_4_3" class="codeshard" aria-label="answer 7 row 5 column 4 " value="0">
    +</span><span class="ans_array_sep" style="display:table-cell;vertical-align:middle;width:8px"></span><span class="ans_array_cell" style="display:table-cell;vertical-align:middle;padding:4px 0;"><input type="TEXT" size="6" name="MaTrIx_AnSwEr0007_4_4" id="MaTrIx_AnSwEr0007_4_4" class="codeshard" aria-label="answer 7 row 5 column 5 " value="4500">
    +</span><span class="ans_array_sep" style="display:table-cell;vertical-align:middle;width:8px"></span><span class="ans_array_cell" style="display:table-cell;vertical-align:middle;padding:4px 0;"><input type="TEXT" size="6" name="MaTrIx_AnSwEr0007_4_5" id="MaTrIx_AnSwEr0007_4_5" class="codeshard" aria-label="answer 7 row 5 column 6 " value="0">
    +</span><span class="ans_array_sep" style="display:table-cell;vertical-align:middle;width:8px"></span><span class="ans_array_cell" style="display:table-cell;vertical-align:middle;padding:4px 0;"><input type="TEXT" size="6" name="MaTrIx_AnSwEr0007_4_6" id="MaTrIx_AnSwEr0007_4_6" class="codeshard" aria-label="answer 7 row 5 column 7 " value="1">
    +</span><span class="ans_array_sep" style="display:table-cell;vertical-align:middle;width:8px"></span><span class="ans_array_cell" style="display:table-cell;vertical-align:middle;padding:4px 0;"><input type="TEXT" size="6" name="MaTrIx_AnSwEr0007_4_7" id="MaTrIx_AnSwEr0007_4_7" class="codeshard" aria-label="answer 7 row 5 column 8 " value="4500">
    +</span></span></span><span class="ans_array_close" style="display:inline-block; vertical-align:middle; margin-left:4px"><span class="MathJax_Preview" style="color: inherit;"></span><span class="MathJax" id="MathJax-Element-11-Frame" tabindex="0" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mrow><mo>]</mo><mpadded width=&quot;0pt&quot; height=&quot;6em&quot; depth=&quot;0pt&quot;><mrow /></mpadded><mo fence=&quot;true&quot; stretchy=&quot;true&quot;></mo></mrow></math>" role="presentation" style="position: relative;"><nobr aria-hidden="true"><span class="math" id="MathJax-Span-97" role="math" style="width: 0.598em; display: inline-block;"><span style="display: inline-block; position: relative; width: 0.479em; height: 0px; font-size: 120%;"><span style="position: absolute; clip: rect(2.027em 1000.48em 15.36em -999.997em); top: -8.926em; left: 0.003em;"><span class="mrow" id="MathJax-Span-98"><span class="mrow" id="MathJax-Span-99"><span class="mo" id="MathJax-Span-100" style="vertical-align: 6.729em;"><span style="display: inline-block; position: relative; width: 0.479em; height: 0px;"><span style="position: absolute; font-family: STIXSizeOneSym; top: -3.33em; left: 0.003em;">⎤<span style="display: inline-block; width: 0px; height: 3.991em;"></span></span><span style="position: absolute; font-family: STIXSizeOneSym; top: 8.693em; left: 0.003em;">⎦<span style="display: inline-block; width: 0px; height: 3.991em;"></span></span><span style="font-family: STIXSizeOneSym; position: absolute; top: -2.378em; left: 0.003em;">⎥<span style="display: inline-block; width: 0px; height: 3.991em;"></span></span><span style="font-family: STIXSizeOneSym; position: absolute; top: -1.485em; left: 0.003em;">⎥<span style="display: inline-block; width: 0px; height: 3.991em;"></span></span><span style="font-family: STIXSizeOneSym; position: absolute; top: -0.533em; left: 0.003em;">⎥<span style="display: inline-block; width: 0px; height: 3.991em;"></span></span><span style="font-family: STIXSizeOneSym; position: absolute; top: 0.36em; left: 0.003em;">⎥<span style="display: inline-block; width: 0px; height: 3.991em;"></span></span><span style="font-family: STIXSizeOneSym; position: absolute; top: 1.313em; left: 0.003em;">⎥<span style="display: inline-block; width: 0px; height: 3.991em;"></span></span><span style="font-family: STIXSizeOneSym; position: absolute; top: 2.265em; left: 0.003em;">⎥<span style="display: inline-block; width: 0px; height: 3.991em;"></span></span><span style="font-family: STIXSizeOneSym; position: absolute; top: 3.158em; left: 0.003em;">⎥<span style="display: inline-block; width: 0px; height: 3.991em;"></span></span><span style="font-family: STIXSizeOneSym; position: absolute; top: 4.11em; left: 0.003em;">⎥<span style="display: inline-block; width: 0px; height: 3.991em;"></span></span><span style="font-family: STIXSizeOneSym; position: absolute; top: 5.003em; left: 0.003em;">⎥<span style="display: inline-block; width: 0px; height: 3.991em;"></span></span><span style="font-family: STIXSizeOneSym; position: absolute; top: 5.955em; left: 0.003em;">⎥<span style="display: inline-block; width: 0px; height: 3.991em;"></span></span><span style="font-family: STIXSizeOneSym; position: absolute; top: 6.848em; left: 0.003em;">⎥<span style="display: inline-block; width: 0px; height: 3.991em;"></span></span><span style="font-family: STIXSizeOneSym; position: absolute; top: 7.801em; left: 0.003em;">⎥<span style="display: inline-block; width: 0px; height: 3.991em;"></span></span></span></span><span class="mpadded" id="MathJax-Span-101"><span style="display: inline-block; position: relative; width: 0.003em; height: 0px;"><span style="position: absolute; clip: rect(3.812em 1000em 4.17em -999.997em); top: -3.985em; left: 0.003em;"><span class="mrow" id="MathJax-Span-102"><span class="mrow" id="MathJax-Span-103"></span></span><span style="display: inline-block; width: 0px; height: 3.991em;"></span></span></span></span><span class="mo" id="MathJax-Span-104"></span></span></span><span style="display: inline-block; width: 0px; height: 8.932em;"></span></span></span><span style="display: inline-block; overflow: hidden; vertical-align: -7.568em; border-left: 0px solid; width: 0px; height: 15.718em;"></span></span></nobr><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo>]</mo><mpadded width="0pt" height="6em" depth="0pt"><mrow></mrow></mpadded><mo fence="true" stretchy="true"></mo></mrow></math></span></span><script type="math/tex" id="MathJax-Element-11">\left]\Rule{0pt}{6em}{0pt}\right.</script></span></span>
    +
    +</div>
    + + + + + +
    
    +              
    +            
    +          
    +            
    + + + +
    + +
    + + + + + + + + + + \ No newline at end of file diff --git a/t/linebreak_at_commas_example.pg b/t/linebreak_at_commas_example.pg new file mode 100644 index 0000000000..6b2ab9d0ae --- /dev/null +++ b/t/linebreak_at_commas_example.pg @@ -0,0 +1,169 @@ +##DESCRIPTION +## Linear programming problem +##ENDDESCRIPTION + + +## DBsubject(Operations research) +## DBchapter(Linear programming) +## DBsection(Simplex method) +## Date(9/11/2013) +## Institution(U. of Rochester) +## Author(M. Gage) +## Level(1) +## KEYWORDS('algebra', 'inequality', 'fraction') + +######################################################################## + +DOCUMENT(); + +loadMacros( + "PGstandard.pl", # Standard macros for PG language + "MathObjects.pl", + "parserLinearInequality.pl", + "parserPopUp.pl", + "scaffold.pl", + "tableau.pl", + #"source.pl", # allows code to be displayed on certain sites. + "PGcourse.pl", # Customization file for the course +); + +# Print problem number and point value (weight) for the problem + +loadMacros( "PGML.pl",); # need to load after changing columns. + +TEXT(beginproblem()); + + +# Show which answers are correct and which ones are incorrect +$showPartialCorrectAnswers = 1; +Context("LinearInequality"); +Context()->variables->add( m2ny=>'Real', m2cal=>'Real',k2ny=>'Real',k2cal=>'Real'); +############################################################## +# +# Setup +# +# +#data + + +#data duckwheat problem +$kansas = 15; # schnupells produced +$mexico = 8; # schnupells produced +$newyork = 10; # schnupells consumed +$california = 13; +$mexico2newyork = 4; # transportation cost per shnupell +$mexico2california = 1; # transportation cost per shnupell +$kansas2newyork = 2; # transportation cost per shnupell +$kansas2california = 3; # transportation cost per shnupell + +# objective function +$popup = PopUp([qw(? Maximize Minimize)], 'Minimize'); +$objfunction = Compute("$mexico2newyork*m2ny + +$mexico2california*m2cal + $kansas2newyork*k2ny + +$kansas2california*k2cal ")->reduce; +$objfunction->{limits}=[[0,1],[2,5],[8,10],[11,12]]; +$objfunction->{checkUndefinedPoints}=1; +$objfunction2 = Formula("5*m2cal")->reduce; +$objfunction2->{checkUndefinedPoints}=1; +#constraints +$constraint0 = Compute(" k2ny + k2cal +<=$kansas"); +$constraint1 = Compute(" m2ny + m2cal +<=$mexico"); +$constraint2 = Compute(" m2ny + k2ny +>=$newyork"); +$constraint3 = Compute(" m2cal + k2cal +>=$california"); + +$constraints = List($constraint0, $constraint1,$constraint2, $constraint3); + + +#variable order: (m2ny, m2cal, k2ny, k2cal) +#matrix for normal form +$constraint_matrix = Matrix("[[ 0, 0, -1, -1], + [-1, -1, 0, 0 ], + [1, 0 , 1 , 0], + [0, 1, 0, 1]] +"); +$m = Matrix([['3',6,7],[2,1,8],[4,6,21],[-6,7,9]]); + +$rhs = Vector($kansas, $mexico, $newyork, $california); +############################################################## +# +# Text +# +# + +####################### +# scaffold + +# use linebreak_at_commas from tableau +# sub linebreak_at_commas { +# return sub { +# my $ans=shift; +# my $foo = $ans->{correct_ans_latex_string}; +# $foo =~ s/,/,~~~~~~~~/g; +# ($ans->{correct_ans_latex_string})=~ s/,/,~~~~~~~~/g; +# ($ans->{preview_latex_string})=~ s/,/,~~~~~~~~/g; +# #DEBUG_MESSAGE("foo", $foo); +# #DEBUG_MESSAGE( "correct", $ans->{correct_ans_latex_string} ); +# #DEBUG_MESSAGE( "preview", $ans->{preview_latex_string} ); +# #DEBUG_MESSAGE("section4ans1 ", pretty_print($ans, $displayMode)); +# $ans; +# }; +# } + +$foochecker = $constraints->cmp()->withPostFilter( +linebreak_at_commas() +); +BEGIN_PGML +Another word problem. Write out the equations for the LOP and the dual LOP. + +Duckwheat is produced in Kansas and Mexico and consumed in New York +and California. +Kansas produces [$kansas] shnupells of duckwheat and [$mexico]. +Meanwhile, New York consumes +[$newyork] shnupells and California [$california]. +The transportation costs per shnupell are [$mexico2newyork] +from Mexico to New York, [$mexico2california] from Mexico to California, +[$kansas2newyork] from Kansas to New York, and +[$kansas2california] and from Kansas to California. + +Write a linear program that decides the amounts of duckwheat +(in shnupells and fractions +of a shnupell) to be transported from each producer to each +consumer, so as to minimize +the overall transportation cost. + (In other words express this problem in a mathematical normal form.) + +* Write the objective function for the problem in terms of the +variables [`m2ny, m2cal, k2ny,k2cal`] which are +the volumes in shnupells for shipped from Mexico to New York, +Mexico to California, Kansas to New York and +Kansas to California in order to obtain the optimum +transportation cost. + +[@ ANS($popup->cmp), $popup->menu @]* +[`transportation cost = `] [@ ANS($objfunction->cmp()), ans_box(2,60) @]* + +* Now write the constraints for the mathematical linear +optimization problem (LOP) in standard form. +Separate each +of the constraint equations by a comma. The order of the constraint equations does not matter. + +[@ ANS($constraints->cmp()->withPostFilter( +linebreak_at_commas() ) +), ans_box(8, 80) @]* + +The variables must be non-negative: [`m2ny, m2cal, k2ny,k2cal\ge 0`] +but you don't need to include these conditions. + + + +END_PGML + + +# Comments used: ($ans->{correct_ans_latex_string})=~ s/,/,\\\\/g; inside PGML +# Used ($ans->{correct_ans_latex_string})=~ s/,/,~~~~~~~~/g; outside PGML + +ENDDOCUMENT(); \ No newline at end of file diff --git a/t/matrix_tableau_tests/linebreaks.pg b/t/matrix_tableau_tests/linebreaks.pg new file mode 100644 index 0000000000..d336c714a2 --- /dev/null +++ b/t/matrix_tableau_tests/linebreaks.pg @@ -0,0 +1,168 @@ +##DESCRIPTION +## Linear programming problem +##ENDDESCRIPTION + + +## DBsubject(Operations research) +## DBchapter(Linear programming) +## DBsection(Simplex method) +## Date(9/11/2013) +## Institution(U. of Rochester) +## Author(M. Gage) +## Level(1) +## KEYWORDS('algebra', 'inequality', 'fraction') + +######################################################################## + +DOCUMENT(); + +loadMacros( + "PGstandard.pl", # Standard macros for PG language + "MathObjects.pl", + "parserLinearInequality.pl", + "parserPopUp.pl", + "scaffold.pl", + "tableau.pl", + #"source.pl", # allows code to be displayed on certain sites. + "PGcourse.pl", # Customization file for the course +); + +# Print problem number and point value (weight) for the problem + +loadMacros( "PGML.pl",); # need to load after changing columns. + +TEXT(beginproblem()); + + +# Show which answers are correct and which ones are incorrect +$showPartialCorrectAnswers = 1; +Context("LinearInequality"); +Context()->variables->add( m2ny=>'Real', m2cal=>'Real',k2ny=>'Real',k2cal=>'Real'); +############################################################## +# +# Setup +# +# +#data + + +#data duckwheat problem +$kansas = 15; # schnupells produced +$mexico = 8; # schnupells produced +$newyork = 10; # schnupells consumed +$california = 13; +$mexico2newyork = 4; # transportation cost per shnupell +$mexico2california = 1; # transportation cost per shnupell +$kansas2newyork = 2; # transportation cost per shnupell +$kansas2california = 3; # transportation cost per shnupell + +# objective function +$popup = PopUp([qw(? Maximize Minimize)], 'Minimize'); +$objfunction = Compute("$mexico2newyork*m2ny + +$mexico2california*m2cal + $kansas2newyork*k2ny + +$kansas2california*k2cal ")->reduce; +$objfunction->{limits}=[[0,1],[2,5],[8,10],[11,12]]; +$objfunction->{checkUndefinedPoints}=1; +$objfunction2 = Formula("5*m2cal")->reduce; +$objfunction2->{checkUndefinedPoints}=1; +#constraints +$constraint0 = Compute(" k2ny + k2cal +<=$kansas"); +$constraint1 = Compute(" m2ny + m2cal +<=$mexico"); +$constraint2 = Compute(" m2ny + k2ny +>=$newyork"); +$constraint3 = Compute(" m2cal + k2cal +>=$california"); + +$constraints = List($constraint0, $constraint1,$constraint2, $constraint3); + + +#variable order: (m2ny, m2cal, k2ny, k2cal) +#matrix for normal form +$constraint_matrix = Matrix("[[ 0, 0, -1, -1], + [-1, -1, 0, 0 ], + [1, 0 , 1 , 0], + [0, 1, 0, 1]] +"); +$m = Matrix([['3',6,7],[2,1,8],[4,6,21],[-6,7,9]]); + +$rhs = Vector($kansas, $mexico, $newyork, $california); +############################################################## +# +# Text +# +# + +####################### +# scaffold + +#sub linebreak_at_commas { +# return sub { +# my $ans=shift; +# my $foo = $ans->{correct_ans_latex_string}; +# $foo =~ s/,/,~~~~~~~~/g; +# ($ans->{correct_ans_latex_string})=~ s/,/,~~~~~~~~/g; +# ($ans->{preview_latex_string})=~ s/,/,~~~~~~~~/g; +# #DEBUG_MESSAGE("foo", $foo); +# #DEBUG_MESSAGE( "correct", $ans->{correct_ans_latex_string} ); +# #DEBUG_MESSAGE( "preview", $ans->{preview_latex_string} ); +# #DEBUG_MESSAGE("section4ans1 ", pretty_print($ans, $displayMode)); +# $ans; +# }; +#} + +$foochecker = $constraints->cmp()->withPostFilter( +linebreak_at_commas() +); +BEGIN_PGML +Another word problem. Write out the equations for the LOP and the dual LOP. + +Duckwheat is produced in Kansas and Mexico and consumed in New York +and California. +Kansas produces [$kansas] shnupells of duckwheat and [$mexico]. +Meanwhile, New York consumes +[$newyork] shnupells and California [$california]. +The transportation costs per shnupell are [$mexico2newyork] +from Mexico to New York, [$mexico2california] from Mexico to California, +[$kansas2newyork] from Kansas to New York, and +[$kansas2california] and from Kansas to California. + +Write a linear program that decides the amounts of duckwheat +(in shnupells and fractions +of a shnupell) to be transported from each producer to each +consumer, so as to minimize +the overall transportation cost. + (In other words express this problem in a mathematical normal form.) + +* Write the objective function for the problem in terms of the +variables [`m2ny, m2cal, k2ny,k2cal`] which are +the volumes in shnupells for shipped from Mexico to New York, +Mexico to California, Kansas to New York and +Kansas to California in order to obtain the optimum +transportation cost. + +[@ ANS($popup->cmp), $popup->menu @]* +[`transportation cost = `] [@ ANS($objfunction->cmp()), ans_box(2,60) @]* + +* Now write the constraints for the mathematical linear +optimization problem (LOP) in standard form. +Separate each +of the constraint equations by a comma. The order of the constraint equations does not matter. + +[@ ANS($constraints->cmp()->withPostFilter( +linebreak_at_commas() ) +), ans_box(8, 80) @]* + +The variables must be non-negative: [`m2ny, m2cal, k2ny,k2cal\ge 0`] +but you don't need to include these conditions. + + + +END_PGML + + +# Comments used: ($ans->{correct_ans_latex_string})=~ s/,/,\\\\/g; inside PGML +# Used ($ans->{correct_ans_latex_string})=~ s/,/,~~~~~~~~/g; outside PGML + +ENDDOCUMENT(); \ No newline at end of file diff --git a/t/matrix_tableau_tests/matrix_test2.pg b/t/matrix_tableau_tests/matrix_test2.pg new file mode 100644 index 0000000000..b4822b4f02 --- /dev/null +++ b/t/matrix_tableau_tests/matrix_test2.pg @@ -0,0 +1,70 @@ + + +############################################## +DOCUMENT(); + +loadMacros( + "PGstandard.pl", # Standard macros for PG language + "MathObjects.pl", + "parserLinearInequality.pl", + "PGML.pl", + "tableau.pl", + "PGmatrixmacros.pl", + "LinearProgramming.pl", + #"source.pl", # allows code to be displayed on certain sites. + "PGcourse.pl", +); + +############################################## + +Context("Matrix"); # need Matrix context to allow string input into Matrix. + +$m = Matrix("[[3,6,7],[2,1,8],[4,6,21],[-6,7,9]]"); + +$v = ColumnVector(1,1,1); + +$m2 = $m*$v; + +$v2 = Vector($m2); + +$m2flat = Matrix( Vector($m*$v) ); +$v2c = ColumnVector($m2); + +$w = Vector(1,1,1,1); +$m3 = $w*$m; + +TEXT("#################",$BR); +@test_rows = map {" ".ref($_)." "} @{$m->extract_rows} ; +TEXT("extracted rows ", scalar(@test_rows), " | ",@test_rows,"|",$BR ); +@test_columns = map {" ".ref($_)." "} @{$m->extract_columns} ; +TEXT("extracted columns ", scalar(@test_columns), "|",join("|", @test_columns),"|",$BR ); + +TEXT($BR,"#################",$BR); +$rows = List($m->extract_rows); +$columns = List( map {ColumnVector($_)} @{$m->extract_columns}); +$columns2 = List( map {$_->transpose} @{$m->extract_columns}); # both of these work + +BEGIN_PGML +matrix [`[$m]`] + +vector [`[$v]`] + +result [`[$m]`]*[`[$v]`] is [$m2] and tex version: [`[$m2]`] + +Convert the result to a vector [`[$v2]`] or to a column vector [`[$v2c]`] + +and then convert to a matrix again [$m2flat] [`[$m2flat]`] + +Multiplication on the left of [`[$w]`] * [`[$m]`] is [$m3], + +the first row of which is [$m3->row(1)] and the tex version:[`[$m3]`] + +Extract rows [@ List($m->extract_rows) @] with tex version [` [$rows] `] + +Extract columns [@ List($m->extract_columns) @] with tex version + +[`[$columns]`] or [`[$columns2]`] + +END_PGML + +ENDDOCUMENT(); \ No newline at end of file diff --git a/t/matrix_tableau_tests/print_tableau_Test.pg b/t/matrix_tableau_tests/print_tableau_Test.pg new file mode 100644 index 0000000000..0ab1abddfc --- /dev/null +++ b/t/matrix_tableau_tests/print_tableau_Test.pg @@ -0,0 +1,69 @@ + + + +############################################## +DOCUMENT(); + +loadMacros( + "PGstandard.pl", # Standard macros for PG language + "MathObjects.pl", + "parserLinearInequality.pl", + "PGML.pl", + "tableau.pl", + "PGmatrixmacros.pl", + "LinearProgramming.pl", + #"source.pl", # allows code to be displayed on certain sites. + "PGcourse.pl", +); + +############################################## + +Context("Matrix"); # need Matrix context to allow string input into Matrix. + +$m = Matrix("[[3,6,7],[2,1,8],[4,6,21],[-6,7,9]]"); +$constraint_matrix = Matrix(" +[[ 0, 0, -1, -1], + [-1, -1, 0, 0 ], + [1, 0 , 1 , 0], + [0, 1, 0, 1]] +"); + +#TEXT ("created ". ref($m)); +#what are the best ways to display a matrix? + +$m1 = matrix_from_matrix_rows($m, [1,3,2]); +$m2 = matrix_from_matrix_cols($m, [3,2,1]); +$m3 = matrix_from_submatrix($m, rows=>[1,4], columns=>[1,2]); + +$list = matrix_rows_to_list($m, 2,3); + +$b = Matrix([1, 2, 3, 4]); +TEXT($BR, "vector", $b->data->[1]); +$c = Matrix([5, 6, 7]); +$t = Tableau->new(A=>$m,b=>$b, c=>$c); + +$basis2 = $t->basis(1,3,5,6); + +BEGIN_PGML + +[` [$m] `] + +Extract rows (1, 4 ) from Matrix + +[` [$m1] `] + + Extract the submatrix of rows (1,4) and columns (1, 2, 3) + + new submatrix is + [` [$m3] `] + +a list of rows 2 and 3 [`[$list]`] + +A matrix from rows (1,3,2) [`[$m1]`] +A matrix from cols (3,2,1) [`[$m2]`] + +A new matrix [`[$b]`] +END_PGML + +ENDDOCUMENT(); + diff --git a/t/matrix_tableau_tests/tableau_javascript.pg b/t/matrix_tableau_tests/tableau_javascript.pg new file mode 100644 index 0000000000..69a666c60f --- /dev/null +++ b/t/matrix_tableau_tests/tableau_javascript.pg @@ -0,0 +1,133 @@ +##DESCRIPTION + + + +##ENDDESCRIPTION + + +DOCUMENT(); # This should be the first executable line in the problem. + +loadMacros( + "PGstandard.pl", # Standard macros for PG language + "MathObjects.pl", + "AppletObjects.pl", + "PGML.pl", + "PGmatrixmacros.pl", + "LinearProgramming.pl", + "parserLinearInequality.pl", + #"gage_matrix_ops.pl", + "tableau.pl", + "quickMatrixEntry.pl", + #"source.pl", # used to display problem source button + "PGcourse.pl", # Customization file for the course +); + +TEXT(beginproblem()); +$showPartialCorrectAnswers = 1; + + + +#BEGIN_TEXT +# + +#
    +# +# +#
    +#END_TEXT + +INITIALIZE_QUICK_MATRIX_ENTRY(); + +############################################################## +# +# Setup +# +# +Context("Numeric"); +# Your resources: +$money_total = 6000; +$time_total = 600; + +# Bill +$bill_money_commitment = 5000; #dollars +$bill_time_commitment = 400; # hours +$bill_profit = 4500; +# Steve +$steve_money_commitment = 4000; +$steve_time_commitment = 500; +$steve_profit = 4500; + +Context()->variables->add(p1=>'Real',p2=>'Real'); +$objfun1 = Formula("${bill_profit}p1 + ${steve_profit}p2"); +#Hack to prevent domain conflict in answer. +# why can't the formula be defined within context "linearInequality"? + +Context("LinearInequality"); +Context()->variables->add(p1=>'Real',p2=>'Real'); +Context()->strings->add("Essay Answer" =>{}); +Context()->strings->add('Minimize'=>{},'Maximize'=>{}, "?"=>{}); +Context()->strings->add('Yes'=>{},'No'=>{}); +our $context=Context(); + +$original_matrix = Matrix([ +[$bill_money_commitment, $steve_money_commitment, 1, 0, 0,0,0, $money_total], +[$bill_time_commitment,$steve_time_commitment, 0, 1, 0,0,0, $time_total], +[1,0,0,0,1,0,0,1], +[0,1,0,0,0,1,0,1], +[-$bill_profit, -$steve_profit, 0, 0, 0,0,1, 0] +]); +$toplabels = [qw(p1 p2 x3 x4 x5 x6 P b)]; +$sidelabels = [' ', qw(cash hours p_1bound p_2obund objfunc) ]; +$matrix1 = $original_matrix; + + + +############################################################################## +# get information on current state +$tableau1 = $matrix1->wwMatrix->array_ref; # translate to the array reference +$basis1 = Set(3,4,5,6); +@statevars1 = get_tableau_variable_values($matrix1, $basis1); +# get z value +$statevars1 = ~~@statevars1; +$state1 = Matrix([[@statevars1]]); + +$matrix1->{top_labels}=$toplabels; +Context()->texStrings; + +BEGIN_TEXT + +Write the matrix/tableau representing the linear optimization problem above. Use +the convention that the objective function is listed on the bottom row and the coefficient in +front of the profit \(P\) is \(1\) or equivalently in the form \( -ax_1 -bx_2 +z = 0 \) +$PAR +We'll use x3 for the slack variable for the money constraint, x4 for the time constraint +slack variable and x5 and x6 for the slack variables for the contraints on p1 and p2. +\{MATRIX_ENTRY_BUTTON(1,5,9)\} +\{ANS($matrix1->cmp()), $matrix1->ans_array()\} + +\{MATRIX_ENTRY_BUTTON(2,5,7)\} +\{ANS($matrix1->cmp()), $matrix1->ans_array()\} +$PAR +END_TEXT + +BEGIN_SOLUTION +displayMode $displayMode $PAR +\[ \{lp_display_mm($matrix1, top_labels=>$toplabels).side_labels($sidelabels)\} \] + +END_SOLUTION +Context()->normalStrings; + + + +############################################################## +# +# Answers +# +# + + + +ENDDOCUMENT(); # This should be the last executable line in the problem. \ No newline at end of file diff --git a/t/matrix_tableau_tests/tableau_test2.pg b/t/matrix_tableau_tests/tableau_test2.pg new file mode 100644 index 0000000000..014ddcdc78 --- /dev/null +++ b/t/matrix_tableau_tests/tableau_test2.pg @@ -0,0 +1,79 @@ +# $b = Matrix([1, 2, 3, 4]); +# TEXT($BR, "vector", $b->data->[1]); +# $c = Matrix([5, 6, 7]); +# $t = Tableau->new(A=>$m,b=>$b, c=>$c); +# @tab = $t->assemble_matrix; +# warn($BR, "rows", join(' ', @{$tab[0]},"|",@{$tab[1]},"|", @{$tab[2]},"|", @{$tab[3]},"|", @{$tab[4]})); +# +# +# +# my @slice = ( 1..(($t->{M}->dimensions)[0]) ); +# DEBUG_MESSAGE("slice is ", @slice); +# +# +# my @matrix_rows = $t->{M}->extract_rows(); +# push @matrix_rows, $t->{obj_row}; +# $m4 = Matrix(@matrix_rows); +# +# $mod_obj_row = $t->{obj_row} ; +# + +# Find the column slice of columns 3,2,1 of the matrix [`[$m2]`] +# +# Output rows 2 and 3 of the matrix as a list [` [$list] `] +# +# Output the complete Tableau (A |S |b): [`[$t->{M}]`] +# +# Output the last row (the objective row): [`[@ Matrix($t->{obj_row}) @]`] +# +# Include the last row [` [$m4] `] +# +# Output the initial basis [`[@ List($t->{basis}) @]`] and the basis columns B: [` [$t->{B}] `] +# +# END_PGML +# TEXT("#############"); +# $basis2 = $t->basis(1,3,5,6); +# $obj_function_basis_coeff = List( matrix_extract_columns($t->{obj_row}, $t->{basis} ) ) ; +# +# @r1 = matrix_extract_columns($t->{obj_row}, $t->{basis}); +# $rtest = List(@r1); +# $r2 = Vector( Vector(@r1)*($t->{M}) ) ; +# TEXT("#############"); +# BEGIN_PGML +# set a new basis [`[$basis2]`] and print the new columns B [` [$t->{B}] `] +# +# the new basis is [$t->basis] and the basis coefficients using $t->current_state are [$t->current_state]. +# This [$obj_function_basis_coeff] should be the same as above. +# +# and this should be identical [@ List(@r1) @] +# +# +# final row [`[$t->{obj_row}]`] +# modifications for final row [` [$r2] `] +# modified final row [` [@ ($t->{obj_row}) - $r2 @] `] +# +# END_PGML +# +# TEXT("*********************"); +# +# +# TEXT("*********************"); +# +# +# BEGIN_PGML +# original tableau = [`[$t->{M}]`] +# +# original object row [`[$t->{obj_row}]`] +# +# here are the two together [`[$m4]`] +# +# +# change basis to [`[$t->basis]`] +# +# print new basis [`[$t->{B}]`] +# +# new tableau is [`[$t->current_state]`] +# +# new obj_row is [`[$t->{current_coeff}]`] +# +# END_PGML \ No newline at end of file diff --git a/t/test_find_file_in_directories.pl b/t/test_find_file_in_directories.pl new file mode 100644 index 0000000000..acd51a12d4 --- /dev/null +++ b/t/test_find_file_in_directories.pl @@ -0,0 +1,43 @@ +#!/Volumes/WW_test/opt/local/bin/perl -w + + +use strict; + + +BEGIN { + die "WEBWORK_ROOT not found in environment. \n + WEBWORK_ROOT can be defined in your .cshrc or .bashrc file\n + It should be set to the webwork2 directory (e.g. /opt/webwork/webwork2)" + unless exists $ENV{WEBWORK_ROOT}; + # Unused variable, but define it twice to avoid an error message. + $WeBWorK::Constants::WEBWORK_DIRECTORY = $ENV{WEBWORK_ROOT}; + + # Define MP2 -- this would normally be done in webwork.apache2.4-config + $ENV{MOD_PERL_API_VERSION}=2; + print "Webwork root directory is $WeBWorK::Constants::WEBWORK_DIRECTORY\n\n"; + + + $WebworkBase::courseName = "gage_test"; + my $topDir = $WeBWorK::Constants::WEBWORK_DIRECTORY; + $topDir =~ s|webwork2?$||; # remove webwork2 link + $WebworkBase::RootWebwork2Dir = "$topDir/webwork2"; + $WebworkBase::RootPGDir = "$topDir/pg"; + $WebworkBase::RootCourseDir = "${topDir}courses"; + + eval "use lib '$WebworkBase::RootWebwork2Dir/lib'"; die $@ if $@; + eval "use lib '$WebworkBase::RootPGDir/lib'"; die $@ if $@; +} + use PGalias; + + my $file = "prob14.html"; + my @directories = ( + "$WebworkBase::RootCourseDir/$WebworkBase::courseName/templates/setaliasCheck/htmlAliasCheck", + "$WebworkBase::RootCourseDir/$WebworkBase::courseName/html", + "$WebworkBase::RootWebwork2Dir/htdocs", + ); + my $file_path = PGalias->find_file_in_directories($file, \@directories)//'not found'; + print "File found at: $file_path\n"; + + + +1; \ No newline at end of file From 8c636d74cbafdcefd204db81147289b3c40a8a13 Mon Sep 17 00:00:00 2001 From: Michael Gage Date: Sun, 8 Oct 2017 14:36:58 -0400 Subject: [PATCH 07/30] Resolve merge --- macros/tableau.pl | 89 +++-------------------------------------------- 1 file changed, 5 insertions(+), 84 deletions(-) diff --git a/macros/tableau.pl b/macros/tableau.pl index 5bee2f2144..a60a8ce7a0 100755 --- a/macros/tableau.pl +++ b/macros/tableau.pl @@ -6,7 +6,7 @@ ##### From gage_matrix_ops # 2014_HKUST_demo/templates/setSequentialWordProblem/bill_and_steve.pg:"gage_matrix_ops.pl", -<<<<<<< HEAD + =head1 Tableaus and matrices # We're going to have several types @@ -156,26 +156,6 @@ =head3 Package Tableau (eventually package Matrix?) Parameter:(rows=>\@row_slice,columns=>\@column_slice) Return: MathObject matrix - -======= -=head3 Matrix extraction mechanisms - - matrix_column_slice (was matrix_from_matrix_cols) - - matrix_row_slice (was matrix_from_matrix_rows) - - matrix_extract_submatrix (was matrix_from_submatrix) - - matrix_extract_rows - - matrix_extract_columns - - matrix_columns_to_List - - matrix_rows_to_List - -Many of these duplicate methods of Value::Matrix -- refactor. ->>>>>>> develop_plus_tableau =cut @@ -270,11 +250,7 @@ sub matrix_extract_columns { # Calculates the values of the basis variables of the tableau, # assuming the parameter variables are 0. # -<<<<<<< HEAD # Usage: ARRAY = get_tableau_variable_values($MathObjectMatrix_tableau, $MathObjectSet_basis) -======= -# Usage: get_tableau_variable_values($MathObjectMatrix_tableau, $MathObjectSet_basis) ->>>>>>> develop_plus_tableau # # feature request -- for tableau object -- allow specification of non-zero parameter variables sub get_tableau_variable_values { @@ -288,26 +264,16 @@ sub get_tableau_variable_values { #DEBUG_MESSAGE( "start new matrix"); foreach my $j (1..$m-2) { # the last two columns of the tableau are object variable and constants if (not $basis->contains($j)) { -<<<<<<< HEAD DEBUG_MESSAGE( "j= $j not in basis"); # set the parameter values to zero -======= - #DEBUG_MESSAGE( "j= $j not in basis"); ->>>>>>> develop_plus_tableau - $var[$j-1]=0; next; # non-basis variables (parameters) are set to 0. + $var[$j-1]=0; next; # non-basis variables (parameters) are set to 0. } else { foreach my $i (1..$n-1) { # the last row is the objective function # if this is a basis column there should be only one non-zero element(the pivot) -<<<<<<< HEAD if ( $mat->element($i,$j)->value != 0 ) { # should this have ->value????? $var[$j-1] = ($mat->element($i,$m)/($mat->element($i,$j))->value); DEBUG_MESSAGE("i=$i j=$j var = $var[$j-1] "); # calculate basis variable value -======= - if ( not $mat->element($i,$j) == 0 ) { # should this have ->value????? - $var[$j-1] = ($mat->element($i,$m)/$mat->element($i,$j))->value; - #DEBUG_MESSAGE("i=$i j=$j var = $var[$j-1] "); ->>>>>>> develop_plus_tableau - next; + next; } } @@ -315,12 +281,7 @@ sub get_tableau_variable_values { } # element($n, $m-1) is the coefficient of the objective value. # this last variable is the value of the objective function push @var , ($mat->element($n,$m)/$mat->element($n,$m-1))->value; - -<<<<<<< HEAD - return wantarray ? @var : \@var; -======= - @var; ->>>>>>> develop_plus_tableau + return wantarray ? @var : \@var; # return either array or reference to an array } #### Test -- assume matrix is this # 1 2 1 0 0 | 0 | 3 @@ -340,24 +301,16 @@ sub get_tableau_variable_values { sub lp_basis_pivot { my ($old_tableau,$old_basis,$pivot) = @_; # $pivot is a Value::Point my $new_tableau= lp_clone($old_tableau); -<<<<<<< HEAD # lp_pivot has 0 based indices main::lp_pivot($new_tableau, $pivot->extract(1)-1,$pivot->extract(2)-1); # lp_pivot pivots in place -======= - main::lp_pivot($new_tableau, $pivot->extract(1)-1,$pivot->extract(2)-1); ->>>>>>> develop_plus_tableau my $new_matrix = Matrix($new_tableau); my ($n,$m) = $new_matrix->dimensions; my $param_size = $m-$n -1; #n=constraints+1, #m = $param_size + $constraints +2 my $new_basis = ( $old_basis - ($pivot->extract(1)+$param_size) + ($pivot->extract(2)) )->sort; my @statevars = get_tableau_variable_values($new_matrix, $new_basis); -<<<<<<< HEAD return ( $new_tableau, Set($new_basis),\@statevars); # force to set (from type Union) to insure that ->data is an array and not a string. -======= - return ( $new_tableau, Set($new_basis),\@statevars); #FIXME -- force to set (from type Union) to insure that ->data is an array and not a string. ->>>>>>> develop_plus_tableau } @@ -382,44 +335,12 @@ sub linebreak_at_commas { # ); -<<<<<<< HEAD -### End gage_matrix_ops include - - - -======= -# We're going to have several types -# MathObject Matrices Value::Matrix -# tableaus form John Jones macros -# MathObject tableaus -# Containing an matrix $A coefficients for constraint -# A vertical vector $b for constants for constraints -# A horizontal vector $c for coefficients for objective function -# A vertical vector $P for the value of the objective function -# dimensions $n problem vectors, $m constraints = $m slack variables -# A basis Value::Set -- positions for columns which are independent and -# whose associated variables can be determined -# uniquely from the parameter variables. -# The non-basis (parameter) variables are set to zero. -# -# state variables (assuming parameter variables are zero or when given parameter variables) -# create the methods for updating the various containers -# create the method for printing the tableau with all its decorations -# possibly with switches to turn the decorations on and off. - - -### End gage_matrix_ops include ->>>>>>> develop_plus_tableau ################################################## package Tableau; our @ISA = qw(Value::Matrix Value); -<<<<<<< HEAD -sub _Matrix { # can we just import this? - # this is a function, not a method -======= + sub _Matrix { # can we just import this? ->>>>>>> develop_plus_tableau Value::Matrix->new(@_); } From 0554fe6f459b8f5a3c56c017a64f128cc8426146 Mon Sep 17 00:00:00 2001 From: Michael Gage Date: Mon, 9 Oct 2017 16:16:18 -0400 Subject: [PATCH 08/30] Update POD documentation --- lib/Matrix.pm | 79 ++++++++++++++++++++++------------------ lib/Value/Matrix.pm | 4 +- macros/PGmatrixmacros.pl | 4 +- macros/tableau.pl | 17 ++++++++- 4 files changed, 65 insertions(+), 39 deletions(-) diff --git a/lib/Matrix.pm b/lib/Matrix.pm index b2f83ce052..af6bc8073d 100644 --- a/lib/Matrix.pm +++ b/lib/Matrix.pm @@ -1,6 +1,9 @@ =head1 NAME -Matrix - Matrix of Reals +lib/Matrix - Matrix of Reals + + +=head1 DESCRIPTION Implements overrides for MatrixReal.pm for WeBWorK In general it is better to use MathObjects Matrices (Value::Matrix) @@ -10,9 +13,6 @@ subroutines in this file are still used behind the scenes by Value::Matrix to perform calculations, such as decompose_LR(). -=head1 DESCRIPTION - - =head1 SYNOPSIS @@ -87,6 +87,9 @@ sub _stringify { Original matrix is P_L * L * R *P_R # obtain the Left Right matrices of the decomposition and the two pivot permutation matrices # the original is M = PL*L*R*PR + +=cut + sub L { my $matrix = shift; my $rows = $matrix->[1]; @@ -138,7 +141,7 @@ sub PR { # use this permuation on the right PL*L*R*PR =M # the original is M = PL*L*R*PR -=head4 +=item rh_options Method $matrix->rh_options @@ -153,14 +156,14 @@ sub rh_options { $self->[$MatrixReal1::OPTION_ENTRY]; # provides a reference to the options hash MEG } -=head4 - Method $matrix->trace - +=item trace + + Method: $matrix->trace Returns: scalar which is the trace of the matrix. - Used by MathObject Matrices for calculating the trace. - Deprecated for direct use in PG questions. +Used by MathObject Matrices for calculating the trace. +Deprecated for direct use in PG questions. =cut @@ -178,11 +181,11 @@ sub trace { } -=head4 +=item new_from_array_ref Method $new_matrix = $matrix->new_from_array_ref ([[a,b,c],[d,e,f]]) - Deprecated in favor of using creation tools for MathObject Matrices +Deprecated in favor of using creation tools for MathObject Matrices =cut @@ -196,7 +199,7 @@ sub new_from_array_ref { # this will build a matrix or a row vector from [a, b $matrix; } -=head4 +=item array_ref Method $matrix->array_ref @@ -209,7 +212,7 @@ sub array_ref { $this->[0]; } -=head4 +=item list Method $matrix->list @@ -229,11 +232,11 @@ sub list { # this is used only for column vectors } -=head4 +=item new_row_matrix Method $matrix->new_row_matrix - Deprecated -- there are better tools for MathObject Matrices. +Deprecated -- there are better tools for MathObject Matrices. Create a row 1 by n matrix from a list. This subroutine appears to be broken @@ -253,7 +256,7 @@ sub new_row_matrix { # this builds a row vector from an array $matrix; } -=head4 +=item proj Method $matrix->proj Provides behind the scenes calculations for MathObject Matrix->proj @@ -267,11 +270,12 @@ sub proj{ $self * $self ->proj_coeff($vec); } -=head4 +=item proj_coeff Method $matrix->proj_coeff - Provides behind the scenes calculations for MathObject Matrix->proj_coeff - Deprecated for direct use in favor of methods of MathObject matrix + +Provides behind the scenes calculations for MathObject Matrix->proj_coeff +Deprecated for direct use in favor of methods of MathObject matrix =cut @@ -288,11 +292,11 @@ sub proj_coeff{ $x_vector; } -=head4 +=item new_column_matrix Method $matrix->new_column_matrix - Create column matrix from an ARRAY reference (list reference) +Create column matrix from an ARRAY reference (list reference) =cut @@ -309,15 +313,15 @@ sub new_column_matrix { $matrix; } -=head4 - - This method takes an array of column vectors, or an array of arrays, - and converts them to a matrix where each column is one of the previous - vectors. +=item new_from_col_vecs Method $matrix->new_from_col_vecs - Deprecated: The tools for creating MathObjects Matrices are simpler +Deprecated: The tools for creating MathObjects Matrices are simpler. +This method takes an array of column vectors, or an array of arrays, +and converts them to a matrix where each column is one of the previous +vectors. + =cut @@ -366,10 +370,12 @@ sub new_from_col_vecs =cut -=head4 +=item cp Function: cp() - Provides ability to use complex numbers. + +Provides ability to use perl complex numbers. N + =cut sub cp { # MEG makes new copies of complex number @@ -379,7 +385,7 @@ sub cp { # MEG makes new copies of complex number return $w; } -=head4 +=item copy Method $matrix->copy @@ -422,7 +428,7 @@ sub copy # MEG added 6/25/03 to accomodate complex entries -=head4 +=item conj Method $matrix->conj @@ -434,7 +440,7 @@ sub conj { $elem; } -=head4 +=item transpose Method $matrix->transpose @@ -483,12 +489,13 @@ sub transpose $matrix1; } -=head4 +=item decompose_LR Method $matrix->decompose_LR - Used by MathObjects Matrix for LR decomposition - Deprecated for direct use in PG problems. +Used by MathObjects Matrix for LR decomposition +Deprecated for direct use in PG problems. + =cut sub decompose_LR diff --git a/lib/Value/Matrix.pm b/lib/Value/Matrix.pm index e1c78f1a56..4447cd9d8e 100644 --- a/lib/Value/Matrix.pm +++ b/lib/Value/Matrix.pm @@ -4,7 +4,9 @@ # # @@@ Still needs lots of work @@@ -=head1 Value::Matrix class +=head1 NAME + + Value::Matrix class References: diff --git a/macros/PGmatrixmacros.pl b/macros/PGmatrixmacros.pl index b34fd8b47f..85698a4fce 100644 --- a/macros/PGmatrixmacros.pl +++ b/macros/PGmatrixmacros.pl @@ -4,7 +4,7 @@ =head1 NAME - Matrix macros for the PG language + PGmatrixmacros.pl =head1 SYNPOSIS @@ -12,6 +12,8 @@ =head1 SYNPOSIS =head1 DESCRIPTION +Matrix macros for the PG language + These macros are fairly old. The most useful is display_matrix and its variants. diff --git a/macros/tableau.pl b/macros/tableau.pl index a60a8ce7a0..141ec4f2c7 100755 --- a/macros/tableau.pl +++ b/macros/tableau.pl @@ -6,8 +6,13 @@ ##### From gage_matrix_ops # 2014_HKUST_demo/templates/setSequentialWordProblem/bill_and_steve.pg:"gage_matrix_ops.pl", +=head1 NAME -=head1 Tableaus and matrices + macros/tableau.pl + + + +=head2 DESCRIPTION # We're going to have several types # MathObject Matrices Value::Matrix @@ -158,6 +163,16 @@ =head3 Package Tableau (eventually package Matrix?) =cut +=head3 References: + +MathObject Matrix methods: L +MathObject Contexts: L +CPAN RealMatrix docs: L + +More references: L + +=cut + sub _tableau_init {}; # don't reload this file package main; From 41121ab034e968bcd123965ba9bf9d64f828db5d Mon Sep 17 00:00:00 2001 From: Michael Gage Date: Mon, 9 Oct 2017 22:27:07 -0400 Subject: [PATCH 09/30] Add one more test file --- t/matrix_tableau_tests/tableau_test3.pg | 826 ++++++++++++++++++++++++ 1 file changed, 826 insertions(+) create mode 100644 t/matrix_tableau_tests/tableau_test3.pg diff --git a/t/matrix_tableau_tests/tableau_test3.pg b/t/matrix_tableau_tests/tableau_test3.pg new file mode 100644 index 0000000000..4d2b3e879d --- /dev/null +++ b/t/matrix_tableau_tests/tableau_test3.pg @@ -0,0 +1,826 @@ +DOCUMENT(); +loadMacros( +"PGstandard.pl", +"MathObjects.pl", +"parserPopUp.pl", +"unionLists.pl", +"MatrixReduce.pl", +#"AppletObjects.pl", +"PGessaymacros.pl", +"PGmatrixmacros.pl", +"LinearProgramming.pl", +"parserLinearInequality.pl", +"quickMatrixEntry.pl", +#"scaffold.pl", +"tableau.pl", +#"gage_matrix_ops.pl", +"PGinfo.pl", +"source.pl", +"PGcourse.pl", +); + +TEXT(beginproblem()); +TEXT($BEGIN_ONE_COLUMN); +$showPartialCorrectAnswers = 1; + +INITIALIZE_QUICK_MATRIX_ENTRY(); + + +############################################################## +# problem data +############################################################## + +# this can be changed (slightly at least) without affecting the behavior of the problem +# The choice of pivots is not yet automatic However + +# Your resources: +$money_total = 6000; +$time_total = 600; + +# Bill +$bill_money_commitment = 5000; #dollars +$bill_time_commitment = 400; # hours +$bill_profit = 4700; +# Steve +$steve_money_commitment = 3000; +$steve_time_commitment = 500; +$steve_profit = 4500; + +#Hack to prevent domain conflict in answer. +Context()->variables->add(p1=>'Real',p2=>'Real'); +$objfun1 = Formula("${bill_profit}p1 + ${steve_profit}p2"); +# why can't the formula be defined within context "linearInequality"? + +Context("LinearInequality"); +Context()->variables->add(p1=>'Real',p2=>'Real'); +Context()->strings->add("Essay Answer" =>{}); +Context()->strings->add('Minimize'=>{},'Maximize'=>{}, "?"=>{}); +Context()->strings->add('Yes'=>{},'No'=>{}); +Context()->flags->set({tolType=>"absolute",tolerance=>.001}); +Context()->flags->set( + zeroLevel=>0.001, + zeroLevelTol=>.001 +); +# $objfun1 = Formula("${bill_profit}p1 + ${steve_profit}p2"); + +our $context=Context(); + +############################################################## +# +# Setup +# +# + + + +$original_matrix = Matrix([ + [$bill_money_commitment, $steve_money_commitment, 1, 0, 0,0,0, $money_total], + [$bill_time_commitment,$steve_time_commitment, 0, 1, 0,0,0, $time_total], + [1,0,0,0,1,0,0,1], + [0,1,0,0,0,1,0,1], + [-$bill_profit, -$steve_profit, 0, 0, 0,0,1, 0] + ] +); +$toplabels = [qw(p1 p2 x3 x4 x5 x6 P b)]; +$sidelabels = [' ', qw(\text{cash} \text{hours} \text{p_1_bound} \text{p_2_bound} \text{obj_func}) ]; +$matrix1 = $original_matrix; + +############################################################################## +# utility subroutine for checking your answers +############################################################################## + +# size of constraint matrix with slack variables, objective column and constant column +$row_size =4; # n-1 n is constraints + objective_row +$param_size=2; # original number of parameters in the problem +$col_size = $param_size+$row_size+2; # m paramvars+slackvars+objcol+constant_col +# the full tableau has one more row -- the objective function +sub display_tableau_state { + my ($original_matrix,$tableau, $matrix, $basis,$pivot)= @_; + $basis = $basis->sort; + my $basis_size = @{$basis->data} ; + my $basis_matrix = matrix_from_submatrix($original_matrix, rows=>[1..$basis_size],columns=>$basis->data); + my $normalized_tableau = $matrix; + my $reduced_matrix = matrix_from_matrix_rows($matrix,1..$basis_size); + + my $normalized_reduced_matrix = ($basis_matrix->det)*$reduced_matrix; + # calculate current tableau by multiplying by the inverse of the basis + my $obj_coeff = - $original_matrix->row($basis_size+1); + my $cB = Matrix($obj_coeff->column_slice($basis->data)); + my $current_matrix = ($basis_matrix->det)*($basis_matrix->inverse)*matrix_from_matrix_rows($original_matrix,1..$basis_size); + my $new_obj_row = ($cB*$current_matrix)->row(1) - ($basis_matrix->det)*($obj_coeff); + my $current_tableau = Matrix(@{$current_matrix->extract_rows},Matrix($new_obj_row)); + + @statevars1 = get_tableau_variable_values($matrix, $basis); + # get z value + $statevars1 = ~~@statevars1; + $state = Matrix([[@statevars1]]); + +# return " +# +# pivot: \($pivot\) basis: \( $basis\) basis matrix \(". +# display_matrix_mm($basis_matrix)."\) $PAR +# tableau and normalized current tableau $PAR +# \(" . lp_display_mm($tableau) . "\)\(".lp_display_mm($normalized_tableau). " \) $PAR +# original matrix and current matrix calculated by Binverse*original $PAR +# \(". +# display_matrix_mm(matrix_from_matrix_rows($original_matrix,1..$basis_size)). +# "\) \(". +# display_matrix_mm($current_matrix). +# "\) $PAR +# objective coefficients = \( $obj_coeff \) $PAR +# basis objective_coefficients = \($cB\) $BR +# current matrix rows \(". +# join(" ", @{$current_matrix->extract_rows}). +# "\)$PAR +# new objective row cB * B^-1 * M -c= \($new_obj_row\)$PAR +# new tableau \[". +# lp_display_mm($current_tableau). +# "\] $PAR +# state: \(" . join(", ", @$state) . "\) $PAR +# " + return " + + pivot: \($pivot\) basis: \( $basis\) $PAR basis matrix \(". + display_matrix_mm($basis_matrix)."\) $PAR + tableau and normalized current tableau $PAR + \(" . lp_display_mm($tableau) . "\)\(".lp_display_mm($normalized_tableau). " \) $PAR + original matrix and current matrix calculated by Binverse*original $PAR + \(". + display_matrix_mm(matrix_from_matrix_rows($original_matrix,1..$basis_size)). + "\) \(". + display_matrix_mm($current_matrix). + "\) $PAR + objective coefficients = \( $obj_coeff \) $PAR + basis objective_coefficients = \($cB\) $PAR + new tableau \[". + lp_display_mm($current_tableau). + "\] $PAR + state: \($state)\) $PAR + " +} + +# here is the outline of the worksheet project +# BEGIN_TEXT +# \{BeginList("OL")\} +# $ITEM Set up problem +# $ITEM Write tableau +# $ITEM Solve using simplex method +# $ITEM Create dual problem +# $ITEM Write tableau for dual problem +# $ITEM Set up LOP for auxiliary method to find feasible solution +# $ITEM Solve the auxiliary problem to find a feasible basic solution. +# $ITEM Finish solving the dual problem using simplex method +# $ITEM Compare answers to the primary problem and the dual problem. +# \{EndList("OL")\} +# +# END_TEXT + + +######################### +# start sections +######################### + + + +######################################################################################################### +# Section 1 +# section 1 (1essay) +######################################################################################################### +Context($context); +$constraint1 = Compute("${bill_money_commitment}p1 + ${steve_money_commitment}p2 <= $money_total"); +$constraint2 = Compute("${bill_time_commitment}p1 + ${steve_time_commitment}p2 <= $time_total"); +$constraint3 = Compute("p1<=1"); +$constraint4 = Compute("p2<=1"); +$constraints = List( $constraint1, $constraint2, $constraint3, $constraint4 ); +$popup1 = PopUp([qw(? Maximize Minimize)],'Maximize'); +Context($context); +# defined above? wtf? # $objfun1 = Formula("${bill_profit}p1 + ${steve_profit}p2"); +Context()->texStrings; + +BEGIN_TEXT +$PAR This is the same as an earlier problem but the numbers have +been changed slightly. How does this change the result? +$HR $PAR 1.$PAR +You have $DOLLAR$money_total to invest. $PAR +Two of your friends, Bill and Steve, have offered you an opportunity to become +a partner in two different entrepreneurial ventures, one planned by each friend. In both cases, this +investment would involve expending some of your time next summer as well as putting up cash. +$PAR Becoming +a full partner in Bill's venture will require an investment of $DOLLAR$bill_money_commitment + and $bill_time_commitment hours and your estimated profit +at the end of the summer (ignoring the value of your time) would be +$DOLLAR$bill_profit (plus your investment money back). +$PAR +The corresponding figures for Steve's venture are +$DOLLAR$steve_money_commitment and $steve_time_commitment hours with an estimated profit of $DOLLAR$steve_profit. +Both friends are flexible and would +allow you to come in at any fraction of a full partnership you would like. If you choose a fraction +of a full partnership, all the above figures given for a full partnership (money investment, +time investment, and your profit) would be multiplied by this same fraction. +$PAR +Because you were looking for an interesting summer job anyway (maximum of $time_total hours), you have decided to participate in +one or both friends' ventures in whichever combination would maximize your total estimated profit. +$PAR +Write a linear program to help you determine the correct fractions. Use p1 for the fraction of +a full partnership with Bill and p2 for the fraction of a full partnership with Steve. These +fractions should each be less than or equal to one but don't have to sum to one. If you had enough money and +time you could invest fully in both ventures. +$PAR + +\{ANS($popup1->cmp), $popup1->menu\} the profit + +\(P = \)\{Context($context),ANS($objfun1->cmp), ans_rule(40)\} + +$PAR subject to the following constraints. Separate the constraints by commas. + +\{ ANS($constraints->cmp->withPostFilter( + linebreak_at_commas() +)), ans_box(10,80) \} +$PAR +END_TEXT + + +BEGIN_TEXT + +SOLUTION $PAR + +\(p_1\) is the fraction to invest in Bill's venture. +\(p_2 = \) fraction of Steve's venture to invest in. +$PAR + +Maximize \(P = $objfun1\) subject to the constraints: + $PAR +\[ +\begin{aligned} +$constraint1 & \\ +$constraint2 & \\ +$constraint3 & \\ +$constraint4 & \\ +\end{aligned} +\] + +END_TEXT + +######################################################################################################### +# Section 2 -- set up tableau +######################################################################################################### +# get information on current state +$tableau1 = $matrix1->wwMatrix->array_ref; # translate to the array reference +$basis1 = Set(3,4,5,6); +@statevars1 = get_tableau_variable_values($matrix1, $basis1); +# get z value +$statevars1 = ~~@statevars1; +$state1 = Matrix([[@statevars1]]); + +$matrix1->{top_labels}=$toplabels; +Context()->texStrings; + +BEGIN_TEXT + +Write the matrix/tableau representing the linear optimization problem above. Use +the convention that the objective function is listed on the bottom row and the coefficient in +front of the profit \(P\) is \(1\) or equivalently in the form \( -ax_1 -bx_2 +z = 0 \) +$PAR +We'll use x3 for the slack variable for the money constraint, x4 for the time constraint +slack variable and x5 and x6 for the slack variables for the contraints on p1 and p2. +$PAR +\{MATRIX_ENTRY_BUTTON($matrix1)\} +$PAR +\{ANS($matrix1->cmp), $matrix1->ans_array()\} +$PAR +END_TEXT + +BEGIN_TEXT +SOLUTION:$PAR + +\[ \{lp_display_mm($matrix1, top_labels=>$toplabels).side_labels($sidelabels)\} \] + +$PAR +First display of state 1 +$PAR +\{display_tableau_state($original_matrix,$tableau1, $matrix1, $basis1, $pivot)\} +$PAR +END_TEXT + + +Context()->normalStrings; + +######################################################################################################### +# Section 3 -- pivots +######################################################################################################### + +# ######################################################################################################### +# # first pivot +# ######################################################################################################### +# +# Context($context); +$pivot1 = Point("(3,1)"); +# the new basis vectors are [1,3,4,6] <- [3,4,5,6] +($tableau2,$basis2, $statevars2) = lp_basis_pivot($tableau1,$basis1,$pivot1); + $B2 = matrix_extract_submatrix($original_matrix,rows=>[1..$row_size],columns=>$basis2->data); +# BEGIN_TEXT +# tableau2 = $tableau2 $BR +# basis2 = $basis2 $BR +# statevars2 = $statevars2 $BR +# original_matrix = $original_matrix $BR +# B2 = $B2 $BR +# row data:\{ join(" ", @{[1..$row_size]})\} $BR +# basis data (columns: \{join(" ", @{ $basis2->data })\} $BR +# END_TEXT +$matrix2 = ($B2->det)*Matrix($tableau2); +$matrix2->{top_labels}=$toplabels; +$state2=Matrix([[@$statevars2]]); + + +BEGIN_TEXT +SOLUTION:$PAR + +\[ \{lp_display_mm($matrix2, top_labels=>($matrix2->{top_labels})).side_labels($sidelabels)\} \] + +$PAR +Display of state 2 $PAR +\{display_tableau_state($original_matrix,$tableau2, $matrix2, $basis2, $pivot1)\} + +END_TEXT + +# BEGIN_TEXT +# +# Some experiments +# extract rows +# \{ @temp = @{$matrix2->extract_rows} \} +# \{pop @temp \} +# $PAR +# first element \{ref($temp[0])\} +# $PAR +# \{ join(" ", @temp, Matrix([1,2,3,4,5,6,7,8])) \}$PAR +# +# \{$temp2 = Matrix(@temp, Matrix([1,2,3,4,5,6,7,8])) \} +# $PAR +# new matrix is \[ \{lp_display_mm($temp2, top_labels=>$toplabels).side_labels($sidelabels)\} \] +# +# +# END_TEXT +# ######################################################################################################### +# # second pivot +# ######################################################################################################### +$pivot2 = Point("(1,2)"); +# the new basis vectors are [1,2,4,6] <- [1,3,4,6] +($tableau3,$basis3, $statevars3) = lp_basis_pivot($tableau2,$basis2,$pivot2); +$B3 = matrix_from_submatrix($original_matrix,rows=>[1..$row_size],columns=>$basis3->data); +$matrix3 = ($B3->det)*Matrix($tableau3); +$matrix3->{top_labels}=$toplabels; +$state3=Matrix([[@$statevars3]]); + +######################################################################################################### +# third pivot +######################################################################################################### +$pivot3 = Point("(2,5)"); +# the new basis vectors are [1,2,5,6] <- [1,2,4,6] +($tableau4,$basis4, $statevars4) = lp_basis_pivot($tableau3,$basis3,$pivot3); +$B4 = matrix_from_submatrix($original_matrix,rows=>[1..$row_size],columns=>$basis4->data); +$matrix4 = ($B4->det)*Matrix($tableau4); +$matrix4->{top_labels}=$toplabels; +$state4=Matrix([[@$statevars4]]); + +$p1 = $state4->element(1,1); +$p2 = $state4->element(1,2); +$profit = $state4->element(1,7); + +######################################################################################################### +# debugging display code +######################################################################################################### +BEGIN_TEXT +pivot1 $pivot1 $PAR +\{display_tableau_state($original_matrix,$tableau2, $matrix2, $basis2, $pivot1)\} +pivot2 $pivot2 $PAR +\{display_tableau_state($original_matrix,$tableau3, $matrix3, $basis3, $pivot2)\} +pivot3 $pivot3 $PAR +\{display_tableau_state($original_matrix,$tableau4, $matrix4, $basis4, $pivot3)\} +END_TEXT +# # +# # +# # ######################################################################################################### +# # # section 3 answers (3,4,5,6,7,8, 9, 10) +# # ######################################################################################################### +# Context()->texStrings; +# +# BEGIN_TEXT +# $PAR +# What is the initial state? $PAR +# \((p_1,p_2,x_3,x_4,x_5,x_6,P)=\): \{ANS($state1->cmp),$state1->ans_array(4)\} $PAR +# +# Using the convention that one removes the "first" nonbasic column that will increase profits +# (the convention that Hurlbert uses) find the first pivot location. (e.g. (2,3) for line 2, column 3). +# and perform the pivot operation. (You can use +# \{htmlLink("http://people.vcu.edu/~ghurlbert/websim/sim.html","websim")\} to do the calculation. ); +# $PAR +# +# Pivot entry: use parentheses as in (row, column): \{ANS($pivot1->cmp),$pivot1->ans_rule \}. +# $PAR +# The matrix has been multiplied by a constant so that all values are integers. This is +# the same result as you will obtain using websim. +# $PAR +# \{MATRIX_ENTRY_BUTTON($matrix2)\} +# $PAR +# $PAR +# \{ANS($matrix2->cmp()), $matrix2->ans_array(6)\} +# +# $PAR +# Current vector (values): \{ANS($state2->cmp),$state2->ans_array(6)\} $PAR +# $HR +# Next pivot entry: \{ANS($pivot2->cmp),$pivot2->ans_rule \}. +# +# $PAR +# \{MATRIX_ENTRY_BUTTON($matrix3)\} +# $PAR +# \{ANS($matrix3->cmp()), $matrix3->ans_array(6)\} +# $HR +# Next pivot entry: \{ANS($pivot3->cmp),$pivot3->ans_rule \} +# results in the tableau: +# $PAR +# \{MATRIX_ENTRY_BUTTON($matrix4)\} +# $PAR $PAR +# \{ANS($matrix4->cmp()), $matrix4->ans_array(6)\} +# $HR +# At this point we are at a local (and therefore a global) maximum and pivoting +# will not increase the profit. +# $PAR +# Your final state is \{ANS($state4->cmp),$state4->ans_array(6)\} $PAR +# (Make sure you are getting all the zeros in the answer! The numbers are quite large +# and overflow the blanks. -- you can make the table blanks wider +# in websim by right clicking on upper left corner of a table. ) +# +# $PAR What percentage of Bill's venture do you invest in? +# \{ANS($p1->cmp), $p1->ans_rule\} +# $PAR What percentage of Steve's venture do you invest in? +# \{ANS($p2->cmp), $p2->ans_rule\} +# $PAR What is your expected profit? +# $DOLLAR\{ANS($profit->cmp),$profit->ans_rule\}. +# +# +# $PAR +# +# END_TEXT +# +# BEGIN_SOLUTION +# Before pivoting the state is \($state1\) $PAR +# Your first pivot should be on \($pivot1\) since that is the left most +# column that +# will increase the profit and the first row has the most limiting ratio. +# $PAR +# \( \{lp_display_mm( [$matrix2->value],top_labels=>$toplabels )\} \) $PAR +# $PAR +# +# $PAR +# \( \{lp_display_mm([$matrix3->value], top_labels=>$toplabels )\}\) +# \( \{side_labels( qw(\text{cash} \text{hours} \text{profits} ) ) \} \) +# $PAR +# +# +# one more +# \( \{lp_display_mm([$matrix4->value],top_labels=>$toplabels )\} \) +# $PAR +# At this point you are done since changing any of the non basic variables will only +# decrease the profit. +# END_SOLUTION +# Context()->normalStrings; +# +# Section::End(); +# +# } # END SECTION 3 +# +# ######################################################################################################### +# # Section 4 dual problem +# ######################################################################################################### +# +# SECTION4: { +# Section::Begin("Part 4: The dual problem"); +# +# Context()->variables->add(y1=>'Real',y2=>'Real', y3=>'Real', y4=>'Real', w=>'Real',y0=>'Real'); +# $dual_constraint1 = Compute("${bill_money_commitment}y1 +${bill_time_commitment}y2 +y3 >=$bill_profit"); +# $dual_constraint2 = Compute("${steve_money_commitment}y1 +${steve_time_commitment}y2 +y4 >=$steve_profit"); +# $dual_constraints= List($dual_constraint1, $dual_constraint2); +# +# $dual_objfun = Formula("${money_total}y1 +${time_total}y2 + y3 +y4"); +# $popupmaxmin = PopUp(["?","Maximize", "Minimize"], "Minimize"); +# +# Context()->texStrings; +# +# BEGIN_TEXT +# Construct the dual problem for the linear optimization problem above. The first goal is to calculate +# an upper bound for the possible profit in the LOP using linear combinations of the inequality constraints. +# Then formulate the search for the best of these upper bounds in such a way that it +# becomes a new LOP -- the dual problem. Use variables \(y_1,y_2,y_3,y_4\) to create linear combinations +# of the constraints on money, time and the total probabilities \(p_1\) and \(p_2\) respectively, +# and create a linear function \(w = Ay_1 + By_2 +Cy_3 +Dy_4\) which guarantees that \(w\) will +# be larger than any profit one could make. What constraints must \(y_1\dots y_4\) satisfy? +# $PAR +# \{ANS($dual_constraints->cmp->withPostFilter( +# linebreak_at_commas() +# )),ans_box(4,80)\} +# $PAR The objective function would be: +# \(w = \) \{ANS($dual_objfun->cmp), $dual_objfun->ans_rule(50)\} +# $PAR +# To get the best possible estimate for the profit in the original problem +# you would want to \{ANS($popupmaxmin->cmp),$popupmaxmin->menu\} \(w\); +# $PAR +# Why? +# $PAR +# \{ANS(essay_cmp()), essay_box(5,80)\} +# END_TEXT +# +# BEGIN_SOLUTION +# The profit is less than the minimum of \( w = $dual_objfun \) subject to +# +# \[\begin{aligned} +# $dual_constraint1 & \\ +# $dual_constraint2 & +# \end{aligned} +# \] +# The \(y_i\) are the coefficients used to add up the constraint inequalities of the primary problem +# so as to estimate an upper bound for the profit. If the \(y_i\) are positive values satisfying +# the constraints then \(w\) will be greater than or equal to the profit. We get the most precise +# estimate by finding values of \(y_i\), satisfying the constraints, which give the smallest +# possible value for \( w\). +# +# END_SOLUTION +# Context()->normalStrings; +# +# Section::End(); +# } # END SECTION 4 +# +# ######################################################################################################### +# # Section 5 set up dual constraints +# ######################################################################################################### +# SECTION5: { +# +# Section::Begin("Part 5: Set up dual constraints"); +# Context($context); +# $dualtableau1 = [[-$bill_money_commitment,-$bill_time_commitment, -1, 0, 1, 0, 0,-$bill_profit], +# [-$steve_money_commitment, -$steve_time_commitment,0,-1, 0, 1, 0,-$steve_profit], +# [${money_total},${time_total},1, 1, 0,0, 1,0]]; +# $dualmatrix1 = Matrix($dualtableau1); +# $dualtoplabels = [qw(y1 y2 y3 y4 y5 y6 v b)]; +# $dualmatrix1->{top_labels}=$dualtoplabels; +# $dualtableau1_string = lp_display_mm($dualtableau1,top_labels=>$dualtoplabels); +# +# $popup = PopUp(["?","Yes", "No"], "No"); +# +# $dual_row_size =2; # figure these out automatically +# $dual_col_size =$col_size; +# BEGIN_TEXT +# dualmatrix1 size \{join(",", $dualmatrix1->dimensions)\} $BR +# $dual_row_size, $dual_col_size +# END_TEXT +# Context()->texStrings; +# +# BEGIN_TEXT +# $PAR +# For this first effort let's rewrite the equations so that they are in "standard" form, meaning +# that the inequalities are all less than or equal to and that the goal is to maximize \(v = -w\). +# This will probably mean that you will have to rewrite the natural way in which you set up +# the problem above. Some coefficients will change sign. +# We will still be using the convention that +# the coefficient of \(v\) will be \(1\). +# +# $PAR +# \{MATRIX_ENTRY_BUTTON($dualmatrix1)\} +# \{ANS($dualmatrix1->cmp()), $dualmatrix1->ans_array \} +# $PAR +# (This tableau is for maximizing \(v\), aka \(-w\) -- there are two minus sign switches. ) +# $PAR +# Is there a natural feasible solution to this problem? In other words does setting the +# problem parameters equal to 0 provide a feasible solution? +# \{ANS($popup->cmp),$popup->menu \} +# $PAR +# Explain why or why not and what are your options for getting started. +# \{ ANS(essay_cmp()), essay_box(10,80) \} +# $PAR +# END_TEXT +# +# BEGIN_SOLUTION +# \[ $dualtableau1_string \] +# $PAR +# Setting the parameters \(y_1, y_2\) to zero is not a feasible solution. +# Options for finding a first feasible solution include guessing (not a bad choice +# for a problem this small), using prior knowledge (e.g. a known optimal solution to +# a similar problem), creating an auxiliary problem to find a feasible point, +# and the "shortcut method" which is an accelerated version of the "auxiliary method". +# END_SOLUTION +# Context()->normalStrings; +# Section::End(); +# +# } #END SECTION 5 +# ######################################################################################################### +# # Begin section 6 -- solve the tableau using simplex method +# # section 6 (13matrix, 14essay) +# # First we have to find a feasible solution -- phase 1 +# # We'll use the auxiliary method -- adding an extra slack variable. +# ######################################################################################################### +# +# SECTION6: { +# Section::Begin("Part 6: Solve the dual tableau using the simplex method"); +# Context($context); +# $dualtableau2 = [[-1,-$bill_money_commitment,-$bill_time_commitment, -1, 0, 1,0,0,0,-$bill_profit], +# [-1,-$steve_money_commitment,-$steve_time_commitment, 0, -1, 0,1,0,0,-$steve_profit], +# [1,0,0,0,0,0,0,0,1,0], +# [0,$money_total,$time_total,1,1,0,0,1,0,0]]; +# $dualmatrix2 = Matrix($dualtableau2); +# $orginal_dual_matrix=$dualmatrix2; +# $dualtoplabelsphase1 = [qw(y0 y1 y2 y3 y4 y5 y6 v z b)]; +# $dualmatrix2->{top_labels}= $dualtoplabelsphase1; +# $dual_constraint1phase1 = Compute("-${bill_money_commitment}y1 -${bill_time_commitment}y2 -y3 <=-$bill_profit+y0"); +# $dual_constraint2phase1 = Compute("-${steve_money_commitment}y1 -${steve_time_commitment}y2 -y4 <=-$steve_profit+y0"); +# $dualconstraintsphase1 = List($dual_constraint1phase1,$dual_constraint2phase1); +# $dualpivot2=Point(1,1); +# $dualbasis2=Set(6,7); +# ######################################################################################################### +# # section 6 answers (15essay 16matrix) +# ######################################################################################################### +# Context()->texStrings; +# +# BEGIN_TEXT +# Using the auxiliary method write the LOP for finding +# the first feasible point. Write +# the new constraints to be used by auxiliary method. $PAR +# \{ ANS($dualconstraintsphase1->cmp),ans_box(3,80) \} +# $PAR +# We will continue to use the convention that we are maximizing +# the auxiliary function so there will be some minus signs that need to be taken into account. +# We'll let \(z = -y_0\) and try to maximize \(z\). +# $PAR +# Now construct the tableau you'll use for the auxiliary method. +# Add a first column for the extra slack variable and a next to the last row +# for the new objective function value \(z\) and write the new tableau. +# The next to the last row will hold the objective function for \(z\). +# The last row will hold the original +# dual objective function which we'll just carry along. +# $PAR +# \{MATRIX_ENTRY_BUTTON($dualmatrix2)\} +# \{ANS($dualmatrix2->cmp()), $dualmatrix2->ans_array \} +# $PAR +# END_TEXT +# ######################################################################################################### +# # debugging display code +# ######################################################################################################### +# BEGIN_TEXT +# $PAR +# \{display_tableau_state($orginal_dual_matrix,$dualtableau2, $dualmatrix2, $dualbasis2, $dualpivot2)\} +# $PAR +# END_TEXT +# +# Context()->normalStrings; +# +# Context()->texStrings; +# BEGIN_SOLUTION +# New objective: Maximize \(z = -y_0 \) subject to +# \[\begin{align} +# $dual_constraint1phase1&\\ +# $dual_constraint2phase1 &\\ +# \end{align} +# \] with all variables non-negative. +# $PAR +# If \(y_0\) is large enough this always has a feasible solution with +# \(y_1=y_2=0\). +# +# \[ \{lp_display_mm($dualtableau2) \} \] +# $PAR +# END_SOLUTION +# Context()->normalStrings; +# Section::End(); +# } #END_SECTION 6 +# ######################################################################################################### +# # Section 7 final comparisons of primary and dual results. +# ######################################################################################################### +# SECTION7: { +# Section::Begin("Part 7: do phase 1 simplex method"); +# +# +# #($tableau3,$basis3, $statevars3) = lp_basis_pivot($dualtableau2,$dualbasis2,$dualpivot2); +# #$B3 = matrix_from_submatrix($original_matrix,rows=>[1..$row_size],columns=>$basis3->data); +# #$matrix3 = ($B3->det)*Matrix($tableau3); +# # $matrix3->{top_labels}=$toplabels; +# #$state3=Matrix([[@$statevars3]]); +# ######################################################################################################### +# # debugging display code +# ######################################################################################################### +# BEGIN_TEXT +# $PAR +# \{display_tableau_state($orginal_dual_matrix,$dualtableau3, $dualmatrix3, $dualbasis3, $dualpivot3)\} +# $PAR +# END_TEXT +# +# $pivot4 = Point("(1,1)"); +# $dualtableau3 = lp_pivot($dualtableau2,0,0); +# $dualmatrix3 = Matrix($dualtableau3); +# $dualmatrix3->{top_labels}=$dualtoplabelsphase1; +# $dualtableau3_string = lp_display_mm($dualtableau3); +# $z_initial=-$bill_profit; # a hint +# # this is the initial value of the z=-y0 variable we are maximizing? +# +# # now start to maximize z +# $pivot5 = Point("(2,2)"); +# $dualtableau4 = lp_pivot($dualtableau3,1,1); +# $denom1 = 2000; +# $dualmatrix4 = $denom1*Matrix($dualtableau4); +# $dualmatrix4->{top_labels}=$dualtoplabelsphase1; +# $dualtableau4_string = lp_display_mm($dualmatrix4); +# $pivot6 = Point("(1,3)"); +# $dualtableau5 = lp_pivot($dualtableau4,0,2); +# +# ## FIXME entries close to zero in a matrix are not being compared properly. +# ## FIXME tolerance and tolType are being ignored +# +# #$dualtableau5->[2][3]=0; +# #$dualtableau5->[2][5]=0; +# +# $denom2 = 1.3E6; +# $dualmatrix5 = $denom2*Matrix($dualtableau5); +# $dualmatrix5->{top_labels}=$dualtoplabelsphase1; +# $dualtableau5_string = lp_display_mm($dualmatrix5); +# $state5 = Matrix([[ 6.4615, 0.423, 0, 0, -6415.38]]); +# $popup5 = PopUp(["?","Yes", "No"], "No"); +# ######################################################################################################### +# # Section 7 answers 17,18matrix,19,20matrix,21,22matrix,23, 24,25essay +# ######################################################################################################### +# +# +# +# Context()->texStrings; +# +# BEGIN_TEXT +# The first pivot, which will make the right hand side entries positive is +# \{ANS($pivot4->cmp),$pivot4->ans_rule \}. $PAR +# Recall that in the case of tie we take entry with the least index +# (i.e. left most or upper most. ) +# $PAR +# +# +# The resulting tableau is $PAR +# \{MATRIX_ENTRY_BUTTON($dualmatrix3)\} +# \{ANS($dualmatrix3->cmp), $dualmatrix3->ans_array() \} +# +# The value of \(z=-y_0\) is \($z_initial\). +# $PAR +# The next pivot, following the simplex method to maximize \(z\), is +# \{ANS($pivot5->cmp), $pivot5->ans_rule\}. +# $PAR +# (Many of the following answers have lots of zeros. You can use the shortcut +# 1E3 to stand for \(1\times 10^3\). ) +# $PAR +# Notice that because of the zero on the right hand side none of the state variables change. We had three +# hyperplanes intersecting at a point and we have changed our mind about which +# of those three we consider basic. The new tableau is +# $PAR +# \{MATRIX_ENTRY_BUTTON($dualmatrix4)\} +# \{ANS($dualmatrix4->cmp), $dualmatrix4->ans_array() \} +# $PAR +# The next pivot \{ ANS($pivot6->cmp), $pivot6->ans_rule \} leads to $PAR +# \{MATRIX_ENTRY_BUTTON($dualmatrix5)\} +# \{#FIXME -- these tolerance leveals are being ignored \} +# \{ANS($dualmatrix5->cmp(zeroLevel=>.1, zeroLevelTol=>.1)), $dualmatrix5->ans_array() \} +# +# $PAR and we notice that now \(z=-y_0=0\) so we have found a basic feasible solution to our +# original dual problem. The variables \(y_1,y_2,y_3,y_4,v\) for this +# solution are +# $PAR +# +# \{ANS($state5->cmp),$state5->ans_array\} +# $PAR +# Do we need to continue to optimize the value for \(v\)? +# \{ANS($popup5->cmp), $popup5->menu()\} Why? $PAR +# \{ANS(essay_cmp), essay_box(3,80)\} +# $PAR +# Compare this answer \(v^*= -w^*\) to the dual problem to the optimal value \(P^*\)for the primary problem. +# The problems' goals were to maximize \(P\) and to minimize \(w\). +# $PAR +# +# END_TEXT +# +# BEGIN_SOLUTION +# The first pivot is \($pivot4\) which makes the right hand side entries positive. +# $PAR +# \[$dualtableau3_string\] +# $PAR +# The next pivot is \($pivot5\). It follows the simplex rule of choosing the row +# with the most restrictive ratio -- in this case zero. The result is simply to +# choose a new representation of the same point -- no change in state takes place. +# $PAR\[$dualtableau4_string\] +# $PAR +# The final pivot (as it turns out) is \($pivot6\). At this point we are done with +# phase 1 because the value of \(z=-y_0=0\) so we have maximized \(z\) and +# minimized \(y_0\) to \(0\) +# $PAR\[$dualtableau5_string\] +# $PAR +# At this point we notice that the coefficients in the last row are such that +# we cannot increase the value of \(v\) any further. \(v\) is at its maximum +# and \(w=-v\) is at its minimum. The minimum value of \(w=6000\) which is the +# same as the maximum profit that we found in the first example. +# END_SOLUTION +# Context()->normalStrings; +# Section::End(); +# TEXT($END_ONE_COLUMN); +# Scaffold::End(); +# } # END SECTION 7 +ENDDOCUMENT(); + From e2b3ef4d81d5b5168e65e16c813033eb62628cf5 Mon Sep 17 00:00:00 2001 From: Michael Gage Date: Thu, 12 Oct 2017 22:09:15 -0400 Subject: [PATCH 10/30] Create test pg files 1 through 6abcd. Fix up tableau.pl This is now working reasonably well for displaying the tableau for any given choice of basis. --- macros/tableau.pl | 74 ++- t/matrix_tableau_tests/tableau-test6.pg | 177 +++++++ t/matrix_tableau_tests/tableau-test6a.pg | 186 +++++++ t/matrix_tableau_tests/tableau-test6b.pg | 190 +++++++ t/matrix_tableau_tests/tableau-test6c.pg | 237 +++++++++ t/matrix_tableau_tests/tableau-test6d.pg | 206 ++++++++ t/matrix_tableau_tests/tableau_test1.pg | 2 +- t/matrix_tableau_tests/tableau_test3.pg | 601 ++++++++++++----------- t/matrix_tableau_tests/tableau_test4.pg | 512 +++++++++++++++++++ t/matrix_tableau_tests/tableau_test5.pg | 505 +++++++++++++++++++ 10 files changed, 2381 insertions(+), 309 deletions(-) create mode 100644 t/matrix_tableau_tests/tableau-test6.pg create mode 100644 t/matrix_tableau_tests/tableau-test6a.pg create mode 100644 t/matrix_tableau_tests/tableau-test6b.pg create mode 100644 t/matrix_tableau_tests/tableau-test6c.pg create mode 100644 t/matrix_tableau_tests/tableau-test6d.pg create mode 100644 t/matrix_tableau_tests/tableau_test4.pg create mode 100644 t/matrix_tableau_tests/tableau_test5.pg diff --git a/macros/tableau.pl b/macros/tableau.pl index 141ec4f2c7..cde5c3129d 100755 --- a/macros/tableau.pl +++ b/macros/tableau.pl @@ -46,12 +46,14 @@ =head2 DESCRIPTION Matrix A, the constraint matrix is n by m Matrix S, the slack variables is m by m Matrix b, the constraint constants is n by 1 + Matrix c, the objective function coefficients is 1 by n The next to the last column holds z or objective value z(...x^i...) = c_i* x^i (Einstein summation convention) =cut + =head2 Package main =cut @@ -177,6 +179,10 @@ =head3 References: sub _tableau_init {}; # don't reload this file package main; +sub isMatrix { + my $m = shift; + return ref($m) =~/Value::Matrix/i; +} sub matrix_column_slice{ matrix_from_matrix_cols(@_); } @@ -199,6 +205,11 @@ sub matrix_row_slice{ sub matrix_from_matrix_rows { my $M = shift; # a MathObject matrix_columns + unless (isMatrix($M)){ + WARN_MESSAGE( "matrix_from_matrix_rows: |".ref($M)."| or |$M| is not a MathObject Matrix"); + return undef; + } + my($n,$m) = $M->dimensions; my @slice = @_; if (ref($slice[0]) =~ /ARRAY/) { # handle array reference @@ -217,7 +228,11 @@ sub matrix_extract_submatrix { } sub matrix_from_submatrix { my $M=shift; - return undef unless ref($M) =~ /Value::Matrix/; + unless (isMatrix($M)){ + warn( "matrix_from_submatrix: |".ref($M)."| or |$M| is not a MathObject Matrix"); + return undef; + } + my %options = @_; my($n,$m) = $M->dimensions; my $row_slice = ($options{rows})?$options{rows}:[1..$m]; @@ -230,6 +245,11 @@ sub matrix_from_submatrix { } sub matrix_extract_rows { my $M =shift; + unless (isMatrix($M)){ + WARN_MESSAGE( "matrix_extract_rows: |".ref($M)."| or |$M| is not a MathObject Matrix"); + return undef; + } + my @slice = @_; if (ref($slice[0]) =~ /ARRAY/) { # handle array reference @slice = @{$slice[0]}; @@ -247,6 +267,11 @@ sub matrix_columns_to_list { } sub matrix_extract_columns { my $M =shift; # Add error checking + unless (isMatrix($M)){ + WARN_MESSAGE( "matrix_extract_columns: |".ref($M)."| or |$M| is not a MathObject Matrix"); + return undef; + } + my @slice = @_; if (ref($slice[0]) =~ /ARRAY/) { # handle array reference @slice = @{$slice[0]}; @@ -270,6 +295,10 @@ sub matrix_extract_columns { # feature request -- for tableau object -- allow specification of non-zero parameter variables sub get_tableau_variable_values { my $mat = shift; # a MathObject matrix + unless (isMatrix($mat)){ + WARN_MESSAGE( "get_tableau_variable_values: |".ref($mat)."| or |$mat| is not a MathObject Matrix"); + return Matrix([0]); + } my $basis =shift; # a MathObject set # FIXME # type check ref($mat)='Matrix'; ref($basis)='Set'; @@ -279,7 +308,7 @@ sub get_tableau_variable_values { #DEBUG_MESSAGE( "start new matrix"); foreach my $j (1..$m-2) { # the last two columns of the tableau are object variable and constants if (not $basis->contains($j)) { - DEBUG_MESSAGE( "j= $j not in basis"); # set the parameter values to zero + # DEBUG_MESSAGE( "j= $j not in basis"); # set the parameter values to zero $var[$j-1]=0; next; # non-basis variables (parameters) are set to 0. } else { @@ -287,7 +316,7 @@ sub get_tableau_variable_values { # if this is a basis column there should be only one non-zero element(the pivot) if ( $mat->element($i,$j)->value != 0 ) { # should this have ->value????? $var[$j-1] = ($mat->element($i,$m)/($mat->element($i,$j))->value); - DEBUG_MESSAGE("i=$i j=$j var = $var[$j-1] "); # calculate basis variable value + # DEBUG_MESSAGE("i=$i j=$j var = $var[$j-1] "); # calculate basis variable value next; } @@ -295,7 +324,12 @@ sub get_tableau_variable_values { } } # element($n, $m-1) is the coefficient of the objective value. # this last variable is the value of the objective function - push @var , ($mat->element($n,$m)/$mat->element($n,$m-1))->value; + # check for division by zero + if ($mat->element($n,$m-1)->value != 0 ) { + push @var , ($mat->element($n,$m)/$mat->element($n,$m-1))->value; + } else { + push @var , '.666'; + } return wantarray ? @var : \@var; # return either array or reference to an array } #### Test -- assume matrix is this @@ -406,10 +440,10 @@ sub initialize { $self->{S} = Value::Matrix->I($m); $self->{basis} = [($n+1)...($n+$m)] unless ref($self->{basis})=~/ARRAY/; my @rows = $self->assemble_matrix; - #main::DEBUG_MESSAGE("rows", @rows); + # main::DEBUG_MESSAGE("rows", map {ref($_)?$_->value :$_} map {@$_} @rows); $self->{M} = _Matrix([@rows]); $self->{B} = $self->{M}->submatrix(rows=>[1..($self->{m})],columns=>$self->{basis}); - $self->{obj_row} = _Matrix($self->objective_row()); + $self->{obj_row} = _Matrix(@{$self->objective_row()}); return(); } @@ -421,31 +455,33 @@ sub assemble_matrix { foreach my $i (1..$m) { my @current_row=(); foreach my $j (1..$n) { - push @current_row, $self->{A}->element($i, $j); + push @current_row, $self->{A}->element($i, $j)->value; } foreach my $j (1..$m) { - push @current_row, $self->{S}->element($i,$j); # slack variables + push @current_row, $self->{S}->element($i,$j)->value; # slack variables } - push @current_row, 0, $self->{b}->data->[$i-1]; # obj column and constant column + push @current_row, 0, $self->{b}->row($i)->value; # obj column and constant column push @rows, [@current_row]; } return @rows; # these are the matrices A | S | obj | b - # the final row describing the objective function is not in this + # the final row describing the objective function + # is not in this part of the matrix } sub objective_row { my $self = shift; my @last_row=(); - push @last_row, ( -($self->{c}) )->value; - foreach my $i (1..($self->{m})) { push @last_row, 0 }; - push @last_row, 1, 0; + push @last_row, ( -($self->{c}) )->value; # add the negative coefficients of the obj function + foreach my $i (1..($self->{m})) { push @last_row, 0 }; # add 0s for the slack variables + push @last_row, 1, 0; # add the 1 for the objective value and 0 for the initial valu return \@last_row; } # return a matrix containing the entire tableau sub current_tableau { - my $Badj = ($self->{B}->det) * ($self->{B}->inverse); + my $self = shift; + my $Badj = ($self->{B}->det->value) * ($self->{B}->inverse); my $current_tableau = $Badj * $self->{M}; # the A | S | obj | b $self->{current_tableau}=$current_tableau; # find the coefficients associated with the basis columns @@ -454,7 +490,9 @@ sub current_tableau { my $correction_coeff = ($c_B2*$current_tableau )->row(1); # subtract the correction coefficients from the obj_row # this essentially extends Gauss reduction applied to the obj_row - my $obj_row_normalized = ($self->{B}->det) *$self->{obj_row}; + my $obj_row_normalized = ($self->{B}->det->value) *$self->{obj_row}; + #main::DEBUG_MESSAGE(" normalized obj row ",$obj_row_normalized->value); + #main::DEBUG_MESSAGE(" correction coeff ", $correction_coeff->value); my $current_coeff = $obj_row_normalized-$correction_coeff ; $self->{current_coeff}= $current_coeff; @@ -470,12 +508,16 @@ sub current_tableau { sub basis { my $self = shift; #update basis + # basis is stored as an ARRAY reference. + # basis is exported as a list my @input = @_; return Value::List->new($self->{basis}) unless @input; #return basis if no input my $new_basis; if (ref( $input[0]) =~/ARRAY/) { $new_basis=$input[0]; - } else { + } elsif (ref( $input[0]) =~/List|Set/){ + $new_basis = $input[0]->value; + } else { # input is assumed to be an array $new_basis = \@input; } $self->{basis}= $new_basis; diff --git a/t/matrix_tableau_tests/tableau-test6.pg b/t/matrix_tableau_tests/tableau-test6.pg new file mode 100644 index 0000000000..547a762075 --- /dev/null +++ b/t/matrix_tableau_tests/tableau-test6.pg @@ -0,0 +1,177 @@ +DOCUMENT(); +loadMacros( +"PGstandard.pl", +"MathObjects.pl", +"parserPopUp.pl", +"unionLists.pl", +"MatrixReduce.pl", +#"AppletObjects.pl", +"PGessaymacros.pl", +"PGmatrixmacros.pl", +"LinearProgramming.pl", +"parserLinearInequality.pl", +"quickMatrixEntry.pl", +#"scaffold.pl", +"tableau.pl", +#"gage_matrix_ops.pl", +"PGinfo.pl", +"source.pl", +"PGcourse.pl", +); + +TEXT(beginproblem()); +TEXT($BEGIN_ONE_COLUMN); +$showPartialCorrectAnswers = 1; + +INITIALIZE_QUICK_MATRIX_ENTRY(); + +$money_total = 6000; +$time_total = 600; + +# Bill +$bill_money_commitment = 5000; #dollars +$bill_time_commitment = 400; # hours +$bill_profit = 4700; +# Steve +$steve_money_commitment = 3000; +$steve_time_commitment = 500; +$steve_profit = 4500; + + +#Hack to prevent domain conflict in answer. +Context()->variables->add(p1=>'Real',p2=>'Real'); +$objfun1 = Formula("${bill_profit}p1 + ${steve_profit}p2"); +# why can't the formula be defined within context "linearInequality"? + +Context("LinearInequality"); +Context()->variables->add(p1=>'Real',p2=>'Real'); +Context()->strings->add("Essay Answer" =>{}); +Context()->strings->add('Minimize'=>{},'Maximize'=>{}, "?"=>{}); +Context()->strings->add('Yes'=>{},'No'=>{}); +Context()->flags->set({tolType=>"absolute",tolerance=>.001}); +Context()->flags->set( + zeroLevel=>0.001, + zeroLevelTol=>.001 +); +# $objfun1 = Formula("${bill_profit}p1 + ${steve_profit}p2"); + +our $context=Context(); + + +sub update_tableau { + my $original_matrix = shift; + my $basis = shift; + # $basis is a Set object + # last row is numbered $basis_size+1; + my $basis_size=@{$basis->data}; + my $basis_matrix = $original_matrix->submatrix(rows=>[1..$basis_size],columns=>($basis->data)); + my $context=Context(); + my $det = 1; #Real($basis_matrix->det); + $det = Real($basis_matrix->det); + $det = $det->value; + $det=Real(130000); #Real(1300000); + DEBUG_MESSAGE("det = $det ", ref($det)); + my $basis2= Matrix($basis_matrix); + #DEBUG_MESSAGE(($basis2->det)*($basis2->inverse) *$original_matrix->row_slice(1..$basis_size), $PAR); + my $obj_coeff = - ($original_matrix->row($basis_size+1)); + #obj function coeff corresponding to basis variables/columns + $cB = Matrix($obj_coeff->column_slice($basis->data)); + # get A | S |b portion of tableau and multiply by det(B)B^-1 + my $current_matrix= ($det)*($basis_matrix->inverse)*($original_matrix->row_slice(1..$basis_size)); + my $new_obj_row = ($cB*$current_matrix)->row(1) - ($det)*($obj_coeff); + my $current_tableau = Matrix(@{$current_matrix->extract_rows},Matrix($new_obj_row)); + return $current_tableau; +} +# the full tableau has one more row -- the objective function +sub display_tableau_state { + my ($original_matrix,$tableau, $matrix, $basis,$pivot)= @_; + $basis = $basis->sort; + my $basis_size = @{$basis->data} ; + my $basis_matrix = matrix_from_submatrix($original_matrix, rows=>[1..$basis_size],columns=>$basis->data); + my $normalized_tableau = $matrix; + my $reduced_matrix = matrix_from_matrix_rows($matrix,1..$basis_size); + warn "basis matrix is $basis_matrix, $reduced_matrix"; + my $normalized_reduced_matrix = ($basis_matrix->det)*$reduced_matrix; + # calculate current tableau by multiplying by the inverse of the basis + my $obj_coeff = - $original_matrix->row($basis_size+1); + my $cB = Matrix($obj_coeff->column_slice($basis->data)); + my $current_matrix = ($basis_matrix->det)*($basis_matrix->inverse)*matrix_from_matrix_rows($original_matrix,1..$basis_size); + my $new_obj_row = ($cB*$current_matrix)->row(1) - ($basis_matrix->det)*($obj_coeff); + my $current_tableau = Matrix(@{$current_matrix->extract_rows},Matrix($new_obj_row)); + $current_tableau = update_tableau($original_matrix, $basis); + + #DEBUG_MESSAGE("get state variables for $matrix using basis $basis"); + @statevars1 = get_tableau_variable_values($matrix, $basis); + # get z value + $statevars1 = ~~@statevars1; + $state = Matrix([[@statevars1]]); + + + return " + + pivot: \($pivot\) basis: \( $basis\) $PAR basis matrix \(". + display_matrix_mm($basis_matrix)."\) $PAR + tableau and normalized current tableau $PAR + \(" . lp_display_mm($tableau) . "\)\(".lp_display_mm($normalized_tableau). " \) $PAR + original matrix and current matrix calculated by Binverse*original $PAR + \(". + display_matrix_mm(matrix_from_matrix_rows($original_matrix,1..$basis_size)). + "\) \(". + display_matrix_mm($current_matrix). + "\) $PAR + objective coefficients = \( $obj_coeff \) $PAR + basis objective_coefficients = \($cB\) $PAR + new tableau \[". + lp_display_mm($current_tableau). + "\] $PAR + state: \($state)\) $PAR + " +} + + +#### problem starts here: + +Context($context); +$ra_matrix = [[-1,-$bill_money_commitment,-$bill_time_commitment, -1, 0, 1,0,0,0,-$bill_profit], + [-1,-$steve_money_commitment,-$steve_time_commitment, 0, -1, 0,1,0,0,-$steve_profit], + [1,0,0,0,0,0,0,0,1,0]]; + +# [0,$money_total,$time_total,1,1,0,0,1,0,0]]; +$original_matrix = Matrix($ra_matrix); + +$ra_matrix1 = lp_clone($ra_matrix); +Context()->texStrings; +BEGIN_TEXT +basis(6,7) $BR +\[\{lp_display_mm($ra_matrix1)\}\] $PAR +\{lp_pivot($ra_matrix1,0,0),''\}$BR pivot(1,1) basis(1,7)$PAR +\[\{lp_display_mm($ra_matrix1)\}\] $PAR +\{lp_pivot($ra_matrix1,1,1),''\}$BR pivot(2,2), basis(1,2) $PAR +\[\{lp_display_mm($ra_matrix1)\}\] $PAR +\{lp_pivot($ra_matrix1,0,2),''\}$BR pivot(1,3), basis(2,3) $PAR +\[\{lp_display_mm($ra_matrix1)\}\] $PAR +END_TEXT + + +$basis2_3_string = update_tableau($original_matrix, Set(2,3)); + + +BEGIN_TEXT +update tableau method $PAR +basis(6,7)$BR +\[ \{ update_tableau($original_matrix, Set(6,7)) \} \] $PAR +basis(1,7) $BR +\[ \{ update_tableau($original_matrix, Set(1,7)) \} \] $PAR +basis(1,2) $BR +\[ \{ update_tableau($original_matrix, Set(1,2)) \} \] $PAR +basis(2,4) $BR +\[ \{ update_tableau($original_matrix, Set(2,4) ) \} \] $PAR +basis(2,3) $BR +\[ $basis2_3_string \] $PAR +basis(2,3) $BR +\[ $basis2_3_string \] $PAR +\[ update_tableau($original_matrix, Set(2,3)\] +END_TEXT +Context()->normalStrings; +TEXT($END_ONE_COLUMN); +ENDDOCUMENT(); diff --git a/t/matrix_tableau_tests/tableau-test6a.pg b/t/matrix_tableau_tests/tableau-test6a.pg new file mode 100644 index 0000000000..d13e303684 --- /dev/null +++ b/t/matrix_tableau_tests/tableau-test6a.pg @@ -0,0 +1,186 @@ +DOCUMENT(); +loadMacros( +"PGstandard.pl", +"MathObjects.pl", +"parserPopUp.pl", +"unionLists.pl", +"MatrixReduce.pl", +#"AppletObjects.pl", +"PGessaymacros.pl", +"PGmatrixmacros.pl", +"LinearProgramming.pl", +"parserLinearInequality.pl", +"quickMatrixEntry.pl", +#"scaffold.pl", +"tableau.pl", +#"gage_matrix_ops.pl", +"PGinfo.pl", +"source.pl", +"PGcourse.pl", +); + +TEXT(beginproblem()); +TEXT($BEGIN_ONE_COLUMN); +$showPartialCorrectAnswers = 1; + +INITIALIZE_QUICK_MATRIX_ENTRY(); + +$money_total = 6000; +$time_total = 600; + +# Bill +$bill_money_commitment = 5000; #dollars +$bill_time_commitment = 400; # hours +$bill_profit = 4700; +# Steve +$steve_money_commitment = 3000; +$steve_time_commitment = 500; +$steve_profit = 4500; + + +#Hack to prevent domain conflict in answer. +Context()->variables->add(p1=>'Real',p2=>'Real'); +$objfun1 = Formula("${bill_profit}p1 + ${steve_profit}p2"); +# why can't the formula be defined within context "linearInequality"? + +Context("LinearInequality"); +Context()->variables->add(p1=>'Real',p2=>'Real'); +Context()->strings->add("Essay Answer" =>{}); +Context()->strings->add('Minimize'=>{},'Maximize'=>{}, "?"=>{}); +Context()->strings->add('Yes'=>{},'No'=>{}); +Context()->flags->set({tolType=>"absolute",tolerance=>.001}); +Context()->flags->set( + zeroLevel=>0.001, + zeroLevelTol=>.001 +); +# $objfun1 = Formula("${bill_profit}p1 + ${steve_profit}p2"); + +our $context=Context(); + +sub basis_size { + my $basis = shift; + return scalar( @{$basis->data} ); +} +sub basis_matrix { + my $original_matrix = shift; + my $basis = shift; + my $basis_size=basis_size($basis); + my $basis_matrix = $original_matrix->submatrix(rows=>[1..$basis_size],columns=>($basis->data)); + return $basis_matrix; +} + +sub update_tableau { + my $original_matrix = shift; + my $basis = shift; + # $basis is a Set object + # last row is numbered $basis_size+1; + my $basis_matrix = basis_matrix($original_matrix); + my $det = Real($basis_matrix->det); +# $det = Real($basis_matrix->det); +# $det = $det->value; +# $det=Real(130000); #Real(1300000); +# DEBUG_MESSAGE("det = $det ", ref($det)); +# my $basis2= Matrix($basis_matrix); +# #DEBUG_MESSAGE(($basis2->det)*($basis2->inverse) *$original_matrix->row_slice(1..$basis_size), $PAR); + my $obj_coeff = - ($original_matrix->row($basis_size+1)); + #obj function coeff corresponding to basis variables/columns + $cB = Matrix($obj_coeff->column_slice($basis->data)); + # get A | S |b portion of tableau and multiply by det(B)B^-1 + my $current_matrix= ($det)*($basis_matrix->inverse)*($original_matrix->row_slice(1..$basis_size)); + my $new_obj_row = ($cB*$current_matrix)->row(1) - ($det)*($obj_coeff); + my $current_tableau = Matrix(@{$current_matrix->extract_rows},Matrix($new_obj_row)); + return $current_tableau; +} +# the full tableau has one more row -- the objective function +sub display_tableau_state { + my ($original_matrix,$tableau, $matrix, $basis,$pivot)= @_; + $basis = $basis->sort; + my $basis_size = @{$basis->data} ; + my $basis_matrix = matrix_from_submatrix($original_matrix, rows=>[1..$basis_size],columns=>$basis->data); + my $normalized_tableau = $matrix; + my $reduced_matrix = matrix_from_matrix_rows($matrix,1..$basis_size); + warn "basis matrix is $basis_matrix, $reduced_matrix"; + my $normalized_reduced_matrix = ($basis_matrix->det)*$reduced_matrix; + # calculate current tableau by multiplying by the inverse of the basis + my $obj_coeff = - $original_matrix->row($basis_size+1); + my $cB = Matrix($obj_coeff->column_slice($basis->data)); + my $current_matrix = ($basis_matrix->det)*($basis_matrix->inverse)*matrix_from_matrix_rows($original_matrix,1..$basis_size); + my $new_obj_row = ($cB*$current_matrix)->row(1) - ($basis_matrix->det)*($obj_coeff); + my $current_tableau = Matrix(@{$current_matrix->extract_rows},Matrix($new_obj_row)); + $current_tableau = update_tableau($original_matrix, $basis); + + #DEBUG_MESSAGE("get state variables for $matrix using basis $basis"); + @statevars1 = get_tableau_variable_values($matrix, $basis); + # get z value + $statevars1 = ~~@statevars1; + $state = Matrix([[@statevars1]]); + + + return " + + pivot: \($pivot\) basis: \( $basis\) $PAR basis matrix \(". + display_matrix_mm($basis_matrix)."\) $PAR + tableau and normalized current tableau $PAR + \(" . lp_display_mm($tableau) . "\)\(".lp_display_mm($normalized_tableau). " \) $PAR + original matrix and current matrix calculated by Binverse*original $PAR + \(". + display_matrix_mm(matrix_from_matrix_rows($original_matrix,1..$basis_size)). + "\) \(". + display_matrix_mm($current_matrix). + "\) $PAR + objective coefficients = \( $obj_coeff \) $PAR + basis objective_coefficients = \($cB\) $PAR + new tableau \[". + lp_display_mm($current_tableau). + "\] $PAR + state: \($state)\) $PAR + " +} + + +#### problem starts here: + +Context($context); +$ra_matrix = [[-1,-$bill_money_commitment,-$bill_time_commitment, -1, 0, 1,0,0,0,-$bill_profit], + [-1,-$steve_money_commitment,-$steve_time_commitment, 0, -1, 0,1,0,0,-$steve_profit], + [1,0,0,0,0,0,0,0,1,0]]; + +# [0,$money_total,$time_total,1,1,0,0,1,0,0]]; +$original_matrix = Matrix($ra_matrix); + +$ra_matrix1 = lp_clone($ra_matrix); + +TEXT("no pivot det67 $det67$BR"); +$det67 = $det = basis_matrix($original_matrix,Set(6,7))->det->value; +$display1 = lp_display_mm($det67*Matrix($ra_matrix)); + +TEXT("det17 $det17$BR"); +$det17 = $det = basis_matrix($original_matrix,Set(1,7))->det; +$display2=lp_display_mm(Matrix( lp_pivot($ra_matrix1,0,0) )); + +TEXT("pivot (2,2), det12 $det12$BR"); +$det12 = $det = basis_matrix($original_matrix,Set(1,2))->det; +$display3 = lp_display_mm(Matrix( lp_pivot($ra_matrix1,1,1) )); + +TEXT("pivot (1,3) det23 $det23$BR"); +$det23 = $det = basis_matrix($original_matrix,Set(2,3))->det; +$display4 = lp_display_mm(Matrix( lp_pivot($ra_matrix1,0,2) )); + +Context()->texStrings; + +BEGIN_TEXT +tableau-test6a.pg $BR + +no pivot -- basis(6,7) $PAR +\[ $display1 \] $PAR + pivot(1,1) basis(1,7)$PAR +\[ $display2 \] $PAR +pivot(2,2), basis(1,2) $PAR +\[ $display3 \] $PAR +pivot(1,3), basis(2,3) $PAR +\[ $display4 \] $PAR +END_TEXT + +Context()->normalStrings; +TEXT($END_ONE_COLUMN); +ENDDOCUMENT(); diff --git a/t/matrix_tableau_tests/tableau-test6b.pg b/t/matrix_tableau_tests/tableau-test6b.pg new file mode 100644 index 0000000000..c0d95c1184 --- /dev/null +++ b/t/matrix_tableau_tests/tableau-test6b.pg @@ -0,0 +1,190 @@ +DOCUMENT(); +loadMacros( +"PGstandard.pl", +"MathObjects.pl", +"parserPopUp.pl", +"unionLists.pl", +"MatrixReduce.pl", +#"AppletObjects.pl", +"PGessaymacros.pl", +"PGmatrixmacros.pl", +"LinearProgramming.pl", +"parserLinearInequality.pl", +"quickMatrixEntry.pl", +#"scaffold.pl", +"tableau.pl", +#"gage_matrix_ops.pl", +"PGinfo.pl", +"source.pl", +"PGcourse.pl", +); + +TEXT(beginproblem()); +TEXT($BEGIN_ONE_COLUMN); +$showPartialCorrectAnswers = 1; + +INITIALIZE_QUICK_MATRIX_ENTRY(); + +$money_total = 6000; +$time_total = 600; + +# Bill +$bill_money_commitment = 5000; #dollars +$bill_time_commitment = 400; # hours +$bill_profit = 4700; +# Steve +$steve_money_commitment = 3000; +$steve_time_commitment = 500; +$steve_profit = 4500; + + +#Hack to prevent domain conflict in answer. +Context()->variables->add(p1=>'Real',p2=>'Real'); +$objfun1 = Formula("${bill_profit}p1 + ${steve_profit}p2"); +# why can't the formula be defined within context "linearInequality"? + +Context("LinearInequality"); +Context()->variables->add(p1=>'Real',p2=>'Real'); +Context()->strings->add("Essay Answer" =>{}); +Context()->strings->add('Minimize'=>{},'Maximize'=>{}, "?"=>{}); +Context()->strings->add('Yes'=>{},'No'=>{}); +Context()->flags->set({tolType=>"absolute",tolerance=>.001}); +Context()->flags->set( + zeroLevel=>0.001, + zeroLevelTol=>.001 +); +# $objfun1 = Formula("${bill_profit}p1 + ${steve_profit}p2"); + +our $context=Context(); + +sub basis_size { + my $basis = shift; + return scalar( @{$basis->data} ); +} +sub basis_matrix { + my $original_matrix = shift; + my $basis = shift; + my $basis_size=basis_size($basis); + my $basis_matrix = $original_matrix->submatrix(rows=>[1..$basis_size],columns=>($basis->data)); + return $basis_matrix; +} + +sub update_tableau { + my $original_matrix = shift; + my $basis = shift; #basis columns -- a Set + # $basis is a Set object + # last row is numbered $basis_size+1; + my $context= Context(); + #Context()->normalStrings; + my $basis_matrix = basis_matrix($original_matrix, $basis); + my $basis_size=basis_size($basis); + my $det = Real($basis_matrix->det)->value; # prevent tex output from det + my $obj_coeff = - ($original_matrix->row($basis_size+1)); + #obj function coeff corresponding to basis variables/columns + $cB = Matrix($obj_coeff->column_slice($basis->data)); + # get A | S |b portion of tableau and multiply by det(B)B^-1 + my $current_matrix= ($det)*($basis_matrix->inverse)*($original_matrix->row_slice(1..$basis_size)); + my $new_obj_row = ($cB*$current_matrix)->row(1) - ($det)*($obj_coeff); + my $current_tableau = Matrix(@{$current_matrix->extract_rows},Matrix($new_obj_row)); + #Context()->texStrings; # restore context + return $current_tableau; +} +# the full tableau has one more row -- the objective function +sub display_tableau_state { + my ($original_matrix,$tableau, $matrix, $basis,$pivot)= @_; + $basis = $basis->sort; + my $basis_size = @{$basis->data} ; + my $basis_matrix = matrix_from_submatrix($original_matrix, rows=>[1..$basis_size],columns=>$basis->data); + my $normalized_tableau = $matrix; + my $reduced_matrix = matrix_from_matrix_rows($matrix,1..$basis_size); + warn "basis matrix is $basis_matrix, $reduced_matrix"; + my $normalized_reduced_matrix = ($basis_matrix->det)*$reduced_matrix; + # calculate current tableau by multiplying by the inverse of the basis + my $obj_coeff = - $original_matrix->row($basis_size+1); + my $cB = Matrix($obj_coeff->column_slice($basis->data)); + my $current_matrix = ($basis_matrix->det)*($basis_matrix->inverse)*matrix_from_matrix_rows($original_matrix,1..$basis_size); + my $new_obj_row = ($cB*$current_matrix)->row(1) - ($basis_matrix->det)*($obj_coeff); + my $current_tableau = Matrix(@{$current_matrix->extract_rows},Matrix($new_obj_row)); + $current_tableau = update_tableau($original_matrix, $basis); + + #DEBUG_MESSAGE("get state variables for $matrix using basis $basis"); + @statevars1 = get_tableau_variable_values($matrix, $basis); + # get z value + $statevars1 = ~~@statevars1; + $state = Matrix([[@statevars1]]); + + + return " + + pivot: \($pivot\) basis: \( $basis\) $PAR basis matrix \(". + display_matrix_mm($basis_matrix)."\) $PAR + tableau and normalized current tableau $PAR + \(" . lp_display_mm($tableau) . "\)\(".lp_display_mm($normalized_tableau). " \) $PAR + original matrix and current matrix calculated by Binverse*original $PAR + \(". + display_matrix_mm(matrix_from_matrix_rows($original_matrix,1..$basis_size)). + "\) \(". + display_matrix_mm($current_matrix). + "\) $PAR + objective coefficients = \( $obj_coeff \) $PAR + basis objective_coefficients = \($cB\) $PAR + new tableau \[". + lp_display_mm($current_tableau). + "\] $PAR + state: \($state)\) $PAR + " +} + + +#### problem starts here: + +Context($context); +$ra_matrix = [[-1,-$bill_money_commitment,-$bill_time_commitment, -1, 0, 1,0,0,0,-$bill_profit], + [-1,-$steve_money_commitment,-$steve_time_commitment, 0, -1, 0,1,0,0,-$steve_profit], + [1,0,0,0,0,0,0,0,1,0]]; + +# [0,$money_total,$time_total,1,1,0,0,1,0,0]]; +$original_matrix = Matrix($ra_matrix); + +$ra_matrix1 = lp_clone($ra_matrix); + + +$string67 = lp_display_mm( update_tableau($original_matrix, Set(6,7)) ); +$string17 = lp_display_mm( update_tableau($original_matrix, Set(1,7)) ); +$string12 = lp_display_mm( update_tableau($original_matrix, Set(1,2)) ); +$string23 = lp_display_mm( update_tableau($original_matrix, Set(2,3)) ); +$string32 = lp_display_mm( update_tableau($original_matrix, Set(3,2)) ); + +Context()->texStrings; +BEGIN_TEXT +update tableau method tableau-test6b$PAR +basis(6,7)$BR +\[ $string67 \] $PAR +basis(1,7) $BR +\[ $string17 \] $PAR +basis(1,2) $BR +\[ $string12 \] $PAR +basis(2,3) $BR +\[ $string23\] $PAR +basis(3,2) $BR +\[ $string32 \] $PAR + +update tableau -- evaluation within BEGIN_TEXT/END_TEXT $PAR +basis(6,7)$BR +\[ \{lp_display_mm( update_tableau($original_matrix, Set(6,7)) )\} \] $PAR +basis(1,7) $BR +\[ \{lp_display_mm( update_tableau($original_matrix, Set(1,7)) )\} \] $PAR +basis(1,2) $BR +\[ \{lp_display_mm( update_tableau($original_matrix, Set(1,2)) )\} \] $PAR +basis(2,3) $BR +\[ \{lp_display_mm( update_tableau($original_matrix, Set(2,3)) )\}\] $PAR +basis(3,2) $BR +\[ \{lp_display_mm( update_tableau($original_matrix, Set(3,2)) )\} \] $PAR + + + + +END_TEXT +Context()->normalStrings; +TEXT($END_ONE_COLUMN); +ENDDOCUMENT(); diff --git a/t/matrix_tableau_tests/tableau-test6c.pg b/t/matrix_tableau_tests/tableau-test6c.pg new file mode 100644 index 0000000000..6db437b1ec --- /dev/null +++ b/t/matrix_tableau_tests/tableau-test6c.pg @@ -0,0 +1,237 @@ +DOCUMENT(); +loadMacros( +"PGstandard.pl", +"MathObjects.pl", +"parserPopUp.pl", +"unionLists.pl", +"MatrixReduce.pl", +#"AppletObjects.pl", +"PGessaymacros.pl", +"PGmatrixmacros.pl", +"PGML.pl", +"LinearProgramming.pl", +"parserLinearInequality.pl", +"quickMatrixEntry.pl", +#"scaffold.pl", +"tableau.pl", +#"gage_matrix_ops.pl", +"PGinfo.pl", +"source.pl", +"PGcourse.pl", +); + +TEXT(beginproblem()); +TEXT($BEGIN_ONE_COLUMN); +$showPartialCorrectAnswers = 1; + +INITIALIZE_QUICK_MATRIX_ENTRY(); + +$money_total = 6000; +$time_total = 600; + +# Bill +$bill_money_commitment = 5000; #dollars +$bill_time_commitment = 400; # hours +$bill_profit = 4700; +# Steve +$steve_money_commitment = 3000; +$steve_time_commitment = 500; +$steve_profit = 4500; + + +#Hack to prevent domain conflict in answer. +Context()->variables->add(p1=>'Real',p2=>'Real'); +$objfun1 = Formula("${bill_profit}p1 + ${steve_profit}p2"); +# why can't the formula be defined within context "linearInequality"? + +Context("LinearInequality"); +Context()->variables->add(p1=>'Real',p2=>'Real'); +Context()->strings->add("Essay Answer" =>{}); +Context()->strings->add('Minimize'=>{},'Maximize'=>{}, "?"=>{}); +Context()->strings->add('Yes'=>{},'No'=>{}); +Context()->flags->set({tolType=>"absolute",tolerance=>.001}); +Context()->flags->set( + zeroLevel=>0.001, + zeroLevelTol=>.001 +); +# $objfun1 = Formula("${bill_profit}p1 + ${steve_profit}p2"); + +our $context=Context(); + +sub basis_size { + my $basis = shift; + return scalar( @{$basis->data} ); +} +sub basis_matrix { + my $original_matrix = shift; + my $basis = shift; + my $basis_size=basis_size($basis); + my $basis_matrix = $original_matrix->submatrix(rows=>[1..$basis_size],columns=>($basis->data)); + return $basis_matrix; +} + +sub update_tableau { + my $original_matrix = shift; + my $basis = shift; #basis columns -- a Set + # $basis is a Set object + # last row is numbered $basis_size+1; + my $context= Context(); + #Context()->normalStrings; + my $basis_matrix = basis_matrix($original_matrix, $basis); + my $basis_size=basis_size($basis); + my $det = Real($basis_matrix->det)->value; # prevent tex output from det + my $obj_coeff = - ($original_matrix->row($basis_size+1)); + #obj function coeff corresponding to basis variables/columns + $cB = Matrix($obj_coeff->column_slice($basis->data)); + # get A | S |b portion of tableau and multiply by det(B)B^-1 + my $current_matrix= ($det)*($basis_matrix->inverse)*($original_matrix->row_slice(1..$basis_size)); + my $new_obj_row = ($cB*$current_matrix)->row(1) - ($det)*($obj_coeff); + my $current_tableau = Matrix(@{$current_matrix->extract_rows},Matrix($new_obj_row)); + #Context()->texStrings; # restore context + return $current_tableau; +} +# the full tableau has one more row -- the objective function +sub display_tableau_state { + my ($original_matrix,$tableau, $matrix, $basis,$pivot)= @_; + $basis = $basis->sort; + my $basis_size = @{$basis->data} ; + my $basis_matrix = matrix_from_submatrix($original_matrix, rows=>[1..$basis_size],columns=>$basis->data); + my $normalized_tableau = $matrix; + my $reduced_matrix = matrix_from_matrix_rows($matrix,1..$basis_size); + warn "basis matrix is $basis_matrix, $reduced_matrix"; + my $normalized_reduced_matrix = ($basis_matrix->det)*$reduced_matrix; + # calculate current tableau by multiplying by the inverse of the basis + my $obj_coeff = - $original_matrix->row($basis_size+1); + my $cB = Matrix($obj_coeff->column_slice($basis->data)); + my $current_matrix = ($basis_matrix->det)*($basis_matrix->inverse)*matrix_from_matrix_rows($original_matrix,1..$basis_size); + my $new_obj_row = ($cB*$current_matrix)->row(1) - ($basis_matrix->det)*($obj_coeff); + my $current_tableau = Matrix(@{$current_matrix->extract_rows},Matrix($new_obj_row)); + $current_tableau = update_tableau($original_matrix, $basis); + + #DEBUG_MESSAGE("get state variables for $matrix using basis $basis"); + @statevars1 = get_tableau_variable_values($matrix, $basis); + # get z value + $statevars1 = ~~@statevars1; + $state = Matrix([[@statevars1]]); + + + return " + + pivot: \($pivot\) basis: \( $basis\) $PAR basis matrix \(". + display_matrix_mm($basis_matrix)."\) $PAR + tableau and normalized current tableau $PAR + \(" . lp_display_mm($tableau) . "\)\(".lp_display_mm($normalized_tableau). " \) $PAR + original matrix and current matrix calculated by Binverse*original $PAR + \(". + display_matrix_mm(matrix_from_matrix_rows($original_matrix,1..$basis_size)). + "\) \(". + display_matrix_mm($current_matrix). + "\) $PAR + objective coefficients = \( $obj_coeff \) $PAR + basis objective_coefficients = \($cB\) $PAR + new tableau \[". + lp_display_mm($current_tableau). + "\] $PAR + state: \($state)\) $PAR + " +} + + +#### problem starts here: + +Context($context); +#constraint matrix +$A = Matrix([[-1,-$bill_money_commitment,-$bill_time_commitment, -1, 0], + [ -1,-$steve_money_commitment,-$steve_time_commitment, 0, -1, ]]); +$b = Matrix([[-$bill_profit,-$steve_profit]])->transpose; +$c = Matrix([1,0,0,0,0]); + +my $tableau1 = Tableau->new(A=>$A, b=>$b, c=>$c); +$m = $tableau1->{M}; +$k = $tableau1->current_tableau; +$basis_cols24 = $tableau1->basis(2,4); + +$k24 = $tableau1->current_tableau; +$basis_cols42 = $tableau1->basis(4,2); + +$k42 = $tableau1->current_tableau; + +Context()->texStrings; +BEGIN_PGML + +A [`[$A]`], b [`[$b]`], C [`[$c]`] + +dimensions = [` [@ join(" ", $b->row(1)) @] `] +[` [@ join(" ", $b->column(1)) @] `] + +tableau [` [$tableau1->{M}] `] + +tableau m [`[$m]`] + +tableau k [`[$k]`] + +---------------------------------- +basis columns [$basis_cols24] + +tableau k24 [`[$k24]`] + +basis columns [$basis_cols42] + +tableau k42 [`[$k42]`] + +END_PGML +Context()->normalStrings; + +ENDDOCUMENT + + + +$ra_matrix = [[-1,-$bill_money_commitment,-$bill_time_commitment, -1, 0, 1,0,0,0,-$bill_profit], + [-1,-$steve_money_commitment,-$steve_time_commitment, 0, -1, 0,1,0,0,-$steve_profit], + [1,0,0,0,0,0,0,0,1,0]]; + +# [0,$money_total,$time_total,1,1,0,0,1,0,0]]; +$original_matrix = Matrix($ra_matrix); + +$ra_matrix1 = lp_clone($ra_matrix); + + +$string67 = lp_display_mm( update_tableau($original_matrix, Set(6,7)) ); +$string17 = lp_display_mm( update_tableau($original_matrix, Set(1,7)) ); +$string12 = lp_display_mm( update_tableau($original_matrix, Set(1,2)) ); +$string23 = lp_display_mm( update_tableau($original_matrix, Set(2,3)) ); +$string32 = lp_display_mm( update_tableau($original_matrix, Set(3,2)) ); + +Context()->texStrings; +BEGIN_TEXT +update tableau method tableau-test6b$PAR +basis(6,7)$BR +\[ $string67 \] $PAR +basis(1,7) $BR +\[ $string17 \] $PAR +basis(1,2) $BR +\[ $string12 \] $PAR +basis(2,3) $BR +\[ $string23\] $PAR +basis(3,2) $BR +\[ $string32 \] $PAR + +update tableau -- evaluation within BEGIN_TEXT/END_TEXT $PAR +basis(6,7)$BR +\[ \{lp_display_mm( update_tableau($original_matrix, Set(6,7)) )\} \] $PAR +basis(1,7) $BR +\[ \{lp_display_mm( update_tableau($original_matrix, Set(1,7)) )\} \] $PAR +basis(1,2) $BR +\[ \{lp_display_mm( update_tableau($original_matrix, Set(1,2)) )\} \] $PAR +basis(2,3) $BR +\[ \{lp_display_mm( update_tableau($original_matrix, Set(2,3)) )\}\] $PAR +basis(3,2) $BR +\[ \{lp_display_mm( update_tableau($original_matrix, Set(3,2)) )\} \] $PAR + + + + +END_TEXT +Context()->normalStrings; +TEXT($END_ONE_COLUMN); +ENDDOCUMENT(); diff --git a/t/matrix_tableau_tests/tableau-test6d.pg b/t/matrix_tableau_tests/tableau-test6d.pg new file mode 100644 index 0000000000..37f3b4bc43 --- /dev/null +++ b/t/matrix_tableau_tests/tableau-test6d.pg @@ -0,0 +1,206 @@ +DOCUMENT(); +loadMacros( +"PGstandard.pl", +"MathObjects.pl", +"parserPopUp.pl", +"unionLists.pl", +"MatrixReduce.pl", +#"AppletObjects.pl", +"PGessaymacros.pl", +"PGmatrixmacros.pl", +"LinearProgramming.pl", +"parserLinearInequality.pl", +"quickMatrixEntry.pl", +#"scaffold.pl", +"tableau.pl", +#"gage_matrix_ops.pl", +"PGinfo.pl", +"source.pl", +"PGcourse.pl", +); + +TEXT(beginproblem()); +TEXT($BEGIN_ONE_COLUMN); +$showPartialCorrectAnswers = 1; + +INITIALIZE_QUICK_MATRIX_ENTRY(); + +$money_total = 6000; +$time_total = 600; + +# Bill +$bill_money_commitment = 5000; #dollars +$bill_time_commitment = 400; # hours +$bill_profit = 4700; +# Steve +$steve_money_commitment = 3000; +$steve_time_commitment = 500; +$steve_profit = 4500; + + +#Hack to prevent domain conflict in answer. +Context()->variables->add(p1=>'Real',p2=>'Real'); +$objfun1 = Formula("${bill_profit}p1 + ${steve_profit}p2"); +# why can't the formula be defined within context "linearInequality"? + +Context("LinearInequality"); +Context()->variables->add(p1=>'Real',p2=>'Real'); +Context()->strings->add("Essay Answer" =>{}); +Context()->strings->add('Minimize'=>{},'Maximize'=>{}, "?"=>{}); +Context()->strings->add('Yes'=>{},'No'=>{}); +Context()->flags->set({tolType=>"absolute",tolerance=>.001}); +Context()->flags->set( + zeroLevel=>0.001, + zeroLevelTol=>.001 +); +# $objfun1 = Formula("${bill_profit}p1 + ${steve_profit}p2"); + +our $context=Context(); + +sub basis_size { + my $basis = shift; + return scalar( @{$basis->data} ); +} +sub basis_matrix { + my $original_matrix = shift; + my $basis = shift; + my $basis_size=basis_size($basis); + my $basis_matrix = $original_matrix->submatrix(rows=>[1..$basis_size],columns=>($basis->data)); + return $basis_matrix; +} + +sub update_tableau { + my $original_matrix = shift; + my $basis = shift; #basis columns -- a Set + # $basis is a Set object + # last row is numbered $basis_size+1; + my $context= Context(); + #Context()->normalStrings; + my $basis_matrix = basis_matrix($original_matrix, $basis); + my $basis_size=basis_size($basis); + my $det = Real($basis_matrix->det)->value; # prevent tex output from det + my $obj_coeff = - ($original_matrix->row($basis_size+1)); + #obj function coeff corresponding to basis variables/columns + $cB = Matrix($obj_coeff->column_slice($basis->data)); + # get A | S |b portion of tableau and multiply by det(B)B^-1 + my $current_matrix= ($det)*($basis_matrix->inverse)*($original_matrix->row_slice(1..$basis_size)); + my $new_obj_row = ($cB*$current_matrix)->row(1) - ($det)*($obj_coeff); + my $current_tableau = Matrix(@{$current_matrix->extract_rows},Matrix($new_obj_row)); + #Context()->texStrings; # restore context + return $current_tableau; +} +# the full tableau has one more row -- the objective function +sub display_tableau_state { + my ($original_matrix,$tableau, $matrix, $basis,$pivot)= @_; + $basis = $basis->sort; + my $basis_size = @{$basis->data} ; + my $basis_matrix = matrix_from_submatrix($original_matrix, rows=>[1..$basis_size],columns=>$basis->data); + my $normalized_tableau = $matrix; + my $reduced_matrix = matrix_from_matrix_rows($matrix,1..$basis_size); + warn "basis matrix is $basis_matrix, $reduced_matrix"; + my $normalized_reduced_matrix = ($basis_matrix->det)*$reduced_matrix; + # calculate current tableau by multiplying by the inverse of the basis + my $obj_coeff = - $original_matrix->row($basis_size+1); + my $cB = Matrix($obj_coeff->column_slice($basis->data)); + my $current_matrix = ($basis_matrix->det)*($basis_matrix->inverse)*matrix_from_matrix_rows($original_matrix,1..$basis_size); + my $new_obj_row = ($cB*$current_matrix)->row(1) - ($basis_matrix->det)*($obj_coeff); + my $current_tableau = Matrix(@{$current_matrix->extract_rows},Matrix($new_obj_row)); + $current_tableau = update_tableau($original_matrix, $basis); + + #DEBUG_MESSAGE("get state variables for $matrix using basis $basis"); + @statevars1 = get_tableau_variable_values($matrix, $basis); + # get z value + $statevars1 = ~~@statevars1; + $state = Matrix([[@statevars1]]); + + + return " + + pivot: \($pivot\) basis: \( $basis\) $PAR basis matrix \(". + display_matrix_mm($basis_matrix)."\) $PAR + tableau and normalized current tableau $PAR + \(" . lp_display_mm($tableau) . "\)\(".lp_display_mm($normalized_tableau). " \) $PAR + original matrix and current matrix calculated by Binverse*original $PAR + \(". + display_matrix_mm(matrix_from_matrix_rows($original_matrix,1..$basis_size)). + "\) \(". + display_matrix_mm($current_matrix). + "\) $PAR + objective coefficients = \( $obj_coeff \) $PAR + basis objective_coefficients = \($cB\) $PAR + new tableau \[". + lp_display_mm($current_tableau). + "\] $PAR + state: \($state)\) $PAR + " +} + + +#### problem starts here: + +Context($context); + +$ra_matrix = [[-1,-$bill_money_commitment,-$bill_time_commitment, -1, 0, 1,0,0,0,-$bill_profit], + [-1,-$steve_money_commitment,-$steve_time_commitment, 0, -1, 0,1,0,0,-$steve_profit], + [1,0,0,0,0,0,0,0,1,0]]; + +# [0,$money_total,$time_total,1,1,0,0,1,0,0]]; +$original_matrix = Matrix($ra_matrix); + +$ra_matrix1 = lp_clone($ra_matrix); + +$A = Matrix([[-1,-$bill_money_commitment,-$bill_time_commitment, -1, 0], + [ -1,-$steve_money_commitment,-$steve_time_commitment, 0, -1, ]]); +$b = Matrix([[-$bill_profit,-$steve_profit]])->transpose; +$c = Matrix([1,0,0,0,0]); + +$tableau1 = Tableau->new(A=>$A, b=>$b, c=>$c); + +# [0,$money_total,$time_total,1,1,0,0,1,0,0]]; + + + +$basis_cols67 = $tableau1->basis(6,7); +$string67 = lp_display_mm( $tableau1->current_tableau ); +$tableau1->basis(1,7); +$string17 = lp_display_mm( $tableau1->current_tableau ); +$tableau1->basis(1,2); +$string12 = lp_display_mm( $tableau1->current_tableau ); +$tableau1->basis(2,3); +$string23 = lp_display_mm( $tableau1->current_tableau ); +$tableau1->basis(3,2); +$string32 = lp_display_mm( $tableau1->current_tableau ); + +Context()->texStrings; +BEGIN_TEXT +update tableau method tableau-test6b$PAR +basis(6,7)$BR +\[ $string67 \] $PAR +basis(1,7) $BR +\[ $string17 \] $PAR +basis(1,2) $BR +\[ $string12 \] $PAR +basis(2,3) $BR +\[ $string23\] $PAR +basis(3,2) $BR +\[ $string32 \] $PAR + +update tableau -- evaluation within BEGIN_TEXT/END_TEXT $PAR +basis(6,7)$BR +\[ \{$tableau1->basis(6,7),lp_display_mm( $tableau1->current_tableau)\} \] $PAR +basis(1,7) $BR +\[ \{$tableau1->basis(1,7),lp_display_mm( $tableau1->current_tableau)\} \] $PAR +basis(1,2) $BR +\[ \{$tableau1->basis(1,2),lp_display_mm( $tableau1->current_tableau)\} \] $PAR +basis(2,3) $BR +\[ \{ $tableau1->basis(2,3),lp_display_mm( $tableau1->current_tableau)\} \] $PAR +basis(3,2) $BR +\[ \{lp_display_mm( update_tableau($original_matrix, Set(3,2)) )\} \] $PAR + + + + +END_TEXT +Context()->normalStrings; +TEXT($END_ONE_COLUMN); +ENDDOCUMENT(); diff --git a/t/matrix_tableau_tests/tableau_test1.pg b/t/matrix_tableau_tests/tableau_test1.pg index cc0a503fe7..1a36ca7be2 100644 --- a/t/matrix_tableau_tests/tableau_test1.pg +++ b/t/matrix_tableau_tests/tableau_test1.pg @@ -36,7 +36,7 @@ $m2 = $m->column_slice([3,2,1]); $list = $m->extract_rows_to_list(2,3); -$b = Matrix([1, 2, 3, 4]); +$b = Matrix([1, 2, 3, 4])->transpose; # make it an n by 1 matrix #TEXT($BR, "vector", $b->data->[1]); $c = Matrix([5, 6, 7]); $t = Tableau->new(A=>$m,b=>$b, c=>$c); diff --git a/t/matrix_tableau_tests/tableau_test3.pg b/t/matrix_tableau_tests/tableau_test3.pg index 4d2b3e879d..705ad114e7 100644 --- a/t/matrix_tableau_tests/tableau_test3.pg +++ b/t/matrix_tableau_tests/tableau_test3.pg @@ -93,6 +93,26 @@ $matrix1 = $original_matrix; $row_size =4; # n-1 n is constraints + objective_row $param_size=2; # original number of parameters in the problem $col_size = $param_size+$row_size+2; # m paramvars+slackvars+objcol+constant_col + +sub update_tableau { + my $original_matrix = shift; + my $basis = shift; + # $basis is a Set object + # last row is numbered $basis_size+1; + my $basis_size=@{$basis->data}; + my $basis_matrix = $original_matrix->submatrix(rows=>[1..$basis_size],columns=>$basis->data); + + my $obj_coeff = - ($original_matrix->row($basis_size+1)); + #obj function coeff corresponding to basis variables/columns + $cB = Matrix($obj_coeff->column_slice($basis->data)); + # get A | S |b portion of tableau and multiply by det(B)B^-1 + my $current_matrix= ($basis_matrix->det)* + ($basis_matrix->inverse)* + $original_matrix->row_slice(1..$basis_size); + my $new_obj_row = ($cB*$current_matrix)->row(1) - ($basis_matrix->det)*($obj_coeff); + my $current_tableau = Matrix(@{$current_matrix->extract_rows},Matrix($new_obj_row)); + return $current_tableau; +} # the full tableau has one more row -- the objective function sub display_tableau_state { my ($original_matrix,$tableau, $matrix, $basis,$pivot)= @_; @@ -101,7 +121,7 @@ sub display_tableau_state { my $basis_matrix = matrix_from_submatrix($original_matrix, rows=>[1..$basis_size],columns=>$basis->data); my $normalized_tableau = $matrix; my $reduced_matrix = matrix_from_matrix_rows($matrix,1..$basis_size); - + warn "basis matrix is $basis_matrix, $reduced_matrix"; my $normalized_reduced_matrix = ($basis_matrix->det)*$reduced_matrix; # calculate current tableau by multiplying by the inverse of the basis my $obj_coeff = - $original_matrix->row($basis_size+1); @@ -109,7 +129,9 @@ sub display_tableau_state { my $current_matrix = ($basis_matrix->det)*($basis_matrix->inverse)*matrix_from_matrix_rows($original_matrix,1..$basis_size); my $new_obj_row = ($cB*$current_matrix)->row(1) - ($basis_matrix->det)*($obj_coeff); my $current_tableau = Matrix(@{$current_matrix->extract_rows},Matrix($new_obj_row)); + $current_tableau = update_tableau($original_matrix, $basis); + #DEBUG_MESSAGE("get state variables for $matrix using basis $basis"); @statevars1 = get_tableau_variable_values($matrix, $basis); # get z value $statevars1 = ~~@statevars1; @@ -396,319 +418,314 @@ pivot2 $pivot2 $PAR \{display_tableau_state($original_matrix,$tableau3, $matrix3, $basis3, $pivot2)\} pivot3 $pivot3 $PAR \{display_tableau_state($original_matrix,$tableau4, $matrix4, $basis4, $pivot3)\} + +$PAR +end of three pivots END_TEXT # # # # # # ######################################################################################################### # # # section 3 answers (3,4,5,6,7,8, 9, 10) # # ######################################################################################################### -# Context()->texStrings; -# -# BEGIN_TEXT -# $PAR -# What is the initial state? $PAR -# \((p_1,p_2,x_3,x_4,x_5,x_6,P)=\): \{ANS($state1->cmp),$state1->ans_array(4)\} $PAR -# -# Using the convention that one removes the "first" nonbasic column that will increase profits -# (the convention that Hurlbert uses) find the first pivot location. (e.g. (2,3) for line 2, column 3). -# and perform the pivot operation. (You can use -# \{htmlLink("http://people.vcu.edu/~ghurlbert/websim/sim.html","websim")\} to do the calculation. ); -# $PAR -# -# Pivot entry: use parentheses as in (row, column): \{ANS($pivot1->cmp),$pivot1->ans_rule \}. -# $PAR -# The matrix has been multiplied by a constant so that all values are integers. This is -# the same result as you will obtain using websim. -# $PAR -# \{MATRIX_ENTRY_BUTTON($matrix2)\} -# $PAR -# $PAR -# \{ANS($matrix2->cmp()), $matrix2->ans_array(6)\} -# -# $PAR -# Current vector (values): \{ANS($state2->cmp),$state2->ans_array(6)\} $PAR -# $HR -# Next pivot entry: \{ANS($pivot2->cmp),$pivot2->ans_rule \}. -# -# $PAR -# \{MATRIX_ENTRY_BUTTON($matrix3)\} -# $PAR -# \{ANS($matrix3->cmp()), $matrix3->ans_array(6)\} -# $HR -# Next pivot entry: \{ANS($pivot3->cmp),$pivot3->ans_rule \} -# results in the tableau: -# $PAR -# \{MATRIX_ENTRY_BUTTON($matrix4)\} -# $PAR $PAR -# \{ANS($matrix4->cmp()), $matrix4->ans_array(6)\} -# $HR -# At this point we are at a local (and therefore a global) maximum and pivoting -# will not increase the profit. -# $PAR -# Your final state is \{ANS($state4->cmp),$state4->ans_array(6)\} $PAR -# (Make sure you are getting all the zeros in the answer! The numbers are quite large -# and overflow the blanks. -- you can make the table blanks wider -# in websim by right clicking on upper left corner of a table. ) -# -# $PAR What percentage of Bill's venture do you invest in? -# \{ANS($p1->cmp), $p1->ans_rule\} -# $PAR What percentage of Steve's venture do you invest in? -# \{ANS($p2->cmp), $p2->ans_rule\} -# $PAR What is your expected profit? -# $DOLLAR\{ANS($profit->cmp),$profit->ans_rule\}. -# -# -# $PAR -# -# END_TEXT -# -# BEGIN_SOLUTION -# Before pivoting the state is \($state1\) $PAR -# Your first pivot should be on \($pivot1\) since that is the left most -# column that -# will increase the profit and the first row has the most limiting ratio. -# $PAR -# \( \{lp_display_mm( [$matrix2->value],top_labels=>$toplabels )\} \) $PAR -# $PAR -# -# $PAR -# \( \{lp_display_mm([$matrix3->value], top_labels=>$toplabels )\}\) -# \( \{side_labels( qw(\text{cash} \text{hours} \text{profits} ) ) \} \) -# $PAR -# -# -# one more -# \( \{lp_display_mm([$matrix4->value],top_labels=>$toplabels )\} \) -# $PAR -# At this point you are done since changing any of the non basic variables will only -# decrease the profit. -# END_SOLUTION -# Context()->normalStrings; -# -# Section::End(); -# -# } # END SECTION 3 +Context()->texStrings; + +BEGIN_TEXT +$PAR +What is the initial state? $PAR + \((p_1,p_2,x_3,x_4,x_5,x_6,P)=\): \{ANS($state1->cmp),$state1->ans_array(4)\} $PAR + +Using the convention that one removes the "first" nonbasic column that will increase profits +(the convention that Hurlbert uses) find the first pivot location. (e.g. (2,3) for line 2, column 3). +and perform the pivot operation. (You can use +\{htmlLink("http://people.vcu.edu/~ghurlbert/websim/sim.html","websim")\} to do the calculation. ); +$PAR + +Pivot entry: use parentheses as in (row, column): \{ANS($pivot1->cmp),$pivot1->ans_rule \}. +$PAR +The matrix has been multiplied by a constant so that all values are integers. This is +the same result as you will obtain using websim. +$PAR +\{MATRIX_ENTRY_BUTTON($matrix2)\} +$PAR +$PAR +\{ANS($matrix2->cmp()), $matrix2->ans_array(6)\} + +$PAR +Current vector (values): \{ANS($state2->cmp),$state2->ans_array(6)\} $PAR +$HR +Next pivot entry: \{ANS($pivot2->cmp),$pivot2->ans_rule \}. + +$PAR +\{MATRIX_ENTRY_BUTTON($matrix3)\} +$PAR +\{ANS($matrix3->cmp()), $matrix3->ans_array(6)\} +$HR +Next pivot entry: \{ANS($pivot3->cmp),$pivot3->ans_rule \} +results in the tableau: +$PAR +\{MATRIX_ENTRY_BUTTON($matrix4)\} +$PAR $PAR +\{ANS($matrix4->cmp()), $matrix4->ans_array(6)\} +$HR +At this point we are at a local (and therefore a global) maximum and pivoting +will not increase the profit. +$PAR +Your final state is \{ANS($state4->cmp),$state4->ans_array(6)\} $PAR +(Make sure you are getting all the zeros in the answer! The numbers are quite large +and overflow the blanks. -- you can make the table blanks wider +in websim by right clicking on upper left corner of a table. ) + +$PAR What percentage of Bill's venture do you invest in? +\{ANS($p1->cmp), $p1->ans_rule\} +$PAR What percentage of Steve's venture do you invest in? +\{ANS($p2->cmp), $p2->ans_rule\} +$PAR What is your expected profit? +$DOLLAR\{ANS($profit->cmp),$profit->ans_rule\}. + + +$PAR + +END_TEXT + +BEGIN_SOLUTION +Before pivoting the state is \($state1\) $PAR +Your first pivot should be on \($pivot1\) since that is the left most +column that +will increase the profit and the first row has the most limiting ratio. +$PAR +\( \{lp_display_mm( [$matrix2->value],top_labels=>$toplabels )\} \) $PAR +$PAR + +$PAR + \( \{lp_display_mm([$matrix3->value], top_labels=>$toplabels )\}\) +\( \{side_labels( qw(\text{cash} \text{hours} \text{profits} ) ) \} \) +$PAR + + +one more + \( \{lp_display_mm([$matrix4->value],top_labels=>$toplabels )\} \) + $PAR +At this point you are done since changing any of the non basic variables will only +decrease the profit. +END_SOLUTION +Context()->normalStrings; # + # ######################################################################################################### # # Section 4 dual problem # ######################################################################################################### -# -# SECTION4: { -# Section::Begin("Part 4: The dual problem"); -# -# Context()->variables->add(y1=>'Real',y2=>'Real', y3=>'Real', y4=>'Real', w=>'Real',y0=>'Real'); -# $dual_constraint1 = Compute("${bill_money_commitment}y1 +${bill_time_commitment}y2 +y3 >=$bill_profit"); -# $dual_constraint2 = Compute("${steve_money_commitment}y1 +${steve_time_commitment}y2 +y4 >=$steve_profit"); -# $dual_constraints= List($dual_constraint1, $dual_constraint2); -# -# $dual_objfun = Formula("${money_total}y1 +${time_total}y2 + y3 +y4"); -# $popupmaxmin = PopUp(["?","Maximize", "Minimize"], "Minimize"); -# -# Context()->texStrings; -# -# BEGIN_TEXT -# Construct the dual problem for the linear optimization problem above. The first goal is to calculate -# an upper bound for the possible profit in the LOP using linear combinations of the inequality constraints. -# Then formulate the search for the best of these upper bounds in such a way that it -# becomes a new LOP -- the dual problem. Use variables \(y_1,y_2,y_3,y_4\) to create linear combinations -# of the constraints on money, time and the total probabilities \(p_1\) and \(p_2\) respectively, -# and create a linear function \(w = Ay_1 + By_2 +Cy_3 +Dy_4\) which guarantees that \(w\) will -# be larger than any profit one could make. What constraints must \(y_1\dots y_4\) satisfy? -# $PAR -# \{ANS($dual_constraints->cmp->withPostFilter( -# linebreak_at_commas() -# )),ans_box(4,80)\} -# $PAR The objective function would be: -# \(w = \) \{ANS($dual_objfun->cmp), $dual_objfun->ans_rule(50)\} -# $PAR -# To get the best possible estimate for the profit in the original problem -# you would want to \{ANS($popupmaxmin->cmp),$popupmaxmin->menu\} \(w\); -# $PAR -# Why? -# $PAR -# \{ANS(essay_cmp()), essay_box(5,80)\} -# END_TEXT -# -# BEGIN_SOLUTION -# The profit is less than the minimum of \( w = $dual_objfun \) subject to -# -# \[\begin{aligned} -# $dual_constraint1 & \\ -# $dual_constraint2 & -# \end{aligned} -# \] -# The \(y_i\) are the coefficients used to add up the constraint inequalities of the primary problem -# so as to estimate an upper bound for the profit. If the \(y_i\) are positive values satisfying -# the constraints then \(w\) will be greater than or equal to the profit. We get the most precise -# estimate by finding values of \(y_i\), satisfying the constraints, which give the smallest -# possible value for \( w\). -# -# END_SOLUTION -# Context()->normalStrings; -# -# Section::End(); -# } # END SECTION 4 -# + + +Context()->variables->add(y1=>'Real',y2=>'Real', y3=>'Real', y4=>'Real', w=>'Real',y0=>'Real'); +$dual_constraint1 = Compute("${bill_money_commitment}y1 +${bill_time_commitment}y2 +y3 >=$bill_profit"); +$dual_constraint2 = Compute("${steve_money_commitment}y1 +${steve_time_commitment}y2 +y4 >=$steve_profit"); +$dual_constraints= List($dual_constraint1, $dual_constraint2); + +$dual_objfun = Formula("${money_total}y1 +${time_total}y2 + y3 +y4"); +$popupmaxmin = PopUp(["?","Maximize", "Minimize"], "Minimize"); + +Context()->texStrings; + +BEGIN_TEXT +Construct the dual problem for the linear optimization problem above. The first goal is to calculate +an upper bound for the possible profit in the LOP using linear combinations of the inequality constraints. +Then formulate the search for the best of these upper bounds in such a way that it +becomes a new LOP -- the dual problem. Use variables \(y_1,y_2,y_3,y_4\) to create linear combinations +of the constraints on money, time and the total probabilities \(p_1\) and \(p_2\) respectively, +and create a linear function \(w = Ay_1 + By_2 +Cy_3 +Dy_4\) which guarantees that \(w\) will +be larger than any profit one could make. What constraints must \(y_1\dots y_4\) satisfy? +$PAR +\{ANS($dual_constraints->cmp->withPostFilter( + linebreak_at_commas() +)),ans_box(4,80)\} +$PAR The objective function would be: + \(w = \) \{ANS($dual_objfun->cmp), $dual_objfun->ans_rule(50)\} +$PAR +To get the best possible estimate for the profit in the original problem +you would want to \{ANS($popupmaxmin->cmp),$popupmaxmin->menu\} \(w\) +Why? +$PAR +\{ANS(essay_cmp()), essay_box(5,80)\} +END_TEXT + +BEGIN_SOLUTION +The profit is less than the minimum of \( w = $dual_objfun \) subject to + +\[\begin{aligned} +$dual_constraint1 & \\ +$dual_constraint2 & +\end{aligned} +\] +The \(y_i\) are the coefficients used to add up the constraint inequalities of the primary problem +so as to estimate an upper bound for the profit. If the \(y_i\) are positive values satisfying +the constraints then \(w\) will be greater than or equal to the profit. We get the most precise +estimate by finding values of \(y_i\), satisfying the constraints, which give the smallest +possible value for \( w\). + +END_SOLUTION +Context()->normalStrings; + + # ######################################################################################################### # # Section 5 set up dual constraints # ######################################################################################################### -# SECTION5: { -# -# Section::Begin("Part 5: Set up dual constraints"); -# Context($context); -# $dualtableau1 = [[-$bill_money_commitment,-$bill_time_commitment, -1, 0, 1, 0, 0,-$bill_profit], -# [-$steve_money_commitment, -$steve_time_commitment,0,-1, 0, 1, 0,-$steve_profit], -# [${money_total},${time_total},1, 1, 0,0, 1,0]]; -# $dualmatrix1 = Matrix($dualtableau1); -# $dualtoplabels = [qw(y1 y2 y3 y4 y5 y6 v b)]; -# $dualmatrix1->{top_labels}=$dualtoplabels; -# $dualtableau1_string = lp_display_mm($dualtableau1,top_labels=>$dualtoplabels); -# -# $popup = PopUp(["?","Yes", "No"], "No"); -# -# $dual_row_size =2; # figure these out automatically -# $dual_col_size =$col_size; -# BEGIN_TEXT -# dualmatrix1 size \{join(",", $dualmatrix1->dimensions)\} $BR -# $dual_row_size, $dual_col_size -# END_TEXT -# Context()->texStrings; -# -# BEGIN_TEXT -# $PAR -# For this first effort let's rewrite the equations so that they are in "standard" form, meaning -# that the inequalities are all less than or equal to and that the goal is to maximize \(v = -w\). -# This will probably mean that you will have to rewrite the natural way in which you set up -# the problem above. Some coefficients will change sign. -# We will still be using the convention that -# the coefficient of \(v\) will be \(1\). -# -# $PAR -# \{MATRIX_ENTRY_BUTTON($dualmatrix1)\} -# \{ANS($dualmatrix1->cmp()), $dualmatrix1->ans_array \} -# $PAR -# (This tableau is for maximizing \(v\), aka \(-w\) -- there are two minus sign switches. ) -# $PAR -# Is there a natural feasible solution to this problem? In other words does setting the -# problem parameters equal to 0 provide a feasible solution? -# \{ANS($popup->cmp),$popup->menu \} -# $PAR -# Explain why or why not and what are your options for getting started. -# \{ ANS(essay_cmp()), essay_box(10,80) \} -# $PAR -# END_TEXT -# -# BEGIN_SOLUTION -# \[ $dualtableau1_string \] -# $PAR -# Setting the parameters \(y_1, y_2\) to zero is not a feasible solution. -# Options for finding a first feasible solution include guessing (not a bad choice -# for a problem this small), using prior knowledge (e.g. a known optimal solution to -# a similar problem), creating an auxiliary problem to find a feasible point, -# and the "shortcut method" which is an accelerated version of the "auxiliary method". -# END_SOLUTION -# Context()->normalStrings; -# Section::End(); -# -# } #END SECTION 5 + +Context($context); +$dualtableau1 = [[-$bill_money_commitment,-$bill_time_commitment, -1, 0, 1, 0, 0,-$bill_profit], + [-$steve_money_commitment, -$steve_time_commitment,0,-1, 0, 1, 0,-$steve_profit], + [${money_total},${time_total},1, 1, 0,0, 1,0]]; +$dualmatrix1 = Matrix($dualtableau1); +$dualtoplabels = [qw(y1 y2 y3 y4 y5 y6 v b)]; +$dualmatrix1->{top_labels}=$dualtoplabels; +$dualtableau1_string = lp_display_mm($dualtableau1,top_labels=>$dualtoplabels); + +$popup = PopUp(["?","Yes", "No"], "No"); + +$dual_row_size =2; # figure these out automatically +$dual_col_size =$col_size; +BEGIN_TEXT +dualmatrix1 size \{join(",", $dualmatrix1->dimensions)\} $BR +$dual_row_size, $dual_col_size +END_TEXT +Context()->texStrings; + +BEGIN_TEXT +$PAR +For this first effort let's rewrite the equations so that they are in "standard" form, meaning +that the inequalities are all less than or equal to and that the goal is to maximize \(v = -w\). +This will probably mean that you will have to rewrite the natural way in which you set up +the problem above. Some coefficients will change sign. +We will still be using the convention that +the coefficient of \(v\) will be \(1\). + +$PAR +\{MATRIX_ENTRY_BUTTON($dualmatrix1)\} +\{ANS($dualmatrix1->cmp()), $dualmatrix1->ans_array \} +$PAR +(This tableau is for maximizing \(v\), aka \(-w\) -- there are two minus sign switches. ) +$PAR +Is there a natural feasible solution to this problem? In other words does setting the +problem parameters equal to 0 provide a feasible solution? +\{ANS($popup->cmp),$popup->menu \} +$PAR +Explain why or why not and what are your options for getting started. +\{ ANS(essay_cmp()), essay_box(10,80) \} +$PAR +END_TEXT + +BEGIN_SOLUTION +\[ $dualtableau1_string \] +$PAR +Setting the parameters \(y_1, y_2\) to zero is not a feasible solution. +Options for finding a first feasible solution include guessing (not a bad choice +for a problem this small), using prior knowledge (e.g. a known optimal solution to +a similar problem), creating an auxiliary problem to find a feasible point, +and the "shortcut method" which is an accelerated version of the "auxiliary method". +END_SOLUTION + + # ######################################################################################################### # # Begin section 6 -- solve the tableau using simplex method # # section 6 (13matrix, 14essay) # # First we have to find a feasible solution -- phase 1 # # We'll use the auxiliary method -- adding an extra slack variable. # ######################################################################################################### -# -# SECTION6: { -# Section::Begin("Part 6: Solve the dual tableau using the simplex method"); -# Context($context); -# $dualtableau2 = [[-1,-$bill_money_commitment,-$bill_time_commitment, -1, 0, 1,0,0,0,-$bill_profit], -# [-1,-$steve_money_commitment,-$steve_time_commitment, 0, -1, 0,1,0,0,-$steve_profit], -# [1,0,0,0,0,0,0,0,1,0], -# [0,$money_total,$time_total,1,1,0,0,1,0,0]]; -# $dualmatrix2 = Matrix($dualtableau2); -# $orginal_dual_matrix=$dualmatrix2; -# $dualtoplabelsphase1 = [qw(y0 y1 y2 y3 y4 y5 y6 v z b)]; -# $dualmatrix2->{top_labels}= $dualtoplabelsphase1; -# $dual_constraint1phase1 = Compute("-${bill_money_commitment}y1 -${bill_time_commitment}y2 -y3 <=-$bill_profit+y0"); -# $dual_constraint2phase1 = Compute("-${steve_money_commitment}y1 -${steve_time_commitment}y2 -y4 <=-$steve_profit+y0"); -# $dualconstraintsphase1 = List($dual_constraint1phase1,$dual_constraint2phase1); -# $dualpivot2=Point(1,1); -# $dualbasis2=Set(6,7); -# ######################################################################################################### -# # section 6 answers (15essay 16matrix) -# ######################################################################################################### -# Context()->texStrings; -# -# BEGIN_TEXT -# Using the auxiliary method write the LOP for finding -# the first feasible point. Write -# the new constraints to be used by auxiliary method. $PAR -# \{ ANS($dualconstraintsphase1->cmp),ans_box(3,80) \} -# $PAR -# We will continue to use the convention that we are maximizing -# the auxiliary function so there will be some minus signs that need to be taken into account. -# We'll let \(z = -y_0\) and try to maximize \(z\). -# $PAR -# Now construct the tableau you'll use for the auxiliary method. -# Add a first column for the extra slack variable and a next to the last row -# for the new objective function value \(z\) and write the new tableau. -# The next to the last row will hold the objective function for \(z\). -# The last row will hold the original -# dual objective function which we'll just carry along. -# $PAR -# \{MATRIX_ENTRY_BUTTON($dualmatrix2)\} -# \{ANS($dualmatrix2->cmp()), $dualmatrix2->ans_array \} -# $PAR -# END_TEXT -# ######################################################################################################### -# # debugging display code -# ######################################################################################################### -# BEGIN_TEXT -# $PAR -# \{display_tableau_state($orginal_dual_matrix,$dualtableau2, $dualmatrix2, $dualbasis2, $dualpivot2)\} -# $PAR -# END_TEXT -# -# Context()->normalStrings; -# -# Context()->texStrings; -# BEGIN_SOLUTION -# New objective: Maximize \(z = -y_0 \) subject to -# \[\begin{align} -# $dual_constraint1phase1&\\ -# $dual_constraint2phase1 &\\ -# \end{align} -# \] with all variables non-negative. -# $PAR -# If \(y_0\) is large enough this always has a feasible solution with -# \(y_1=y_2=0\). -# -# \[ \{lp_display_mm($dualtableau2) \} \] -# $PAR -# END_SOLUTION -# Context()->normalStrings; -# Section::End(); -# } #END_SECTION 6 + + +Context($context); +$dualtableau2aux = [[-1,-$bill_money_commitment,-$bill_time_commitment, -1, 0, 1,0,0,0,-$bill_profit], + [-1,-$steve_money_commitment,-$steve_time_commitment, 0, -1, 0,1,0,0,-$steve_profit], + [1,0,0,0,0,0,0,0,1,0]]; + +# [0,$money_total,$time_total,1,1,0,0,1,0,0]]; +$dualmatrix2aux = Matrix($dualtableau2aux); +$original_dual_aux_matrix=$dualmatrix2aux; +$dualtoplabelsphase1 = [qw(y0 y1 y2 y3 y4 y5 y6 v z b)]; +$dualmatrix2aux->{top_labels}= $dualtoplabelsphase1; +$dual_constraint1phase1 = Compute("-${bill_money_commitment}y1 -${bill_time_commitment}y2 -y3 <=-$bill_profit+y0"); +$dual_constraint2phase1 = Compute("-${steve_money_commitment}y1 -${steve_time_commitment}y2 -y4 <=-$steve_profit+y0"); +$dualconstraintsphase1 = List($dual_constraint1phase1,$dual_constraint2phase1); +$dualpivot2aux=Point(1,1); +$dualbasis2aux=Set(6,7); +######################################################################################################### +# section 6 answers (15essay 16matrix) +######################################################################################################### +Context()->texStrings; + +BEGIN_TEXT +Using the auxiliary method write the LOP for finding +the first feasible point. Write +the new constraints to be used by auxiliary method. $PAR +\{ ANS($dualconstraintsphase1->cmp),ans_box(3,80) \} +$PAR +We will continue to use the convention that we are maximizing +the auxiliary function so there will be some minus signs that need to be taken into account. +We'll let \(z = -y_0\) and try to maximize \(z\). +$PAR +Now construct the tableau you'll use for the auxiliary method. +Add a first column for the extra slack variable and a next to the last row +for the new objective function value \(z\) and write the new tableau. +The next to the last row will hold the objective function for \(z\). +The last row will hold the original +dual objective function which we'll just carry along. +$PAR +\{MATRIX_ENTRY_BUTTON($dualmatrix2)\} +\{ANS($dualmatrix2aux->cmp()), $dualmatrix2aux->ans_array \} +$PAR +END_TEXT +######################################################################################################### +# debugging display code +######################################################################################################### +BEGIN_TEXT + +Display dualtableau2aux +\($dualtableau2aux\), matrix \($dualmatrix2aux\) +dualbasis \($dualbasis2aux\) and \($dualpivot2aux\) +$PAR +\{display_tableau_state($original_dual_aux_matrix,$dualtableau2aux, $dualmatrix2aux, $dualbasis2aux, $dualpivot2aux)\} +$PAR +END_TEXT + +Context()->normalStrings; + +Context()->texStrings; +BEGIN_SOLUTION +New objective: Maximize \(z = -y_0 \) subject to +\[\begin{align} +$dual_constraint1phase1&\\ +$dual_constraint2phase1 &\\ +\end{align} +\] with all variables non-negative. +$PAR +If \(y_0\) is large enough this always has a feasible solution with +\(y_1=y_2=0\). + +\[ \{lp_display_mm($dualtableau2aux) \} \] +$PAR +END_SOLUTION +Context()->normalStrings; + + + # ######################################################################################################### # # Section 7 final comparisons of primary and dual results. # ######################################################################################################### -# SECTION7: { -# Section::Begin("Part 7: do phase 1 simplex method"); -# -# -# #($tableau3,$basis3, $statevars3) = lp_basis_pivot($dualtableau2,$dualbasis2,$dualpivot2); -# #$B3 = matrix_from_submatrix($original_matrix,rows=>[1..$row_size],columns=>$basis3->data); -# #$matrix3 = ($B3->det)*Matrix($tableau3); -# # $matrix3->{top_labels}=$toplabels; -# #$state3=Matrix([[@$statevars3]]); -# ######################################################################################################### -# # debugging display code -# ######################################################################################################### -# BEGIN_TEXT -# $PAR -# \{display_tableau_state($orginal_dual_matrix,$dualtableau3, $dualmatrix3, $dualbasis3, $dualpivot3)\} -# $PAR -# END_TEXT + ($dualtableau3aux,$dualbasis3aux, $dualstatevars3aux) = lp_basis_pivot($dualtableau2aux,$dualbasis2aux,$dualpivot2aux); + $B3 = matrix_from_submatrix($original_dual_aux_matrix,rows=>[1..$dual_row_size],columns=>($dualbasis3aux->data)); +$dualmatrix3aux = ($B3->det)*Matrix($dualtableau3aux); + $dualmatrix3aux->{top_labels}=$dualtoplabelsphase1; + $dualstate3aux=Matrix([[@$dualstatevars3aux]]); +######################################################################################################### +# debugging display code +######################################################################################################### +BEGIN_TEXT +$PAR +Debugging display code dualtableau3aux. This is the matrix +after pivoting on (1,1). $PAR + +\{display_tableau_state($original_dual_aux_matrix,$dualtableau3aux, $dualmatrix3aux, $dualbasis3aux, $dualpivot3aux)\} +$PAR +END_TEXT # # $pivot4 = Point("(1,1)"); # $dualtableau3 = lp_pivot($dualtableau2,0,0); diff --git a/t/matrix_tableau_tests/tableau_test4.pg b/t/matrix_tableau_tests/tableau_test4.pg new file mode 100644 index 0000000000..02448d246f --- /dev/null +++ b/t/matrix_tableau_tests/tableau_test4.pg @@ -0,0 +1,512 @@ +DOCUMENT(); +loadMacros( +"PGstandard.pl", +"MathObjects.pl", +"parserPopUp.pl", +"unionLists.pl", +"MatrixReduce.pl", +#"AppletObjects.pl", +"PGessaymacros.pl", +"PGmatrixmacros.pl", +"LinearProgramming.pl", +"parserLinearInequality.pl", +"quickMatrixEntry.pl", +#"scaffold.pl", +"tableau.pl", +#"gage_matrix_ops.pl", +"PGinfo.pl", +"source.pl", +"PGcourse.pl", +); + +TEXT(beginproblem()); +TEXT($BEGIN_ONE_COLUMN); +$showPartialCorrectAnswers = 1; + +INITIALIZE_QUICK_MATRIX_ENTRY(); + + +############################################################## +# problem data +############################################################## + +# this can be changed (slightly at least) without affecting the behavior of the problem +# The choice of pivots is not yet automatic However + +# Your resources: +$money_total = 6000; +$time_total = 600; + +# Bill +$bill_money_commitment = 5000; #dollars +$bill_time_commitment = 400; # hours +$bill_profit = 4700; +# Steve +$steve_money_commitment = 3000; +$steve_time_commitment = 500; +$steve_profit = 4500; + +#Hack to prevent domain conflict in answer. +Context()->variables->add(p1=>'Real',p2=>'Real'); +$objfun1 = Formula("${bill_profit}p1 + ${steve_profit}p2"); +# why can't the formula be defined within context "linearInequality"? + +Context("LinearInequality"); +Context()->variables->add(p1=>'Real',p2=>'Real'); +Context()->strings->add("Essay Answer" =>{}); +Context()->strings->add('Minimize'=>{},'Maximize'=>{}, "?"=>{}); +Context()->strings->add('Yes'=>{},'No'=>{}); +Context()->flags->set({tolType=>"absolute",tolerance=>.001}); +Context()->flags->set( + zeroLevel=>0.001, + zeroLevelTol=>.001 +); +# $objfun1 = Formula("${bill_profit}p1 + ${steve_profit}p2"); + +our $context=Context(); + +############################################################## +# +# Setup +# +# + + + +############################################################################## +# utility subroutine for checking your answers +############################################################################## + +sub update_tableau { + my $original_matrix = shift; + my $basis = shift; + # $basis is a Set object + # last row is numbered $basis_size+1; + my $basis_size=@{$basis->data}; + my $basis_matrix = $original_matrix->submatrix(rows=>[1..$basis_size],columns=>$basis->data); + + my $obj_coeff = - ($original_matrix->row($basis_size+1)); + #obj function coeff corresponding to basis variables/columns + $cB = Matrix($obj_coeff->column_slice($basis->data)); + # get A | S |b portion of tableau and multiply by det(B)B^-1 + my $current_matrix= ($basis_matrix->det)* + ($basis_matrix->inverse)* + $original_matrix->row_slice(1..$basis_size); + my $new_obj_row = ($cB*$current_matrix)->row(1) - ($basis_matrix->det)*($obj_coeff); + my $current_tableau = Matrix(@{$current_matrix->extract_rows},Matrix($new_obj_row)); + return $current_tableau; +} +# the full tableau has one more row -- the objective function +sub display_tableau_state { + my ($original_matrix,$tableau, $matrix, $basis,$pivot)= @_; + $basis = $basis->sort; + my $basis_size = @{$basis->data} ; + my $basis_matrix = matrix_from_submatrix($original_matrix, rows=>[1..$basis_size],columns=>$basis->data); + my $normalized_tableau = $matrix; + my $reduced_matrix = matrix_from_matrix_rows($matrix,1..$basis_size); + warn "basis matrix is $basis_matrix, $reduced_matrix"; + my $normalized_reduced_matrix = ($basis_matrix->det)*$reduced_matrix; + # calculate current tableau by multiplying by the inverse of the basis + my $obj_coeff = - $original_matrix->row($basis_size+1); + my $cB = Matrix($obj_coeff->column_slice($basis->data)); + my $current_matrix = ($basis_matrix->det)*($basis_matrix->inverse)*matrix_from_matrix_rows($original_matrix,1..$basis_size); + my $new_obj_row = ($cB*$current_matrix)->row(1) - ($basis_matrix->det)*($obj_coeff); + my $current_tableau = Matrix(@{$current_matrix->extract_rows},Matrix($new_obj_row)); + $current_tableau = update_tableau($original_matrix, $basis); + + #DEBUG_MESSAGE("get state variables for $matrix using basis $basis"); + @statevars1 = get_tableau_variable_values($matrix, $basis); + # get z value + $statevars1 = ~~@statevars1; + $state = Matrix([[@statevars1]]); + + + return " + + pivot: \($pivot\) basis: \( $basis\) $PAR basis matrix \(". + display_matrix_mm($basis_matrix)."\) $PAR + tableau and normalized current tableau $PAR + \(" . lp_display_mm($tableau) . "\)\(".lp_display_mm($normalized_tableau). " \) $PAR + original matrix and current matrix calculated by Binverse*original $PAR + \(". + display_matrix_mm(matrix_from_matrix_rows($original_matrix,1..$basis_size)). + "\) \(". + display_matrix_mm($current_matrix). + "\) $PAR + objective coefficients = \( $obj_coeff \) $PAR + basis objective_coefficients = \($cB\) $PAR + new tableau \[". + lp_display_mm($current_tableau). + "\] $PAR + state: \($state)\) $PAR + " +} + + +# ######################################################################################################### +# # Section 4 dual problem +# ######################################################################################################### + + +Context()->variables->add(y1=>'Real',y2=>'Real', y3=>'Real', y4=>'Real', w=>'Real',y0=>'Real'); +$dual_constraint1 = Compute("${bill_money_commitment}y1 +${bill_time_commitment}y2 +y3 >=$bill_profit"); +$dual_constraint2 = Compute("${steve_money_commitment}y1 +${steve_time_commitment}y2 +y4 >=$steve_profit"); +$dual_constraints= List($dual_constraint1, $dual_constraint2); + +$dual_objfun = Formula("${money_total}y1 +${time_total}y2 + y3 +y4"); +$popupmaxmin = PopUp(["?","Maximize", "Minimize"], "Minimize"); + +Context()->texStrings; + +BEGIN_TEXT +Construct the dual problem for the linear optimization problem above. The first goal is to calculate +an upper bound for the possible profit in the LOP using linear combinations of the inequality constraints. +Then formulate the search for the best of these upper bounds in such a way that it +becomes a new LOP -- the dual problem. Use variables \(y_1,y_2,y_3,y_4\) to create linear combinations +of the constraints on money, time and the total probabilities \(p_1\) and \(p_2\) respectively, +and create a linear function \(w = Ay_1 + By_2 +Cy_3 +Dy_4\) which guarantees that \(w\) will +be larger than any profit one could make. What constraints must \(y_1\dots y_4\) satisfy? +$PAR +\{ANS($dual_constraints->cmp->withPostFilter( + linebreak_at_commas() +)),ans_box(4,80)\} +$PAR The objective function would be: + \(w = \) \{ANS($dual_objfun->cmp), $dual_objfun->ans_rule(50)\} +$PAR +To get the best possible estimate for the profit in the original problem +you would want to \{ANS($popupmaxmin->cmp),$popupmaxmin->menu\} \(w\) +Why? +$PAR +\{ANS(essay_cmp()), essay_box(5,80)\} +END_TEXT + +BEGIN_SOLUTION +The profit is less than the minimum of \( w = $dual_objfun \) subject to + +\[\begin{aligned} +$dual_constraint1 & \\ +$dual_constraint2 & +\end{aligned} +\] +The \(y_i\) are the coefficients used to add up the constraint inequalities of the primary problem +so as to estimate an upper bound for the profit. If the \(y_i\) are positive values satisfying +the constraints then \(w\) will be greater than or equal to the profit. We get the most precise +estimate by finding values of \(y_i\), satisfying the constraints, which give the smallest +possible value for \( w\). + +END_SOLUTION +Context()->normalStrings; + + +# ######################################################################################################### +# # Section 5 set up dual constraints +# ######################################################################################################### + +Context($context); +$dualtableau1 = [[-$bill_money_commitment,-$bill_time_commitment, -1, 0, 1, 0, 0,-$bill_profit], + [-$steve_money_commitment, -$steve_time_commitment,0,-1, 0, 1, 0,-$steve_profit], + [${money_total},${time_total},1, 1, 0,0, 1,0]]; +$dualmatrix1 = Matrix($dualtableau1); +$dualtoplabels = [qw(y1 y2 y3 y4 y5 y6 v b)]; +$dualmatrix1->{top_labels}=$dualtoplabels; +$dualtableau1_string = lp_display_mm($dualtableau1,top_labels=>$dualtoplabels); + +$popup = PopUp(["?","Yes", "No"], "No"); + +$dual_row_size =2; # figure these out automatically +$dual_col_size =$col_size; +BEGIN_TEXT +dualmatrix1 size \{join(",", $dualmatrix1->dimensions)\} $BR +$dual_row_size, $dual_col_size +END_TEXT +Context()->texStrings; + +BEGIN_TEXT +$PAR +For this first effort let's rewrite the equations so that they are in "standard" form, meaning +that the inequalities are all less than or equal to and that the goal is to maximize \(v = -w\). +This will probably mean that you will have to rewrite the natural way in which you set up +the problem above. Some coefficients will change sign. +We will still be using the convention that +the coefficient of \(v\) will be \(1\). + +$PAR +\{MATRIX_ENTRY_BUTTON($dualmatrix1)\} +\{ANS($dualmatrix1->cmp()), $dualmatrix1->ans_array \} +$PAR +(This tableau is for maximizing \(v\), aka \(-w\) -- there are two minus sign switches. ) +$PAR +Is there a natural feasible solution to this problem? In other words does setting the +problem parameters equal to 0 provide a feasible solution? +\{ANS($popup->cmp),$popup->menu \} +$PAR +Explain why or why not and what are your options for getting started. +\{ ANS(essay_cmp()), essay_box(10,80) \} +$PAR +END_TEXT + +BEGIN_SOLUTION +\[ $dualtableau1_string \] +$PAR +Setting the parameters \(y_1, y_2\) to zero is not a feasible solution. +Options for finding a first feasible solution include guessing (not a bad choice +for a problem this small), using prior knowledge (e.g. a known optimal solution to +a similar problem), creating an auxiliary problem to find a feasible point, +and the "shortcut method" which is an accelerated version of the "auxiliary method". +END_SOLUTION + + +# ######################################################################################################### +# # Begin section 6 -- solve the tableau using simplex method +# # section 6 (13matrix, 14essay) +# # First we have to find a feasible solution -- phase 1 +# # We'll use the auxiliary method -- adding an extra slack variable. +# ######################################################################################################### + + +Context($context); +$dualtableau2aux = [[-1,-$bill_money_commitment,-$bill_time_commitment, -1, 0, 1,0,0,0,-$bill_profit], + [-1,-$steve_money_commitment,-$steve_time_commitment, 0, -1, 0,1,0,0,-$steve_profit], + [1,0,0,0,0,0,0,0,1,0]]; + +# [0,$money_total,$time_total,1,1,0,0,1,0,0]]; +$dualmatrix2aux = Matrix($dualtableau2aux); +$original_dual_aux_matrix=$dualmatrix2aux; +$dualtoplabelsphase1 = [qw(y0 y1 y2 y3 y4 y5 y6 v z b)]; +$dualmatrix2aux->{top_labels}= $dualtoplabelsphase1; +$dual_constraint1phase1 = Compute("-${bill_money_commitment}y1 -${bill_time_commitment}y2 -y3 <=-$bill_profit+y0"); +$dual_constraint2phase1 = Compute("-${steve_money_commitment}y1 -${steve_time_commitment}y2 -y4 <=-$steve_profit+y0"); +$dualconstraintsphase1 = List($dual_constraint1phase1,$dual_constraint2phase1); +$dualpivot2aux=Point(1,1); +$dualbasis2aux=Set(6,7); +######################################################################################################### +# section 6 answers (15essay 16matrix) +######################################################################################################### +Context()->texStrings; + +BEGIN_TEXT +Using the auxiliary method write the LOP for finding +the first feasible point. Write +the new constraints to be used by auxiliary method. $PAR +\{ ANS($dualconstraintsphase1->cmp),ans_box(3,80) \} +$PAR +We will continue to use the convention that we are maximizing +the auxiliary function so there will be some minus signs that need to be taken into account. +We'll let \(z = -y_0\) and try to maximize \(z\). +$PAR +Now construct the tableau you'll use for the auxiliary method. +Add a first column for the extra slack variable and a next to the last row +for the new objective function value \(z\) and write the new tableau. +The next to the last row will hold the objective function for \(z\). +The last row will hold the original +dual objective function which we'll just carry along. +$PAR +\{MATRIX_ENTRY_BUTTON($dualmatrix2)\} +\{ANS($dualmatrix2aux->cmp()), $dualmatrix2aux->ans_array \} +$PAR +END_TEXT +######################################################################################################### +# debugging display code +######################################################################################################### +BEGIN_TEXT + +Display dualtableau2aux +\($dualtableau2aux\), matrix \($dualmatrix2aux\) +dualbasis \($dualbasis2aux\) and \($dualpivot2aux\) +$PAR +\{display_tableau_state($original_dual_aux_matrix,$dualtableau2aux, $dualmatrix2aux, $dualbasis2aux, $dualpivot2aux)\} +$PAR +END_TEXT + +Context()->normalStrings; + +Context()->texStrings; +BEGIN_SOLUTION +New objective: Maximize \(z = -y_0 \) subject to +\[\begin{align} +$dual_constraint1phase1&\\ +$dual_constraint2phase1 &\\ +\end{align} +\] with all variables non-negative. +$PAR +If \(y_0\) is large enough this always has a feasible solution with +\(y_1=y_2=0\). + +\[ \{lp_display_mm($dualtableau2aux) \} \] +$PAR +END_SOLUTION +Context()->normalStrings; + + + +# ######################################################################################################### +# # Section 7 final comparisons of primary and dual results. +# ######################################################################################################### + $dualpivot3aux=$dualpivot2aux=Point(1,1); #already set + ($dualtableau3aux,$dualbasis3aux, $dualstatevars3aux) = lp_basis_pivot($dualtableau2aux,$dualbasis2aux,$dualpivot2aux); + $B3 = matrix_from_submatrix($original_dual_aux_matrix,rows=>[1..$dual_row_size],columns=>($dualbasis3aux->data)); + $dualmatrix3aux = ($B3->det)*Matrix($dualtableau3aux); + $dualmatrix3aux->{top_labels}=$dualtoplabelsphase1; + $dualstate3aux=Matrix([[@$dualstatevars3aux]]); + + +# $dualpivot4aux = Point("(1,1)"); +# $dualtableau3aux = lp_pivot($dualtableau2,0,0); +# $dualmatrix3 = Matrix($dualtableau3aux); +# $dualmatrix3->{top_labels}=$dualtoplabelsphase1; +# $dualtableau3_string = lp_display_mm($dualtableau3aux); +# $z_initial=-$bill_profit; # a hint +# # this is the initial value of the z=-y0 variable we are maximizing? + +# now start to maximize z +$dualpivot4aux = Point("(2,2)"); +$dualtableau4aux = lp_pivot($dualtableau3aux,1,1); +$denom1 = 2000; +$dualmatrix4aux = $denom1*Matrix($dualtableau4aux); +$dualmatrix4aux->{top_labels}=$dualtoplabelsphase1; +$dualtableau4aux_string = lp_display_mm($dualmatrix4aux); + +$dualpivot5aux= Point("(1,3)"); +$dualtableau5aux = lp_pivot($dualtableau4aux,0,2); + +## FIXME entries close to zero in a matrix are not being compared properly. +## FIXME tolerance and tolType are being ignored + +#$dualtableau5->[2][3]=0; +#$dualtableau5->[2][5]=0; + +$denom2 = 1.3E6; +$dualmatrix5aux = $denom2*Matrix($dualtableau5aux); +$dualmatrix5aux->{top_labels}=$dualtoplabelsphase1; +$dualtableau5_string = lp_display_mm($dualmatrix5aux); +$state5 = Matrix([[ 0.423, 6.4615, 0, 0, -6415.38]]); +$popup5 = PopUp(["?","Yes", "No"], "No"); + +$dualpivot5aux= Point("(1,3)"); +$dualtableau5aux = lp_pivot($dualtableau4aux,0,2); + +## FIXME entries close to zero in a matrix are not being compared properly. +## FIXME tolerance and tolType are being ignored + +#$dualtableau5->[2][3]=0; +#$dualtableau5->[2][5]=0; + +$denom2 = 1.3E6; +$dualmatrix5aux = $denom2*Matrix($dualtableau5aux); +$dualmatrix5aux->{top_labels}=$dualtoplabelsphase1; +$dualtableau5_string = lp_display_mm($dualmatrix5aux); + +######################################################################################################### +# debugging display code +######################################################################################################### +BEGIN_TEXT +$PAR +(1,1) Debugging display code dualtableau3aux. This is the matrix +after pivoting on (1,1) \($dualpivot3aux\) to basis $dualbasis3aux . +newbasis: \{$newdualbasis3aux=Set(1,7)\} $newdualbasis3aux +$PAR + +\{display_tableau_state($original_dual_aux_matrix, + $dualtableau3aux, $dualmatrix3aux, + $newdualbasis3aux, $dualpivot3aux)\} +$PAR + +(2,2) Debugging display code dualtableau4aux. +This is the matrix after pivoting on $dualpivot4aux and basis $dualbasis3aux +and basis $dualbasis3aux . +newbasis: \{$newdualbasis4aux=Set(1,2)\} $newdualbasis4aux +$PAR +\{display_tableau_state($original_dual_aux_matrix, + $dualtableau4aux, $dualmatrix4aux, + $newdualbasis4aux, $dualpivot4aux)\} +$PAR + +(1,3) Debugging display code dualtableau5aux. +This is the matrix after pivoting on $dualpivot5aux and basis $dualbasis3aux . +newbasis: \{$newdualbasis5aux=Set(2,3)\} +$PAR +\{display_tableau_state($original_dual_aux_matrix, + $dualtableau5aux, $dualmatrix5aux, + $newdualbasis5aux, $dualpivot5aux)\} +$PAR +END_TEXT +# ######################################################################################################### +# # Section 7 answers 17,18matrix,19,20matrix,21,22matrix,23, 24,25essay +# ######################################################################################################### + + + +Context()->texStrings; + +BEGIN_TEXT +The first pivot, which will make the right hand side entries positive is +\{ANS($dualpivot3aux->cmp),$dualpivot3aux->ans_rule \}. $PAR +Recall that in the case of tie we take entry with the least index +(i.e. left most or upper most. ) +$PAR + + +The resulting tableau is $PAR +\{MATRIX_ENTRY_BUTTON($dualmatrix3aux)\} +\{ANS($dualmatrix3aux->cmp), $dualmatrix3aux->ans_array() \} + +The value of \(z=-y_0\) is \($z_initial\). +$PAR +The next pivot, following the simplex method to maximize \(z\), is +\{ANS($dualpivot4aux->cmp), $dualpivot4aux->ans_rule\}. +$PAR +(Many of the following answers have lots of zeros. You can use the shortcut +1E3 to stand for \(1\times 10^3\). ) +$PAR +Notice that because of the zero on the right hand side none of the state variables change. We had three +hyperplanes intersecting at a point and we have changed our mind about which +of those three we consider basic. The new tableau is +$PAR +\{MATRIX_ENTRY_BUTTON($dualmatrix4aux)\} +\{ANS($dualmatrix4aux->cmp), $dualmatrix4aux->ans_array() \} +$PAR +The next pivot \{ ANS($dualpivot5aux->cmp), $dualpivot5aux->ans_rule \} leads to $PAR +\{MATRIX_ENTRY_BUTTON($dualmatrix5aux)\} +\{#FIXME -- these tolerance leveals are being ignored \} +\{ANS($dualmatrix5aux->cmp()), $dualmatrix5aux->ans_array() \} + +$PAR and we notice that now \(z=-y_0=0\) so we have found a basic feasible solution to our +original dual problem. The variables \(y_1,y_2,y_3,y_4,v\) for this +solution are +$PAR + +\{ANS($state5->cmp),$state5->ans_array\} +$PAR +Do we need to continue to optimize the value for \(v\)? +\{ANS($popup5->cmp), $popup5->menu()\} Why? $PAR +\{ANS(essay_cmp), essay_box(3,80)\} +$PAR +Compare this answer \(v^*= -w^*\) to the dual problem to the optimal value \(P^*\)for the primary problem. +The problems' goals were to maximize \(P\) and to minimize \(w\). +$PAR + +END_TEXT + +BEGIN_SOLUTION +The first pivot is \($dualpivot3aux\) which makes the right hand side entries positive. +$PAR +\[$dualtableau3_string\] +$PAR +The next pivot is \($dualpivot4aux\). It follows the simplex rule of choosing the row +with the most restrictive ratio -- in this case zero. The result is simply to +choose a new representation of the same point -- no change in state takes place. +$PAR\[$dualtableau4_string\] +$PAR +The final pivot (as it turns out) is \($dualpivot5aux\). At this point we are done with +phase 1 because the value of \(z=-y_0=0\) so we have maximized \(z\) and +minimized \(y_0\) to \(0\) +$PAR\[$dualtableau5_string\] +$PAR +At this point we notice that the coefficients in the last row are such that +we cannot increase the value of \(v\) any further. \(v\) is at its maximum +and \(w=-v\) is at its minimum. The minimum value of \(w=64?\) which is the +same as the maximum profit that we found in the first example. +END_SOLUTION +TEXT($END_ONE_COLUMN); +ENDDOCUMENT(); + diff --git a/t/matrix_tableau_tests/tableau_test5.pg b/t/matrix_tableau_tests/tableau_test5.pg new file mode 100644 index 0000000000..51648e8c7a --- /dev/null +++ b/t/matrix_tableau_tests/tableau_test5.pg @@ -0,0 +1,505 @@ +DOCUMENT(); +loadMacros( +"PGstandard.pl", +"MathObjects.pl", +"parserPopUp.pl", +"unionLists.pl", +"MatrixReduce.pl", +#"AppletObjects.pl", +"PGessaymacros.pl", +"PGmatrixmacros.pl", +"LinearProgramming.pl", +"parserLinearInequality.pl", +"quickMatrixEntry.pl", +#"scaffold.pl", +"tableau.pl", +#"gage_matrix_ops.pl", +"PGinfo.pl", +"source.pl", +"PGcourse.pl", +); + +TEXT(beginproblem()); +TEXT($BEGIN_ONE_COLUMN); +$showPartialCorrectAnswers = 1; + +INITIALIZE_QUICK_MATRIX_ENTRY(); + + +############################################################## +# problem data +############################################################## + +# this can be changed (slightly at least) without affecting the behavior of the problem +# The choice of pivots is not yet automatic However + +# Your resources: +$money_total = 6000; +$time_total = 600; + +# Bill +$bill_money_commitment = 5000; #dollars +$bill_time_commitment = 400; # hours +$bill_profit = 4700; +# Steve +$steve_money_commitment = 3000; +$steve_time_commitment = 500; +$steve_profit = 4500; + +#Hack to prevent domain conflict in answer. +Context()->variables->add(p1=>'Real',p2=>'Real'); +$objfun1 = Formula("${bill_profit}p1 + ${steve_profit}p2"); +# why can't the formula be defined within context "linearInequality"? + +Context("LinearInequality"); +Context()->variables->add(p1=>'Real',p2=>'Real'); +Context()->strings->add("Essay Answer" =>{}); +Context()->strings->add('Minimize'=>{},'Maximize'=>{}, "?"=>{}); +Context()->strings->add('Yes'=>{},'No'=>{}); +Context()->flags->set({tolType=>"absolute",tolerance=>.001}); +Context()->flags->set( + zeroLevel=>0.001, + zeroLevelTol=>.001 +); +# $objfun1 = Formula("${bill_profit}p1 + ${steve_profit}p2"); + +our $context=Context(); + +############################################################## +# +# Setup +# +# + + + +############################################################################## +# utility subroutine for checking your answers +############################################################################## + +sub update_tableau { + my $original_matrix = shift; + my $basis = shift; + # $basis is a Set object + # last row is numbered $basis_size+1; + my $basis_size=@{$basis->data}; + my $basis_matrix = $original_matrix->submatrix(rows=>[1..$basis_size],columns=>$basis->data); + + my $obj_coeff = - ($original_matrix->row($basis_size+1)); + #obj function coeff corresponding to basis variables/columns + $cB = Matrix($obj_coeff->column_slice($basis->data)); + # get A | S |b portion of tableau and multiply by det(B)B^-1 + my $current_matrix= ($basis_matrix->det)* + ($basis_matrix->inverse)* + $original_matrix->row_slice(1..$basis_size); + my $new_obj_row = ($cB*$current_matrix)->row(1) - ($basis_matrix->det)*($obj_coeff); + my $current_tableau = Matrix(@{$current_matrix->extract_rows},Matrix($new_obj_row)); + return $current_tableau; +} +# the full tableau has one more row -- the objective function +sub display_tableau_state { + my ($original_matrix,$tableau, $matrix, $basis,$pivot)= @_; + $basis = $basis->sort; + my $basis_size = @{$basis->data} ; + my $basis_matrix = matrix_from_submatrix($original_matrix, rows=>[1..$basis_size],columns=>$basis->data); + my $normalized_tableau = $matrix; + my $reduced_matrix = matrix_from_matrix_rows($matrix,1..$basis_size); + warn "basis matrix is $basis_matrix, $reduced_matrix"; + my $normalized_reduced_matrix = ($basis_matrix->det)*$reduced_matrix; + # calculate current tableau by multiplying by the inverse of the basis + my $obj_coeff = - $original_matrix->row($basis_size+1); + my $cB = Matrix($obj_coeff->column_slice($basis->data)); + my $current_matrix = ($basis_matrix->det)*($basis_matrix->inverse)*matrix_from_matrix_rows($original_matrix,1..$basis_size); + my $new_obj_row = ($cB*$current_matrix)->row(1) - ($basis_matrix->det)*($obj_coeff); + my $current_tableau = Matrix(@{$current_matrix->extract_rows},Matrix($new_obj_row)); + $current_tableau = update_tableau($original_matrix, $basis); + + #DEBUG_MESSAGE("get state variables for $matrix using basis $basis"); + @statevars1 = get_tableau_variable_values($matrix, $basis); + # get z value + $statevars1 = ~~@statevars1; + $state = Matrix([[@statevars1]]); + + + return " + + pivot: \($pivot\) basis: \( $basis\) $PAR basis matrix \(". + display_matrix_mm($basis_matrix)."\) $PAR + tableau and normalized current tableau $PAR + \(" . lp_display_mm($tableau) . "\)\(".lp_display_mm($normalized_tableau). " \) $PAR + original matrix and current matrix calculated by Binverse*original $PAR + \(". + display_matrix_mm(matrix_from_matrix_rows($original_matrix,1..$basis_size)). + "\) \(". + display_matrix_mm($current_matrix). + "\) $PAR + objective coefficients = \( $obj_coeff \) $PAR + basis objective_coefficients = \($cB\) $PAR + new tableau \[". + lp_display_mm($current_tableau). + "\] $PAR + state: \($state)\) $PAR + " +} + + +# ######################################################################################################### +# # Section 4 dual problem +# ######################################################################################################### + + +Context()->variables->add(y1=>'Real',y2=>'Real', y3=>'Real', y4=>'Real', w=>'Real',y0=>'Real'); +$dual_constraint1 = Compute("${bill_money_commitment}y1 +${bill_time_commitment}y2 +y3 >=$bill_profit"); +$dual_constraint2 = Compute("${steve_money_commitment}y1 +${steve_time_commitment}y2 +y4 >=$steve_profit"); +$dual_constraints= List($dual_constraint1, $dual_constraint2); + +$dual_objfun = Formula("${money_total}y1 +${time_total}y2 + y3 +y4"); +$popupmaxmin = PopUp(["?","Maximize", "Minimize"], "Minimize"); + +Context()->texStrings; + +BEGIN_TEXT +Construct the dual problem for the linear optimization problem above. The first goal is to calculate +an upper bound for the possible profit in the LOP using linear combinations of the inequality constraints. +Then formulate the search for the best of these upper bounds in such a way that it +becomes a new LOP -- the dual problem. Use variables \(y_1,y_2,y_3,y_4\) to create linear combinations +of the constraints on money, time and the total probabilities \(p_1\) and \(p_2\) respectively, +and create a linear function \(w = Ay_1 + By_2 +Cy_3 +Dy_4\) which guarantees that \(w\) will +be larger than any profit one could make. What constraints must \(y_1\dots y_4\) satisfy? +$PAR +\{ANS($dual_constraints->cmp->withPostFilter( + linebreak_at_commas() +)),ans_box(4,80)\} +$PAR The objective function would be: + \(w = \) \{ANS($dual_objfun->cmp), $dual_objfun->ans_rule(50)\} +$PAR +To get the best possible estimate for the profit in the original problem +you would want to \{ANS($popupmaxmin->cmp),$popupmaxmin->menu\} \(w\) +Why? +$PAR +\{ANS(essay_cmp()), essay_box(5,80)\} +END_TEXT + +BEGIN_SOLUTION +The profit is less than the minimum of \( w = $dual_objfun \) subject to + +\[\begin{aligned} +$dual_constraint1 & \\ +$dual_constraint2 & +\end{aligned} +\] +The \(y_i\) are the coefficients used to add up the constraint inequalities of the primary problem +so as to estimate an upper bound for the profit. If the \(y_i\) are positive values satisfying +the constraints then \(w\) will be greater than or equal to the profit. We get the most precise +estimate by finding values of \(y_i\), satisfying the constraints, which give the smallest +possible value for \( w\). + +END_SOLUTION +Context()->normalStrings; + + +# ######################################################################################################### +# # Section 5 set up dual constraints +# ######################################################################################################### + +Context($context); +$dualtableau1 = [[-$bill_money_commitment,-$bill_time_commitment, -1, 0, 1, 0, 0,-$bill_profit], + [-$steve_money_commitment, -$steve_time_commitment,0,-1, 0, 1, 0,-$steve_profit], + [${money_total},${time_total},1, 1, 0,0, 1,0]]; +$dualmatrix1 = Matrix($dualtableau1); +$dualtoplabels = [qw(y1 y2 y3 y4 y5 y6 v b)]; +$dualmatrix1->{top_labels}=$dualtoplabels; +$dualtableau1_string = lp_display_mm($dualtableau1,top_labels=>$dualtoplabels); + +$popup = PopUp(["?","Yes", "No"], "No"); + +$dual_row_size =2; # figure these out automatically +$dual_col_size =$col_size; +BEGIN_TEXT +dualmatrix1 size \{join(",", $dualmatrix1->dimensions)\} $BR +$dual_row_size, $dual_col_size +END_TEXT +Context()->texStrings; + +BEGIN_TEXT +$PAR +For this first effort let's rewrite the equations so that they are in "standard" form, meaning +that the inequalities are all less than or equal to and that the goal is to maximize \(v = -w\). +This will probably mean that you will have to rewrite the natural way in which you set up +the problem above. Some coefficients will change sign. +We will still be using the convention that +the coefficient of \(v\) will be \(1\). + +$PAR +\{MATRIX_ENTRY_BUTTON($dualmatrix1)\} +\{ANS($dualmatrix1->cmp()), $dualmatrix1->ans_array \} +$PAR +(This tableau is for maximizing \(v\), aka \(-w\) -- there are two minus sign switches. ) +$PAR +Is there a natural feasible solution to this problem? In other words does setting the +problem parameters equal to 0 provide a feasible solution? +\{ANS($popup->cmp),$popup->menu \} +$PAR +Explain why or why not and what are your options for getting started. +\{ ANS(essay_cmp()), essay_box(10,80) \} +$PAR +END_TEXT + +BEGIN_SOLUTION +\[ $dualtableau1_string \] +$PAR +Setting the parameters \(y_1, y_2\) to zero is not a feasible solution. +Options for finding a first feasible solution include guessing (not a bad choice +for a problem this small), using prior knowledge (e.g. a known optimal solution to +a similar problem), creating an auxiliary problem to find a feasible point, +and the "shortcut method" which is an accelerated version of the "auxiliary method". +END_SOLUTION + + +# ######################################################################################################### +# # Begin section 6 -- solve the tableau using simplex method +# # section 6 (13matrix, 14essay) +# # First we have to find a feasible solution -- phase 1 +# # We'll use the auxiliary method -- adding an extra slack variable. +# ######################################################################################################### + + +Context($context); +$dualtableau2aux = [[-1,-$bill_money_commitment,-$bill_time_commitment, -1, 0, 1,0,0,0,-$bill_profit], + [-1,-$steve_money_commitment,-$steve_time_commitment, 0, -1, 0,1,0,0,-$steve_profit], + [1,0,0,0,0,0,0,0,1,0]]; + +# [0,$money_total,$time_total,1,1,0,0,1,0,0]]; +$dualmatrix2aux = Matrix($dualtableau2aux); +$original_dual_aux_matrix=$dualmatrix2aux; +$dualtoplabelsphase1 = [qw(y0 y1 y2 y3 y4 y5 y6 v z b)]; +$dualmatrix2aux->{top_labels}= $dualtoplabelsphase1; +$dual_constraint1phase1 = Compute("-${bill_money_commitment}y1 -${bill_time_commitment}y2 -y3 <=-$bill_profit+y0"); +$dual_constraint2phase1 = Compute("-${steve_money_commitment}y1 -${steve_time_commitment}y2 -y4 <=-$steve_profit+y0"); +$dualconstraintsphase1 = List($dual_constraint1phase1,$dual_constraint2phase1); +$dualpivot2aux=Point(1,1); +$dualbasis2aux=Set(6,7); +######################################################################################################### +# section 6 answers (15essay 16matrix) +######################################################################################################### +Context()->texStrings; + +BEGIN_TEXT +Using the auxiliary method write the LOP for finding +the first feasible point. Write +the new constraints to be used by auxiliary method. $PAR +\{ ANS($dualconstraintsphase1->cmp),ans_box(3,80) \} +$PAR +We will continue to use the convention that we are maximizing +the auxiliary function so there will be some minus signs that need to be taken into account. +We'll let \(z = -y_0\) and try to maximize \(z\). +$PAR +Now construct the tableau you'll use for the auxiliary method. +Add a first column for the extra slack variable and a next to the last row +for the new objective function value \(z\) and write the new tableau. +The next to the last row will hold the objective function for \(z\). +The last row will hold the original +dual objective function which we'll just carry along. +$PAR +\{MATRIX_ENTRY_BUTTON($dualmatrix2)\} +\{ANS($dualmatrix2aux->cmp()), $dualmatrix2aux->ans_array \} +$PAR +END_TEXT +######################################################################################################### +# debugging display code +######################################################################################################### +BEGIN_TEXT + +Display dualtableau2aux +\($dualtableau2aux\), matrix \($dualmatrix2aux\) +dualbasis \($dualbasis2aux\) and \($dualpivot2aux\) +$PAR +\{display_tableau_state($original_dual_aux_matrix,$dualtableau2aux, $dualmatrix2aux, $dualbasis2aux, $dualpivot2aux)\} +$PAR +END_TEXT + +Context()->normalStrings; + +Context()->texStrings; +BEGIN_SOLUTION +New objective: Maximize \(z = -y_0 \) subject to +\[\begin{align} +$dual_constraint1phase1&\\ +$dual_constraint2phase1 &\\ +\end{align} +\] with all variables non-negative. +$PAR +If \(y_0\) is large enough this always has a feasible solution with +\(y_1=y_2=0\). + +\[ \{lp_display_mm($dualtableau2aux) \} \] +$PAR +END_SOLUTION +Context()->normalStrings; + + + +# ######################################################################################################### +# # Section 7 final comparisons of primary and dual results. +# ######################################################################################################### + $dualpivot3aux=$dualpivot2aux=Point(1,1); #already set + ($dualtableau3aux,$dualbasis3aux, $dualstatevars3aux) = lp_basis_pivot($dualtableau2aux,$dualbasis2aux,$dualpivot2aux); + $B3 = matrix_from_submatrix($original_dual_aux_matrix,rows=>[1..$dual_row_size],columns=>($dualbasis3aux->data)); + $dualmatrix3aux = ($B3->det)*Matrix($dualtableau3aux); + $dualmatrix3aux->{top_labels}=$dualtoplabelsphase1; + $dualstate3aux=Matrix([[@$dualstatevars3aux]]); + + +# $dualpivot4aux = Point("(1,1)"); +# $dualtableau3aux = lp_pivot($dualtableau2,0,0); +# $dualmatrix3 = Matrix($dualtableau3aux); +# $dualmatrix3->{top_labels}=$dualtoplabelsphase1; +# $dualtableau3_string = lp_display_mm($dualtableau3aux); +# $z_initial=-$bill_profit; # a hint +# # this is the initial value of the z=-y0 variable we are maximizing? + +# now start to maximize z +$dualpivot4aux = Point("(2,2)"); +$dualtableau4aux = lp_pivot($dualtableau3aux,1,1); +$denom1 = 2000; +$dualmatrix4aux = $denom1*Matrix($dualtableau4aux); +$dualmatrix4aux->{top_labels}=$dualtoplabelsphase1; +$dualtableau4aux_string = lp_display_mm($dualmatrix4aux); + +$dualpivot5aux= Point("(1,3)"); +$dualtableau5aux = lp_pivot($dualtableau4aux,0,2); +$denom2 = 1.3E6; +$dualmatrix5aux = $denom2*Matrix($dualtableau5aux); +$dualmatrix5aux->{top_labels}=$dualtoplabelsphase1; +$dualtableau5_string = lp_display_mm($dualmatrix5aux); +$state5 = Matrix([[ 0.423, 6.4615, 0, 0, -6415.38]]); +$popup5 = PopUp(["?","Yes", "No"], "No"); + +$dualpivot5aux= Point("(1,3)"); +$dualtableau5aux = lp_pivot($dualtableau4aux,0,2); + +## FIXME entries close to zero in a matrix are not being compared properly. +## FIXME tolerance and tolType are being ignored + +#$dualtableau5->[2][3]=0; +#$dualtableau5->[2][5]=0; + +$denom2 = 1.3E6; +$dualmatrix5aux = $denom2*Matrix($dualtableau5aux); +$dualmatrix5aux->{top_labels}=$dualtoplabelsphase1; +$dualtableau5_string = lp_display_mm($dualmatrix5aux); + +######################################################################################################### +# debugging display code +######################################################################################################### +BEGIN_TEXT +$PAR +(1,1) Debugging display code dualtableau3aux. This is the matrix +after pivoting on (1,1) \($dualpivot3aux\) to basis $dualbasis3aux . +newbasis: \{$newdualbasis3aux=Set(1,7)\} $newdualbasis3aux +$PAR + +\{display_tableau_state($original_dual_aux_matrix, + $dualtableau3aux, $dualmatrix3aux, + $newdualbasis3aux, $dualpivot3aux)\} +$PAR + +(2,2) Debugging display code dualtableau4aux. +This is the matrix after pivoting on $dualpivot4aux and basis $dualbasis3aux +and basis $dualbasis3aux . +newbasis: \{$newdualbasis4aux=Set(1,2)\} $newdualbasis4aux +$PAR +\{display_tableau_state($original_dual_aux_matrix, + $dualtableau4aux, $dualmatrix4aux, + $newdualbasis4aux, $dualpivot4aux)\} +$PAR + +(1,3) Debugging display code dualtableau5aux. +This is the matrix after pivoting on $dualpivot5aux and basis $dualbasis3aux . +newbasis: \{$newdualbasis5aux=Set(2,3)\} +$PAR +\{display_tableau_state($original_dual_aux_matrix, + $dualtableau5aux, $dualmatrix5aux, + $newdualbasis5aux, $dualpivot5aux)\} +$PAR +END_TEXT +# ######################################################################################################### +# # Section 7 answers 17,18matrix,19,20matrix,21,22matrix,23, 24,25essay +# ######################################################################################################### + + + +Context()->texStrings; + +BEGIN_TEXT +The first pivot, which will make the right hand side entries positive is +\{ANS($dualpivot3aux->cmp),$dualpivot3aux->ans_rule \}. $PAR +Recall that in the case of tie we take entry with the least index +(i.e. left most or upper most. ) +$PAR + + +The resulting tableau is $PAR +\{MATRIX_ENTRY_BUTTON($dualmatrix3aux)\} +\{ANS($dualmatrix3aux->cmp), $dualmatrix3aux->ans_array() \} + +The value of \(z=-y_0\) is \($z_initial\). +$PAR +The next pivot, following the simplex method to maximize \(z\), is +\{ANS($dualpivot4aux->cmp), $dualpivot4aux->ans_rule\}. +$PAR +(Many of the following answers have lots of zeros. You can use the shortcut +1E3 to stand for \(1\times 10^3\). ) +$PAR +Notice that because of the zero on the right hand side none of the state variables change. We had three +hyperplanes intersecting at a point and we have changed our mind about which +of those three we consider basic. The new tableau is +$PAR +\{MATRIX_ENTRY_BUTTON($dualmatrix4aux)\} +\{ANS($dualmatrix4aux->cmp), $dualmatrix4aux->ans_array() \} +$PAR +The next pivot \{ ANS($dualpivot5aux->cmp), $dualpivot5aux->ans_rule \} leads to $PAR +\{MATRIX_ENTRY_BUTTON($dualmatrix5aux)\} +\{#FIXME -- these tolerance leveals are being ignored \} +\{ANS($dualmatrix5aux->cmp()), $dualmatrix5aux->ans_array() \} + +$PAR and we notice that now \(z=-y_0=0\) so we have found a basic feasible solution to our +original dual problem. The variables \(y_1,y_2,y_3,y_4,v\) for this +solution are +$PAR + +\{ANS($state5->cmp),$state5->ans_array\} +$PAR +Do we need to continue to optimize the value for \(v\)? +\{ANS($popup5->cmp), $popup5->menu()\} Why? $PAR +\{ANS(essay_cmp), essay_box(3,80)\} +$PAR +Compare this answer \(v^*= -w^*\) to the dual problem to the optimal value \(P^*\)for the primary problem. +The problems' goals were to maximize \(P\) and to minimize \(w\). +$PAR + +END_TEXT + +BEGIN_SOLUTION +The first pivot is \($dualpivot3aux\) which makes the right hand side entries positive. +$PAR +\[$dualtableau3_string\] +$PAR +The next pivot is \($dualpivot4aux\). It follows the simplex rule of choosing the row +with the most restrictive ratio -- in this case zero. The result is simply to +choose a new representation of the same point -- no change in state takes place. +$PAR\[$dualtableau4_string\] +$PAR +The final pivot (as it turns out) is \($dualpivot5aux\). At this point we are done with +phase 1 because the value of \(z=-y_0=0\) so we have maximized \(z\) and +minimized \(y_0\) to \(0\) +$PAR\[$dualtableau5_string\] +$PAR +At this point we notice that the coefficients in the last row are such that +we cannot increase the value of \(v\) any further. \(v\) is at its maximum +and \(w=-v\) is at its minimum. The minimum value of \(w=64?\) which is the +same as the maximum profit that we found in the first example. +END_SOLUTION +TEXT($END_ONE_COLUMN); +ENDDOCUMENT(); + From c4cd4fb341347c523f8a5be4a257561b233f70a2 Mon Sep 17 00:00:00 2001 From: Michael Gage Date: Fri, 13 Oct 2017 13:10:55 -0400 Subject: [PATCH 11/30] Split off subroutine (non-object oriented) versions of tableau methods into file tableau_main_subroutines.pl. Added two more test .pg files. --- macros/tableau.pl | 227 +++--------------- macros/tableau_main_subroutines.pl | 186 ++++++++++++++ .../tableau_answer_test1.pg | 105 ++++++++ t/matrix_tableau_tests/test_scalar_mult.pg | 60 +++++ 4 files changed, 390 insertions(+), 188 deletions(-) create mode 100644 macros/tableau_main_subroutines.pl create mode 100644 t/matrix_tableau_tests/tableau_answer_test1.pg create mode 100644 t/matrix_tableau_tests/test_scalar_mult.pg diff --git a/macros/tableau.pl b/macros/tableau.pl index cde5c3129d..477212052f 100755 --- a/macros/tableau.pl +++ b/macros/tableau.pl @@ -43,12 +43,13 @@ =head2 DESCRIPTION ---------------------------------------------- | -c | 0 | 1 | 0 | ---------------------------------------------- - Matrix A, the constraint matrix is n by m + Matrix A, the constraint matrix is n=num_problem_vars by m=num_slack_vars Matrix S, the slack variables is m by m Matrix b, the constraint constants is n by 1 - Matrix c, the objective function coefficients is 1 by n + Matrix c, the objective function coefficients matrix is 1 by n or 2 by n The next to the last column holds z or objective value z(...x^i...) = c_i* x^i (Einstein summation convention) + FIXME: allow c to be a 2 by n matrix so that you can do phase1 calculations easily =cut @@ -177,193 +178,46 @@ =head3 References: sub _tableau_init {}; # don't reload this file -package main; -sub isMatrix { - my $m = shift; - return ref($m) =~/Value::Matrix/i; -} -sub matrix_column_slice{ - matrix_from_matrix_cols(@_); -} -sub matrix_from_matrix_cols { - my $M = shift; # a MathObject matrix_columns - my($n,$m) = $M->dimensions; - my @slice = @_; - if (ref($slice[0]) =~ /ARRAY/) { # handle array reference - @slice = @{$slice[0]}; - } - @slice = @slice?@slice : (1..$m); - my @columns = map {$M->column($_)->transpose->value} @slice; - #create the chosen columns as rows - # then transform to array_refs. - Matrix(@columns)->transpose; #transpose and return an n by m matrix (2 dim) -} -sub matrix_row_slice{ - matrix_from_matrix_rows(@_); -} - -sub matrix_from_matrix_rows { - my $M = shift; # a MathObject matrix_columns - unless (isMatrix($M)){ - WARN_MESSAGE( "matrix_from_matrix_rows: |".ref($M)."| or |$M| is not a MathObject Matrix"); - return undef; - } - - my($n,$m) = $M->dimensions; - my @slice = @_; - if (ref($slice[0]) =~ /ARRAY/) { # handle array reference - @slice = @{$slice[0]}; - } - @slice = @slice? @slice : (1..$n); # the default is the whole matrix. - # DEBUG_MESSAGE("row slice in matrix from rows is @slice"); - my @rows = map {[$M->row($_)->value]} @slice; - #create the chosen columns as rows - # then transform to array_refs. - Matrix([@rows]); # insure that it is still an n by m matrix (2 dim) -} - -sub matrix_extract_submatrix { - matrix_from_submatrix(@_); -} -sub matrix_from_submatrix { - my $M=shift; - unless (isMatrix($M)){ - warn( "matrix_from_submatrix: |".ref($M)."| or |$M| is not a MathObject Matrix"); - return undef; - } - - my %options = @_; - my($n,$m) = $M->dimensions; - my $row_slice = ($options{rows})?$options{rows}:[1..$m]; - my $col_slice = ($options{columns})?$options{columns}:[1..$n]; - # DEBUG_MESSAGE("ROW SLICE", join(" ", @$row_slice)); - # DEBUG_MESSAGE("COL SLICE", join(" ", @$col_slice)); - my $M1 = matrix_from_matrix_rows($M,@$row_slice); - # DEBUG_MESSAGE("M1 - matrix from rows) $M1"); - return matrix_from_matrix_cols($M1, @$col_slice); -} -sub matrix_extract_rows { - my $M =shift; - unless (isMatrix($M)){ - WARN_MESSAGE( "matrix_extract_rows: |".ref($M)."| or |$M| is not a MathObject Matrix"); - return undef; - } - - my @slice = @_; - if (ref($slice[0]) =~ /ARRAY/) { # handle array reference - @slice = @{$slice[0]}; - } elsif (@slice == 0) { # export all rows to List - @slice = ( 1..(($M->dimensions)[0]) ); - } - return map {$M->row($_)} @slice ; -} +loadMacros("tableau_main_subroutines.pl"); -sub matrix_rows_to_list { - List(matrix_extract_rows(@_)); -} -sub matrix_columns_to_list { - List(matrix_extract_columns(@_) ); -} -sub matrix_extract_columns { - my $M =shift; # Add error checking - unless (isMatrix($M)){ - WARN_MESSAGE( "matrix_extract_columns: |".ref($M)."| or |$M| is not a MathObject Matrix"); - return undef; - } +# tableauEquivalence is meant to compare two matrices up +# reshuffling the rows and multiplying each row by a constant. +# E.g. equivalent up to multiplying on the left by permuation matrix +# or a (non-uniformly constant) diagonal matrix +#useage tableauEquivalence +# $tableaus_are_equal = +#List($tableau1->extract_rows)->cmp(checker=>tableauEquivalence) +# ->evaluate(List($tableau2->extract_rows))->score : - my @slice = @_; - if (ref($slice[0]) =~ /ARRAY/) { # handle array reference - @slice = @{$slice[0]}; - } elsif (@slice == 0) { # export all columns to an array - @slice = 1..($M->dimensions->[1]); +sub tableauEquivalence { + return sub { + my ($correct, $student, $ansHash) = @_; + # convert matrices to arrays of row references + my @rows1 = matrix_extract_rows($correct); + my @rows2 = matrix_extract_rows($student); + # compare the rows as lists with each row being compared as + # a parallel Vector (i.e. up to multiples) + my $score = List(@rows1)->cmp( checker => + sub { + my ($listcorrect,$liststudent,$listansHash,$nth,$value)=@_; + my $listscore = Vector($listcorrect)->cmp(parallel=>1) + ->evaluate(Vector($liststudent))->{score}; + return $listscore; + } + )->evaluate(List(@rows2))->{score}; + return $score; } - return map {$M->column($_)} @slice; -} + # this function returns a subroutine to be inserted + # into $tableau->cmp(checker=>tableauEquivalence) + } +# Useage linebreak_at_commas +# $foochecker = $constraints->cmp()->withPostFilter( +# linebreak_at_commas() +# ); -######################## -############## -# get_tableau_variable_values -# -# Calculates the values of the basis variables of the tableau, -# assuming the parameter variables are 0. -# -# Usage: ARRAY = get_tableau_variable_values($MathObjectMatrix_tableau, $MathObjectSet_basis) -# -# feature request -- for tableau object -- allow specification of non-zero parameter variables -sub get_tableau_variable_values { - my $mat = shift; # a MathObject matrix - unless (isMatrix($mat)){ - WARN_MESSAGE( "get_tableau_variable_values: |".ref($mat)."| or |$mat| is not a MathObject Matrix"); - return Matrix([0]); - } - my $basis =shift; # a MathObject set - # FIXME - # type check ref($mat)='Matrix'; ref($basis)='Set'; - # or check that $mat has dimensions, element methods; and $basis has a contains method - my ($n, $m) = $mat->dimensions; - @var = (); - #DEBUG_MESSAGE( "start new matrix"); - foreach my $j (1..$m-2) { # the last two columns of the tableau are object variable and constants - if (not $basis->contains($j)) { - # DEBUG_MESSAGE( "j= $j not in basis"); # set the parameter values to zero - $var[$j-1]=0; next; # non-basis variables (parameters) are set to 0. - - } else { - foreach my $i (1..$n-1) { # the last row is the objective function - # if this is a basis column there should be only one non-zero element(the pivot) - if ( $mat->element($i,$j)->value != 0 ) { # should this have ->value????? - $var[$j-1] = ($mat->element($i,$m)/($mat->element($i,$j))->value); - # DEBUG_MESSAGE("i=$i j=$j var = $var[$j-1] "); # calculate basis variable value - next; - } - - } - } - } # element($n, $m-1) is the coefficient of the objective value. - # this last variable is the value of the objective function - # check for division by zero - if ($mat->element($n,$m-1)->value != 0 ) { - push @var , ($mat->element($n,$m)/$mat->element($n,$m-1))->value; - } else { - push @var , '.666'; - } - return wantarray ? @var : \@var; # return either array or reference to an array -} -#### Test -- assume matrix is this -# 1 2 1 0 0 | 0 | 3 -# 4 5 0 1 0 | 0 | 6 -# 7 8 0 0 1 | 0 | 9 -# -1 -2 0 0 0 | 1 | 10 # objective row -# and basis is {3,4,5} (start columns with 1) -# $n= 4; $m = 7 -# $x1=0; $x2=0; $x3=s1=3; $x4=s2=6; $x5=s3=9; w=10=objective value -# -# - -#################################### -# -# Cover for lp_pivot which allows us to use a set object for the new and old basis - -sub lp_basis_pivot { - my ($old_tableau,$old_basis,$pivot) = @_; # $pivot is a Value::Point - my $new_tableau= lp_clone($old_tableau); - # lp_pivot has 0 based indices - main::lp_pivot($new_tableau, $pivot->extract(1)-1,$pivot->extract(2)-1); - # lp_pivot pivots in place - my $new_matrix = Matrix($new_tableau); - my ($n,$m) = $new_matrix->dimensions; - my $param_size = $m-$n -1; #n=constraints+1, #m = $param_size + $constraints +2 - my $new_basis = ( $old_basis - ($pivot->extract(1)+$param_size) + ($pivot->extract(2)) )->sort; - my @statevars = get_tableau_variable_values($new_matrix, $new_basis); - return ( $new_tableau, Set($new_basis),\@statevars); - # force to set (from type Union) to insure that ->data is an array and not a string. -} - - - sub linebreak_at_commas { return sub { my $ans=shift; @@ -378,10 +232,6 @@ sub linebreak_at_commas { $ans; }; } -# Useage -# $foochecker = $constraints->cmp()->withPostFilter( -# linebreak_at_commas() -# ); ################################################## @@ -399,7 +249,7 @@ sub new { my $tableau = { A => undef, # constraint matrix MathObjectMatrix b => undef, # constraint constants Vector or MathObjectMatrix 1 by n - c => undef, # coefficients for objective function Vector or MathObjectMatrix 1 by n + c => undef, # coefficients for objective function MathObjectMatrix 1 by n or 2 by n matrix obj_row => undef, # contains the negative of the coefficients of the objective function. z => undef, # value for objective function n => undef, # dimension of problem variables (columns in A) @@ -408,10 +258,11 @@ sub new { basis => undef, # list describing the current basis columns corresponding to determined variables. B => undef, # square invertible matrix corresponding to the current basis columns M => undef, # matrix of consisting of all columns and all rows except for the objective function row - obj_col_num => undef, # flag indicating the column (1 or n+m+1) for the objective value + obj_col_index => undef, # an array reference indicating the columns (e.g 1 or n+m+1) for the objective value or values constraint_labels => undef, problem_var_labels => undef, slack_var_labels => undef, + @_, }; bless $tableau, $class; diff --git a/macros/tableau_main_subroutines.pl b/macros/tableau_main_subroutines.pl new file mode 100644 index 0000000000..8eb3249854 --- /dev/null +++ b/macros/tableau_main_subroutines.pl @@ -0,0 +1,186 @@ +# subroutines included into the main:: package. + +package main; + +sub isMatrix { + my $m = shift; + return ref($m) =~/Value::Matrix/i; +} +sub matrix_column_slice{ + matrix_from_matrix_cols(@_); +} +sub matrix_from_matrix_cols { + my $M = shift; # a MathObject matrix_columns + my($n,$m) = $M->dimensions; + my @slice = @_; + if (ref($slice[0]) =~ /ARRAY/) { # handle array reference + @slice = @{$slice[0]}; + } + @slice = @slice?@slice : (1..$m); + my @columns = map {$M->column($_)->transpose->value} @slice; + #create the chosen columns as rows + # then transform to array_refs. + Matrix(@columns)->transpose; #transpose and return an n by m matrix (2 dim) +} +sub matrix_row_slice{ + matrix_from_matrix_rows(@_); +} + +sub matrix_from_matrix_rows { + my $M = shift; # a MathObject matrix_columns + unless (isMatrix($M)){ + WARN_MESSAGE( "matrix_from_matrix_rows: |".ref($M)."| or |$M| is not a MathObject Matrix"); + return undef; + } + + my($n,$m) = $M->dimensions; + my @slice = @_; + if (ref($slice[0]) =~ /ARRAY/) { # handle array reference + @slice = @{$slice[0]}; + } + @slice = @slice? @slice : (1..$n); # the default is the whole matrix. + # DEBUG_MESSAGE("row slice in matrix from rows is @slice"); + my @rows = map {[$M->row($_)->value]} @slice; + #create the chosen columns as rows + # then transform to array_refs. + Matrix([@rows]); # insure that it is still an n by m matrix (2 dim) +} + +sub matrix_extract_submatrix { + matrix_from_submatrix(@_); +} +sub matrix_from_submatrix { + my $M=shift; + unless (isMatrix($M)){ + warn( "matrix_from_submatrix: |".ref($M)."| or |$M| is not a MathObject Matrix"); + return undef; + } + + my %options = @_; + my($n,$m) = $M->dimensions; + my $row_slice = ($options{rows})?$options{rows}:[1..$m]; + my $col_slice = ($options{columns})?$options{columns}:[1..$n]; + # DEBUG_MESSAGE("ROW SLICE", join(" ", @$row_slice)); + # DEBUG_MESSAGE("COL SLICE", join(" ", @$col_slice)); + my $M1 = matrix_from_matrix_rows($M,@$row_slice); + # DEBUG_MESSAGE("M1 - matrix from rows) $M1"); + return matrix_from_matrix_cols($M1, @$col_slice); +} +sub matrix_extract_rows { + my $M =shift; + unless (isMatrix($M)){ + WARN_MESSAGE( "matrix_extract_rows: |".ref($M)."| or |$M| is not a MathObject Matrix"); + return undef; + } + + my @slice = @_; + if (ref($slice[0]) =~ /ARRAY/) { # handle array reference + @slice = @{$slice[0]}; + } elsif (@slice == 0) { # export all rows to List + @slice = ( 1..(($M->dimensions)[0]) ); + } + return map {$M->row($_)} @slice ; +} + +sub matrix_rows_to_list { + List(matrix_extract_rows(@_)); +} +sub matrix_columns_to_list { + List(matrix_extract_columns(@_) ); +} +sub matrix_extract_columns { + my $M =shift; # Add error checking + unless (isMatrix($M)){ + WARN_MESSAGE( "matrix_extract_columns: |".ref($M)."| or |$M| is not a MathObject Matrix"); + return undef; + } + + my @slice = @_; + if (ref($slice[0]) =~ /ARRAY/) { # handle array reference + @slice = @{$slice[0]}; + } elsif (@slice == 0) { # export all columns to an array + @slice = 1..($M->dimensions->[1]); + } + return map {$M->column($_)} @slice; +} + + + +######################## +############## +# get_tableau_variable_values +# +# Calculates the values of the basis variables of the tableau, +# assuming the parameter variables are 0. +# +# Usage: ARRAY = get_tableau_variable_values($MathObjectMatrix_tableau, $MathObjectSet_basis) +# +# feature request -- for tableau object -- allow specification of non-zero parameter variables +sub get_tableau_variable_values { + my $mat = shift; # a MathObject matrix + unless (isMatrix($mat)){ + WARN_MESSAGE( "get_tableau_variable_values: |".ref($mat)."| or |$mat| is not a MathObject Matrix"); + return Matrix([0]); + } + my $basis =shift; # a MathObject set + # FIXME + # type check ref($mat)='Matrix'; ref($basis)='Set'; + # or check that $mat has dimensions, element methods; and $basis has a contains method + my ($n, $m) = $mat->dimensions; + @var = (); + #DEBUG_MESSAGE( "start new matrix"); + foreach my $j (1..$m-2) { # the last two columns of the tableau are object variable and constants + if (not $basis->contains($j)) { + # DEBUG_MESSAGE( "j= $j not in basis"); # set the parameter values to zero + $var[$j-1]=0; next; # non-basis variables (parameters) are set to 0. + + } else { + foreach my $i (1..$n-1) { # the last row is the objective function + # if this is a basis column there should be only one non-zero element(the pivot) + if ( $mat->element($i,$j)->value != 0 ) { # should this have ->value????? + $var[$j-1] = ($mat->element($i,$m)/($mat->element($i,$j))->value); + # DEBUG_MESSAGE("i=$i j=$j var = $var[$j-1] "); # calculate basis variable value + next; + } + + } + } + } # element($n, $m-1) is the coefficient of the objective value. + # this last variable is the value of the objective function + # check for division by zero + if ($mat->element($n,$m-1)->value != 0 ) { + push @var , ($mat->element($n,$m)/$mat->element($n,$m-1))->value; + } else { + push @var , '.666'; + } + return wantarray ? @var : \@var; # return either array or reference to an array +} +#### Test -- assume matrix is this +# 1 2 1 0 0 | 0 | 3 +# 4 5 0 1 0 | 0 | 6 +# 7 8 0 0 1 | 0 | 9 +# -1 -2 0 0 0 | 1 | 10 # objective row +# and basis is {3,4,5} (start columns with 1) +# $n= 4; $m = 7 +# $x1=0; $x2=0; $x3=s1=3; $x4=s2=6; $x5=s3=9; w=10=objective value +# +# + +#################################### +# +# Cover for lp_pivot which allows us to use a set object for the new and old basis + +sub lp_basis_pivot { + my ($old_tableau,$old_basis,$pivot) = @_; # $pivot is a Value::Point + my $new_tableau= lp_clone($old_tableau); + # lp_pivot has 0 based indices + main::lp_pivot($new_tableau, $pivot->extract(1)-1,$pivot->extract(2)-1); + # lp_pivot pivots in place + my $new_matrix = Matrix($new_tableau); + my ($n,$m) = $new_matrix->dimensions; + my $param_size = $m-$n -1; #n=constraints+1, #m = $param_size + $constraints +2 + my $new_basis = ( $old_basis - ($pivot->extract(1)+$param_size) + ($pivot->extract(2)) )->sort; + my @statevars = get_tableau_variable_values($new_matrix, $new_basis); + return ( $new_tableau, Set($new_basis),\@statevars); + # force to set (from type Union) to insure that ->data is an array and not a string. +} diff --git a/t/matrix_tableau_tests/tableau_answer_test1.pg b/t/matrix_tableau_tests/tableau_answer_test1.pg new file mode 100644 index 0000000000..753b46cc12 --- /dev/null +++ b/t/matrix_tableau_tests/tableau_answer_test1.pg @@ -0,0 +1,105 @@ +##DESCRIPTION + + + +##ENDDESCRIPTION + + +DOCUMENT(); # This should be the first executable line in the problem. + +loadMacros( + "PGstandard.pl", # Standard macros for PG language + "MathObjects.pl", + "PGML.pl", + "tableau.pl", + #"source.pl", # used to display problem source button + "PGcourse.pl", # Customization file for the course +); + +TEXT(beginproblem()); +$showPartialCorrectAnswers = 1; + +############################################################## +# +# Setup +# +# +Context("Matrix"); + +$m = Matrix([1,2,3],[4,5,6],[7,8,9]); +$n = Matrix([4,5,6],[2,4,6],[7,8,9]); + +@rows1 = matrix_extract_rows($m); +@rows2 = matrix_extract_rows($n); + +@rows1a= map {Vector($_)} @rows1; +TEXT( join(" ", @rows1a)); +@rows2a= map {Vector($_)} @rows2; +############################################################## +# +# Text +# +# +sub tableauEquivalence_orig { + return sub { + my ($correct,$student,$ansHash,$nth,$value)=@_; + my $score = Vector($correct)->cmp(parallel=>1)->evaluate(Vector($student))->{score}; + return $score; + } + } + +# $rh_ans=List(@rows1a)->cmp(checker=>sub { +# my ($correct,$student,$ansHash,$nth,$value)=@_; +# my $score = $correct->cmp(parallel=>1)->evaluate($student)->{score}; +# return $score; +# })->evaluate(List(@rows2a)); + +$rh_ans=List(@rows1a)->cmp(checker=>tableauEquivalence_orig())->evaluate(List(@rows2a)); +$rh_ans2 = List(@rows1)->cmp(checker=>tableauEquivalence_orig())->evaluate(List(@rows2)); +Context()->texStrings; + +$rh_ans3 = $m->cmp(checker=>tableauEquivalence())->evaluate($n); +BEGIN_TEXT +rows : \[\{join(" ", @rows1,"|||",@rows2)\}\]; $PAR + +rows are equal \{$rh_ans->{score}\} $PAR +rows are equal (even with out being classed to Vector) \{$rh_ans2->{score}\} $PAR +Comparing as matrices \{$rh_ans3->{score}\}$PAR +\{pretty_print($rh_ans)\} $PAR +\{pretty_print($rh_ans2)\}$PAR + + + +$PAR + +END_TEXT + +Context()->normalStrings; + +# This works in online PG lab + +# $a= Vector(1,2,3); +# $b= Vector(2,4,6); +# $c= Vector(4,2,3); +# $d=Vector(8,4,6); +# +# $list2=List($d,$a); +# TEXT( $list1 = List($a,$c)->cmp( +# checker=> sub { +# my ($correct,$student,$ansHash,$nth,$value)=@_; +# my $score = $correct->cmp(parallel=>1)->evaluate($student)->{score}; +# return $score; +# } +# )->evaluate($list2)->pretty_print ); +# +# TEXT($a->cmp(parallel=>1)->evaluate($b)->pretty_print); + +############################################################## +# +# Answers +# +# + + + +ENDDOCUMENT(); # This should be the last executable line in the problem. \ No newline at end of file diff --git a/t/matrix_tableau_tests/test_scalar_mult.pg b/t/matrix_tableau_tests/test_scalar_mult.pg new file mode 100644 index 0000000000..11ffbbb906 --- /dev/null +++ b/t/matrix_tableau_tests/test_scalar_mult.pg @@ -0,0 +1,60 @@ +##DESCRIPTION + + + +##ENDDESCRIPTION + + +DOCUMENT(); # This should be the first executable line in the problem. + +loadMacros( + "PGstandard.pl", # Standard macros for PG language + "MathObjects.pl", + #"source.pl", # used to display problem source button + "PGcourse.pl", # Customization file for the course +); + +TEXT(beginproblem()); +$showPartialCorrectAnswers = 1; + +############################################################## +# +# Setup +# +# +Context("Matrix"); + +############################################################## +# +# Text +# +# + +#$m = Matrix([[0,1,0,0,0,0,0,0,0,0.423077],[0.00153846,0,1,-0.00230769,0.00384615,0.00230769,-0.00384615,0,0,6.46154]]); +$c = Real(1300000); # this gives "can't convert a Real Number to a Matrix"; +# $c = Real(130000); # this works +#$c = Real(1300000)->value; # this also works +$m = Matrix([[1,2],[3,4]]); + + +Context()->texStrings; +$string = $c*$m; +BEGIN_TEXT + +\[ $string \] $PAR + +\[\{$c*$m\}\] + +END_TEXT +Context()->normalStrings; + + +############################################################## +# +# Answers +# +# + + + +ENDDOCUMENT(); # This should be the last executable line in the problem. \ No newline at end of file From d27bd2ac9748bf43daa3b5c4f25a9eb97fbdb5bf Mon Sep 17 00:00:00 2001 From: Michael Gage Date: Fri, 13 Oct 2017 21:12:06 -0400 Subject: [PATCH 12/30] Updates to tableau that add tableau->cmp(checker=>tableauEquivalence); This compares to matrices up to row permutation and multiplying individual rows by a constant . this is less than row equivalence, but it what you want to check whether the simplex method is being performed properly (this could be compared to the matching methods in parserLinearInequality.pl) -- perhaps some refactoring???? --- macros/tableau.pl | 43 ++++++---- .../tableau_module_test1.pg | 79 +++++++++++++++++++ t/matrix_tableau_tests/test_scalar_mult.pg | 19 +++-- 3 files changed, 121 insertions(+), 20 deletions(-) create mode 100644 t/matrix_tableau_tests/tableau_module_test1.pg diff --git a/macros/tableau.pl b/macros/tableau.pl index 477212052f..b7601460e5 100755 --- a/macros/tableau.pl +++ b/macros/tableau.pl @@ -59,6 +59,20 @@ =head2 Package main =cut + +=item tableauEquivalence + + ANS( $tableau->cmp(checker=>tableauEquivalence()) ); + + # Note: it is important to include the () at the end of tableauEquivalence + + # tableauEquivalence is meant to compare two matrices up to + # reshuffling the rows and multiplying each row by a constant. + # E.g. equivalent up to multiplying on the left by a permuation matrix + # or a (non-uniformly constant) diagonal matrix + +=cut + =item get_tableau_variable_values Parameters: ($MathObjectMatrix_tableau, $MathObjectSet_basis) @@ -181,18 +195,16 @@ =head3 References: loadMacros("tableau_main_subroutines.pl"); -# tableauEquivalence is meant to compare two matrices up -# reshuffling the rows and multiplying each row by a constant. -# E.g. equivalent up to multiplying on the left by permuation matrix -# or a (non-uniformly constant) diagonal matrix -#useage tableauEquivalence -# $tableaus_are_equal = -#List($tableau1->extract_rows)->cmp(checker=>tableauEquivalence) -# ->evaluate(List($tableau2->extract_rows))->score : + +=head4 Subroutines added to the main:: Package + + +=cut sub tableauEquivalence { return sub { my ($correct, $student, $ansHash) = @_; + # DEBUG_MESSAGE("executing tableau equivalence"); # convert matrices to arrays of row references my @rows1 = matrix_extract_rows($correct); my @rows2 = matrix_extract_rows($student); @@ -208,15 +220,18 @@ sub tableauEquivalence { )->evaluate(List(@rows2))->{score}; return $score; } - # this function returns a subroutine to be inserted - # into $tableau->cmp(checker=>tableauEquivalence) } +=item linebreak_at_commas + + Useage: + + $foochecker = $constraints->cmp()->withPostFilter( + linebreak_at_commas() + ); + +=cut -# Useage linebreak_at_commas -# $foochecker = $constraints->cmp()->withPostFilter( -# linebreak_at_commas() -# ); sub linebreak_at_commas { return sub { diff --git a/t/matrix_tableau_tests/tableau_module_test1.pg b/t/matrix_tableau_tests/tableau_module_test1.pg new file mode 100644 index 0000000000..30cf72eb2f --- /dev/null +++ b/t/matrix_tableau_tests/tableau_module_test1.pg @@ -0,0 +1,79 @@ +############################################## +DOCUMENT(); + +loadMacros( + "PGstandard.pl", # Standard macros for PG language + "MathObjects.pl", + "parserLinearInequality.pl", + "PGML.pl", + "tableau.pl", + "PGmatrixmacros.pl", + "LinearProgramming.pl", + #"source.pl", # allows code to be displayed on certain sites. + "PGcourse.pl", +); + +############################################## + +Context("Matrix"); # need Matrix context to allow string input into Matrix. + +$m = Matrix("[[3,6,7],[2,1,8],[4,6,21],[-6,7,9]]"); +$constraint_matrix = Matrix(" +[[ 0, 0, -1, -1], + [-1, -1, 0, 0 ], + [1, 0 , 1 , 0], + [0, 1, 0, 1]] +"); + +#TEXT ("created ". ref($m)); +#what are the best ways to display a matrix? + +$m1 = $m->row_slice([4,1]); +$m2 = $m->column_slice([3,2,1]); + +$list = $m->extract_rows_to_list(2,3); + +$b = Matrix([1, 2, 3, 4])->transpose; # make it an n by 1 matrix +$c = Matrix([5, 6, 7]); +$t = Tableau->new(A=>$m,b=>$b, c=>$c); + +$c_4 = $t->current_tableau; +############################################################## +# +# Text +# +# + +Context()->texStrings; + +# tableau is \[$c_4\] $PAR +# +# inline tableau is \[\{ $t->current_tableau \}\] +# +# tableau->{M} is \[\{ $t->{M} \}\] +# +# $PAR +# Enter tableau->{M} \{$t->{M}->ans_array\} + +BEGIN_TEXT +m is \[$m\] + +$PAR +enter m = \{$m->ans_array\} + +\{#$m->cmp->evaluate($m)->pretty_print\} + +END_TEXT +Context()->normalStrings; + + +############################################################## +# +# Answers +# +# +#ANS($t->{M}->cmp(checker=>tableauEquivalence()) ); +ANS($m->cmp(checker=>tableauEquivalence())); + + +ENDDOCUMENT(); # This should be the last executable line in the problem. \ No newline at end of file diff --git a/t/matrix_tableau_tests/test_scalar_mult.pg b/t/matrix_tableau_tests/test_scalar_mult.pg index 11ffbbb906..fc74906686 100644 --- a/t/matrix_tableau_tests/test_scalar_mult.pg +++ b/t/matrix_tableau_tests/test_scalar_mult.pg @@ -1,7 +1,13 @@ ##DESCRIPTION - - - +# +# +# tests multiplying a matrix by a large scalar inside BEGIN_TEXT/END_TEXT +# if the scalar is large enough this fails in the presence of texStrings because +# the tex description of $c*$m becomes 1.3\times 10^{6}*\left[\begin{array}{cc} +# 1 &2\cr +# 3 &4 +# \end{array}\right] +# which can't be ##ENDDESCRIPTION @@ -31,19 +37,20 @@ Context("Matrix"); # #$m = Matrix([[0,1,0,0,0,0,0,0,0,0.423077],[0.00153846,0,1,-0.00230769,0.00384615,0.00230769,-0.00384615,0,0,6.46154]]); -$c = Real(1300000); # this gives "can't convert a Real Number to a Matrix"; +$c = Real(1300000); # this gives "can't convert a Real Number to a Matrix" when executed inside texStrings mode; # $c = Real(130000); # this works #$c = Real(1300000)->value; # this also works $m = Matrix([[1,2],[3,4]]); +$string = $c*$m; # this does not cause errors. Context()->texStrings; -$string = $c*$m; +#$string = $c*$m; # this causes errors. BEGIN_TEXT \[ $string \] $PAR -\[\{$c*$m\}\] +\[\{ $c*$m # this causes errors if you remove this comment \} \] END_TEXT Context()->normalStrings; From 334315b43b8b215ffb372e4b3af6bdd67f857dec Mon Sep 17 00:00:00 2001 From: "Davide P. Cervone" Date: Sat, 14 Oct 2017 10:34:07 -0400 Subject: [PATCH 13/30] Fix checks for scalar multiplication to not have to stringify the value if it is already a Real or Complex number. --- lib/Value/Matrix.pm | 5 ++--- lib/Value/Point.pm | 6 ++---- lib/Value/Vector.pm | 6 ++---- 3 files changed, 6 insertions(+), 11 deletions(-) diff --git a/lib/Value/Matrix.pm b/lib/Value/Matrix.pm index f230931256..d03b696e43 100644 --- a/lib/Value/Matrix.pm +++ b/lib/Value/Matrix.pm @@ -209,7 +209,7 @@ sub mult { # # Constant multiplication # - if (Value::matchNumber($r) || Value::isComplex($r)) { + if (Value::isNumber($r)) { my @coords = (); foreach my $x (@{$l->data}) {push(@coords,$x*$r)} return $self->make(@coords); @@ -247,8 +247,7 @@ sub mult { sub div { my ($l,$r,$flag) = @_; my $self = $l; Value::Error("Can't divide by a Matrix") if $flag; - Value::Error("Matrices can only be divided by Numbers") - unless (Value::matchNumber($r) || Value::isComplex($r)); + Value::Error("Matrices can only be divided by Numbers") unless Value::isNumber($r); Value::Error("Division by zero") if $r == 0; my @coords = (); foreach my $x (@{$l->data}) {push(@coords,$x/$r)} diff --git a/lib/Value/Point.pm b/lib/Value/Point.pm index 6ab10d25bf..9d508ef265 100644 --- a/lib/Value/Point.pm +++ b/lib/Value/Point.pm @@ -81,8 +81,7 @@ sub sub { sub mult { my ($l,$r) = @_; my $self = $l; - Value::Error("Points can only be multiplied by Numbers") - unless (Value::matchNumber($r) || Value::isComplex($r)); + Value::Error("Points can only be multiplied by Numbers") unless Value::isNumber($r); my @coords = (); foreach my $x ($l->value) {push(@coords,$x*$r)} return $self->make(@coords); @@ -91,8 +90,7 @@ sub mult { sub div { my ($l,$r,$flag) = @_; my $self = $l; Value::Error("Can't divide by a Point") if $flag; - Value::Error("Points can only be divided by Numbers") - unless (Value::matchNumber($r) || Value::isComplex($r)); + Value::Error("Points can only be divided by Numbers") unless Value::isNumber($r); Value::Error("Division by zero") if $r == 0; my @coords = (); foreach my $x ($l->value) {push(@coords,$x/$r)} diff --git a/lib/Value/Vector.pm b/lib/Value/Vector.pm index 16116a2dab..52ac31f79f 100644 --- a/lib/Value/Vector.pm +++ b/lib/Value/Vector.pm @@ -92,8 +92,7 @@ sub sub { sub mult { my ($l,$r,$flag) = @_; my $self = $l; - Value::Error("Vectors can only be multiplied by Numbers") - unless (Value::matchNumber($r) || Value::isComplex($r)); + Value::Error("Vectors can only be multiplied by Numbers") unless Value::isNumber($r); my @coords = (); foreach my $x ($l->value) {push(@coords,$x*$r)} return $self->make(@coords); @@ -102,8 +101,7 @@ sub mult { sub div { my ($l,$r,$flag) = @_; my $self = $l; Value::Error("Can't divide by a Vector") if $flag; - Value::Error("Vectors can only be divided by Numbers") - unless (Value::matchNumber($r) || Value::isComplex($r)); + Value::Error("Vectors can only be divided by Numbers") unless Value::isNumber($r); Value::Error("Division by zero") if $r == 0; my @coords = (); foreach my $x ($l->value) {push(@coords,$x/$r)} From 7efab9708106280df9c6bdf2f3b133a16e51ee1a Mon Sep 17 00:00:00 2001 From: Michael Gage Date: Sun, 15 Oct 2017 10:24:18 -0400 Subject: [PATCH 14/30] updates to tableau row operations new test file; t/tableau_row_operations_test.pg --- macros/tableau.pl | 311 +++++++++++++++++- .../tableau_row_operations_test.pg | 206 ++++++++++++ t/matrix_tableau_tests/test_scalar_mult.pg | 12 +- 3 files changed, 514 insertions(+), 15 deletions(-) create mode 100644 t/matrix_tableau_tests/tableau_row_operations_test.pg diff --git a/macros/tableau.pl b/macros/tableau.pl index b7601460e5..3dea9a1b97 100755 --- a/macros/tableau.pl +++ b/macros/tableau.pl @@ -120,6 +120,7 @@ =head2 Package tableau B => undef, # square invertible matrix corresponding to the current basis columns M => undef, # matrix of consisting of all columns and all rows except for the objective function row obj_col_num => undef, + current_constraint_matrix=>undef, # flag indicating the column (1 or n+m+1) for the objective value constraint_labels => undef, problem_var_labels => undef, @@ -253,7 +254,8 @@ sub linebreak_at_commas { package Tableau; our @ISA = qw(Value::Matrix Value); - +sub class {"Matrix"}; + sub _Matrix { # can we just import this? Value::Matrix->new(@_); } @@ -273,6 +275,7 @@ sub new { basis => undef, # list describing the current basis columns corresponding to determined variables. B => undef, # square invertible matrix corresponding to the current basis columns M => undef, # matrix of consisting of all columns and all rows except for the objective function row + current_constraint_matrix=>undef, obj_col_index => undef, # an array reference indicating the columns (e.g 1 or n+m+1) for the objective value or values constraint_labels => undef, problem_var_labels => undef, @@ -308,6 +311,8 @@ sub initialize { my @rows = $self->assemble_matrix; # main::DEBUG_MESSAGE("rows", map {ref($_)?$_->value :$_} map {@$_} @rows); $self->{M} = _Matrix([@rows]); + $self->{current_constraint_matrix}= $self->{M}; + $self->{data}= $self->{M}->data; $self->{B} = $self->{M}->submatrix(rows=>[1..($self->{m})],columns=>$self->{basis}); $self->{obj_row} = _Matrix(@{$self->objective_row()}); return(); @@ -347,16 +352,20 @@ sub objective_row { # return a matrix containing the entire tableau sub current_tableau { my $self = shift; - my $Badj = ($self->{B}->det->value) * ($self->{B}->inverse); - my $current_tableau = $Badj * $self->{M}; # the A | S | obj | b - $self->{current_tableau}=$current_tableau; # find the coefficients associated with the basis columns my $c_B = $self->{obj_row}->extract_columns($self->{basis} ); my $c_B2 = Value::Vector->new([ map {$_->value} @$c_B]); - my $correction_coeff = ($c_B2*$current_tableau )->row(1); + my $current_constraint_matrix = $self->{current_constraint_matrix}; + my $B = $self->{B}; + #main::DEBUG_MESSAGE( "basis: current_constraint_matrix $current_constraint_matrix"); + #main::DEBUG_MESSAGE( "current_tableau: matrix is $current_constraint_matrix"); + #main::DEBUG_MESSAGE( "current_basis matrix: $B"); + my $correction_coeff = ($c_B2*($self->{current_constraint_matrix}) )->row(1); # subtract the correction coefficients from the obj_row # this essentially extends Gauss reduction applied to the obj_row - my $obj_row_normalized = ($self->{B}->det->value) *$self->{obj_row}; + #my $B = $self->{B}; + #main::DEBUG_MESSAGE( "det is $B det=", $self->{B}->det->value); + my $obj_row_normalized = abs($self->{B}->det->value)*$self->{obj_row}; #main::DEBUG_MESSAGE(" normalized obj row ",$obj_row_normalized->value); #main::DEBUG_MESSAGE(" correction coeff ", $correction_coeff->value); my $current_coeff = $obj_row_normalized-$correction_coeff ; @@ -368,7 +377,7 @@ sub current_tableau { #main::DEBUG_MESSAGE("type of $self->{basis}", ref($self->{basis}) ); #main::DEBUG_MESSAGE("current basis",join("|", @{$self->{basis}})); #main::DEBUG_MESSAGE("CURRENT STATE ", $current_tableau); - return _Matrix( @{$current_tableau->extract_rows},$self->{current_coeff} ); + return _Matrix( @{$self->{current_constraint_matrix}->extract_rows},$self->{current_coeff} ); #return( $self->{current_coeff} ); } @@ -376,6 +385,8 @@ sub basis { my $self = shift; #update basis # basis is stored as an ARRAY reference. # basis is exported as a list + # FIXME should basis be sorted? + Value::Error( "call basis as a method") unless ref($self)=~/Tableau/; my @input = @_; return Value::List->new($self->{basis}) unless @input; #return basis if no input my $new_basis; @@ -387,13 +398,291 @@ sub basis { $new_basis = \@input; } $self->{basis}= $new_basis; + $self->{B} = $self->{M}->submatrix(rows=>[1..($self->{m})],columns=>$self->{basis}); + my $B = $self->{B}; + #main::DEBUG_MESSAGE("basis: B is $B" ); + my $Badj = abs($self->{B}->det->value) * ($self->{B}->inverse); + my $M = $self->{M}; + my ($row_dim, $col_dim) = $M->dimensions; + my $current_constraint_matrix = $Badj*$M; + $self->{data} = $current_constraint_matrix->data; + $self->{current_constraint_matrix} = $current_constraint_matrix; + my $c_B = $self->{obj_row}->extract_columns($self->{basis} ); + my $c_B2 = Value::Vector->new([ map {$_->value} @$c_B]); + my $correction_coeff = ($c_B2*($self->{current_constraint_matrix}) )->row(1); + my $obj_row_normalized = abs($self->{B}->det->value)*$self->{obj_row}; + my $current_coeff = $obj_row_normalized-$correction_coeff ; + $self->{current_coeff}= $current_coeff; + $self->{current_b} = $current_constraint_matrix->column($col_dim); + # the A | S | obj | b + # main::DEBUG_MESSAGE( "basis: current_constraint_matrix $current_constraint_matrix ". + # ref($self->{current_constraint_matrix}) ); + # main::DEBUG_MESSAGE("basis self ",ref($self), "---", ref($self->{basis})); return Value::List->new($self->{basis}); } +=item find_pivot_column + + ($index, $value, $optimum) = $self->find_pivot_column (max/min, row_number) + +This find the left most obj function coefficient that is negative (for maximizing) +or positive (for minimizing) and returns the value and the index. Only the +index is really needed for this method. The row number is included because there might +be more than one objective function in the table (for example when using +the Auxiliary method in phase1 of the simplex method.) If there is no coefficient +of the appropriate sign then the $optimum flag is set and $index and $value +are undefined. + +=cut + +sub find_pivot_column { + my $self = shift; + my $max_or_min = shift; + my $obj_row_index = shift; + # sanity check + unless ($max_or_min =~ /max|min/) { + Value::Error( "The optimization method must be + 'max' or 'min'. |$max_or_min| is not defined."); + } + #FIXME is this a 1 by n or an n dimensional matrix?? + my $obj_row_matrix = $self->{current_coeff}; + my $obj_col_dim = $obj_row_matrix->dimensions; + $obj_col_dim=$obj_col_dim-2; + #sanity check row + if (not defined($obj_row_index) ) { + $obj_row_index = 1; + } elsif ($obj_row_index<1 or $obj_row_index >$obj_row_dim){ + Value::Error( "The choice for the objective row $obj_row_index is out of range."); + } + #FIXME -- make sure objective row is always a two dimensional matrix, often with one row. + + + my @obj_row = @{$obj_row_matrix->extract_rows($obj_row_index)}; + my $index = undef; + my $optimum = 1; + my $value = undef; + main::DEBUG_MESSAGE(" coldim: $obj_col_dim , + row: $obj_row_index obj_matrix: $obj_row_matrix ",ref($obj_row_matrix) ); + main::DEBUG_MESSAGE(" \@obj_row ", join(' ', @obj_row ) ); + for (my $k=1; $k<=$obj_col_dim; $k++) { + main::DEBUG_MESSAGE("find pivot column: k $k, ", $obj_row_matrix->element($k)->value); + if ( ($obj_row_matrix->element($k) < 0 and $max_or_min eq 'max') or + ($obj_row_matrix->element($k) > 0 and $max_or_min eq 'min') ) { + $index = $k; #memorize index + $value = $obj_row_matrix->element($k); + $optimum = 0; + last; # found first coefficient with correct sign + } + } + return ($index, $value, $optimum); +} + +=item find_pivot_row + + ($index, $value, $unbounded) = $self->find_pivot_row(col_number) + +Compares the ratio $b[$j]/a[$j, $col_number] and chooses the smallest +non-negative entry. It assumes that we are in phase2 of simplex methods +so that $b[j]>0; If all entries are negative (or infinity) then +the $unbounded flag is set and returned and the $index and $value +quantities are undefined. +=cut + +sub find_pivot_row { + my $self = shift; + my $column_index = shift; + my ($row_dim, $col_dim) = $self->{M}->dimensions; + $col_dim = $col_dim-2; # omit the obj_value and constraint columns + # sanity check column_index + unless (1<=$column_index and $column_index <= $col_dim) { + Value::Error( "Column index must be between 1 and $col_dim" ); + } + main::DEBUG_MESSAGE("dim = ($row_dim, $col_dim)"); + my $value = undef; + my $index = undef; + my $unbounded = 1; + for (my $k=1; $k<=$row_dim; $k++) { + my $m = $self->{current_constraint_matrix}->element($k,$column_index); + main::DEBUG_MESSAGE(" m[$k,$column_index] is ", $m->value); + next if $m <=0; + my $b = $self->{current_b}->element($k,1); + main::DEBUG_MESSAGE(" b[$k] is ", $b->value); + main::DEBUG_MESSAGE("finding pivot row in column $column_index, row: $k ", ($b/$m)->value); + if ( not defined($value) or $b/$m < $value) { + $value = $b/$m; + $index = $k; # memorize index + $unbounded = 0; + } + } + return( $index, $value, $unbounded); +} + + + + +=item find_leaving_column + ($index, $value) = $self->find_leaving_column(row_number) + +Finds the non-basis column with a non-zero entry in the given row. When +called with the pivot row number this index gives the column which will +be removed from the basis while the pivot col number gives the basis +column which will become a parameter column. + +=cut + +sub find_leaving_column { + my $self = shift; + my $row_index = shift; + my ($row_dim,$col_dim) = $self->{current_constraint_matrix}->dimensions; + $col_dim= $col_dim - 1; # both problem and slack variables are included + # but not the constraint column or the obj_value column(s) (the latter are zero) + # FIXME + #sanity check row index; + unless (1<=$row_index and $row_index <= $row_dim) { + Value::Error("The row number must be between 1 and $row_dim" ); + } + my $basis = main::Set($self->{basis}); + my $index = 0; + my $value = undef; + foreach my $k (1..$col_dim) { + next unless $basis->contains(main::Set($k)); + $m_ik = $self->{current_constraint_matrix}->element($row_index, $k); + next unless $m_ik !=0; + $index = $k; # memorize index + $value = $m_ik; + last; + } + return( $index, $value); +} +=item find_short_cut_row + + ($index, $value, $feasible)=$self->find_short_cut_row + +Find the most negative entry in the constraint column vector $b. If all entries +are positive then the tableau represents a feasible point, $feasible is set to 1 +and $index and $value are undefined. +=cut + +sub find_short_cut_row { + my $self = shift; +# my @b = map {$_->value} @{ $self->{b}->column(1)->data }; #get raw numbers +# main::DEBUG_MESSAGE("b is ", join(" ", @b )); + my ($row_dim, $col_dim) = $self->{b}->dimensions; +# main::DEBUG_MESSAGE($self->{b}->dimensions); +# main::DEBUG_MESSAGE("dimensions of b: $row_dim, $col_dim "); + my $col_index = 1; + my $index = undef; + my $value = undef; + my $feasible = 1; + for (my $k=1; $k<=$row_dim; $k++) { + my $b_k1 = $self->{current_b}->element($k,$col_index); + # main::DEBUG_MESSAGE("b[$k] = $b_k1"); + next if $b_k1>=0; #skip positive entries; + $index =$k; + $value = $b_k1; + $feasible = 0; + last; + } + return ( $index, $value, $feasible); +} +=item find_short_cut_column + + ($index, $value, $infeasible) = $self->find_short_cut_column(row number) + +Find the left most negative entry in the specified row. If all coefficients are +positive then the tableau represents an infeasible LOP, the $infeasible flag is set, +and the $index and $value are undefined. + +=cut + +sub find_short_cut_column { + my $self = shift; + my $row_index = shift; + my ($row_dim,$col_dim) = $self->{M}->dimensions; + $col_dim = $col_dim - 1; # omit constraint column + # FIXME to adjust for additional obj_value columns + #sanity check row index + unless (1<= $row_index and $row_index <= $row_dim) { + Value::Error("The row must be between 1 and $row_dim"); + } + my $index = undef; + my $value = undef; + my $infeasible = 1; + for (my $k = 1; $k<=$col_dim; $k++ ) { + my $m_ik = $self->{current_constraint_matrix}->element($row_index, $k); + # main::DEBUG_MESSAGE( "in M: ($row_index, $k) contains $m_ik"); + next if $m_ik >=0; + $index = $k; + $value = $m_ik; + $infeasible = 0; + last; + } + return( $index, $value, $infeasible); +} + +=item find_next_short_cut_pivot + + ($row, $col, $feasible, $infeasible) = $self->find_next_short_cut_pivot +=cut + + +=item find_next_pivot + + ($row, $col,$optimum,$unbounded) = $self->find_next_pivot (max/minm row_num) + +This is used in phase2 so the possible outcomes are only $optimum and $unbounded. +$infeasible is not possible. Use the lowest index strategy to find the next pivot +point. This calls find_pivot_row and find_pivot_column. $row and $col are undefined if +either $optimum or $unbounded is set. + +=cut + +sub find_next_pivot { + my $self = shift; + my $max_or_min = shift; + my $row_num =shift; + + # sanity check max or min in find pivot column + my ($col_index, $value, $row_index, $optimum, $unbounded) = (undef,undef,undef,undef); + ($col_index, $value, $optimum) = $self->find_pivot_column($max_or_min, $row_num); + main::DEBUG_MESSAGE("find_next_pivot: col: $col_index, value: $value opt: $optimum "); + return ( $row_index, $col_index, $optimum, $unbounded) if $optimum; + ($row_index, $value, $unbounded) = $self->find_pivot_row($col_index); + main::DEBUG_MESSAGE("find_next pivot row: $row_index, value: $value unbound: $unbounded"); + return ( $row_index, $col_index, $optimum,$unbounded); +} + + + +=item find_next_basis_from_pivot + + List(basis) = $self->find_next_basis (row_index, col_index) + +Calculate the next basis from the current basis given the pivot position. + +=cut + +sub find_next_basis_from_pivot { + my $self = shift; + my $row_index = shift; + my $col_index =shift; + # sanity check max or min in find pivot column + my $basis = main::Set($self->{basis}); + my ($leaving_col_index, $value) = $self->find_leaving_column($row_index); + $basis = $basis - main::Set($leaving_col_index); + main::DEBUG_MESSAGE( "basis is $basis, leaving index $leaving_col_index + entering index is $col_index"); + $basis = [$basis->value, main::Real($col_index)]; + return (main::List($basis)); +} + + +# eventually these routines should be included in the Value::Matrix +# module? package Value::Matrix; sub _Matrix { @@ -401,12 +690,12 @@ sub _Matrix { } sub row_slice { - $self = shift; + my $self = shift; @slice = @_; return _Matrix( $self->extract_rows(@slice) ); } sub extract_rows { - $self = shift; + my $self = shift; my @slice = @_; if (ref($slice[0]) =~ /ARRAY/) { # handle array reference @slice = @{$slice[0]}; @@ -416,11 +705,11 @@ sub extract_rows { return [map {$self->row($_)} @slice ]; #prefer to pass references when possible } sub column_slice { - $self = shift; + my $self = shift; return _Matrix( $self->extract_columns(@_) )->transpose; # matrix is built as rows then transposed. } sub extract_columns { - $self = shift; + my $self = shift; my @slice = @_; if (ref($slice[0]) =~ /ARRAY/) { # handle array reference @slice = @{$slice[0]}; diff --git a/t/matrix_tableau_tests/tableau_row_operations_test.pg b/t/matrix_tableau_tests/tableau_row_operations_test.pg new file mode 100644 index 0000000000..c449ea1838 --- /dev/null +++ b/t/matrix_tableau_tests/tableau_row_operations_test.pg @@ -0,0 +1,206 @@ +##DESCRIPTION + + + +##ENDDESCRIPTION + + +DOCUMENT(); # This should be the first executable line in the problem. + +loadMacros( + "PGstandard.pl", # Standard macros for PG language + "MathObjects.pl", + "MatrixReduce.pl", + #"AppletObjects.pl", + "PGessaymacros.pl", + "PGmatrixmacros.pl", + "PGML.pl", + "LinearProgramming.pl", + "parserLinearInequality.pl", + "quickMatrixEntry.pl", + #"scaffold.pl", + "tableau.pl", + #"gage_matrix_ops.pl", + "PGinfo.pl", + "source.pl", + "PGcourse.pl", +); + +TEXT(beginproblem()); +$showPartialCorrectAnswers = 1; + +############################################################## +# +# Setup +# +# +Context("Matrix"); + +Context()->flags->set( + zeroLevel=>0.001, + zeroLevelTol=>.001 +); +INITIALIZE_QUICK_MATRIX_ENTRY(); + +$money_total = 6000; +$time_total = 600; + +# Bill +$bill_money_commitment = 5000; #dollars +$bill_time_commitment = 400; # hours +$bill_profit = 4700; +# Steve +$steve_money_commitment = 3000; +$steve_time_commitment = 500; +$steve_profit = 4500; + + +#constraint matrix +$A = Matrix([[-1,-$bill_money_commitment,-$bill_time_commitment, -1, 0], + [ -1,-$steve_money_commitment,-$steve_time_commitment, 0, -1, ]]); +$b = Matrix([[-$bill_profit,-$steve_profit]])->transpose; +$c = Matrix([1,0,0,0,0]); + +$tableau1 = Tableau->new(A=>$A, b=>$b, c=>$c); +$m = $tableau1->{M}; +$m2 = $tableau1->{current_constraint_matrix}; +$tableau_orig = $tableau1->current_tableau; +$basis_orig = $tableau1->basis; +#HR +$tableau1->basis(1,2); +$m3 = $tableau1->{M}; +$m4 = $tableau1->{current_constraint_matrix}; +$tableau_orig3 = $tableau1->current_tableau; +$basis_orig3 = $tableau1->basis; +#HR +$tableau1->basis(6,7); +$t1 = $tableau1->current_tableau; +($index1, $value1, $feasible1) = $tableau1->find_short_cut_row(); +($index2, $value2, $infeasible2) = $tableau1->find_short_cut_column(1); +# search row 1 +($index3, $value3) = $tableau1->find_leaving_column(1); +$basis17 = $tableau1->find_next_basis_from_pivot(1,1); +$basis17=Set($basis17)->sort ; + +$tableau1->basis($basis17->value); # basis should be able to accept Set and List objects +$basis17check = $tableau1->basis; +$tableau17= $tableau1->current_tableau; +$matrix17 = $tableau1->{current_constraint_matrix}; # M = A | S |b + +#HR last part of phase 1 +($index11, $value11, $feasible11) = $tableau1->find_short_cut_row(); + +#HR first pivot of phase2 +($pivot1col, $value1c, $optimum1a) = $tableau1->find_pivot_column('min'); +($pivot1row, $value1r, $unbounded1a) = $tableau1->find_pivot_row(2); +#($row2,$col2,$optimum2,$unbounded2) = $tableau1->find_next_pivot('min'); +#$basis2 = $tableau1->find_next_basis_from_pivot($row2, $col2); +#TEXT("basis2 has type ".ref($basis2)); +#$tableau1->basis($basis2->value); +#$tableaudisplay1 = $tableau1->current_tableau; + +#HR second pivot of phase2 +#($pivot3col, $value3c, $optimum3a) = $tableau1->find_pivot_column('min'); +#($pivot3row, $value3r, $unbounded3a) = $tableau1->find_pivot_row(3); +#($row3,$col3,$optimum3,$unbounded3) = $tableau1->find_next_pivot('min'); +#$basis3 = $tableau1->find_next_basis_from_pivot($row3, $col3); +#$tableau1->basis($basis3->value); +#$tableaudisplay2 = $tableau1->current_tableau; + +#HR third pivot of phase2 +#($pivot4col, $value4c, $optimum4a) = $tableau1->find_pivot_column('min'); +#($pivot4row, $value4r, $unbounded4a) = $tableau1->find_pivot_row(3); +#($row4,$col4,$optimum4,$unbounded4) = $tableau1->find_next_pivot('min'); +#optimum is reached so can't go forward from here +#$basis4 = $tableau1->find_next_basis_from_pivot($row4, $col4); +#$tableau1->basis($basis4->value); +#$tableaudisplay3 = $tableau1->current_tableau; + +### experimental +# basis calculation +TEXT(" start with basis (6,7) and pivot on (1,1)"); + $basis = Set(6,7); + $basis = $basis - Set(6); + $basis = List( 1, $basis->value, ); +TEXT( "new basis is $basis"); + +############################################################## +# +# Text +# +# + +Context()->texStrings; +BEGIN_TEXT +matrix: \[ $m \quad $m2 \] $PAR +tableau: \[ $tableau_orig \] $PAR +original basis: $basis_orig $PAR +$HR +matrix: \[ $m3 \quad $m4 \] $PAR +tableau: \[ $tableau_orig3 \] $PAR +new basis: $basis_orig3 $PAR +$HR +\[ $t1\] +find shortcut row: (index, value, feasible) = ($index1, $value1, $feasible1) $PAR +Since it is not feasible yet we'll search row 1 for a short cut pivot $PAR +find shortcut column: (index, value, infeasible) = ($index2, $value2, $infeasible2) $PAR +find leaving_column for row 1: (col_index,value) = ($index3, $value3) $PAR +find new basis from pivot at (1,1): \($basis17\) $PAR +check basis: ($basis17check), \($basis17check\) $PAR +tableau17, matrix17: \[ $tableau17 \qquad $matrix17 \] $PAR +$HR +last part of phase 1 $PAR +find shortcut row: (row $index11,value $value11,feasible $feasible11) $PAR +since the tableau is now feasible we move on to phase2 +$HR + +first pivot of phase 2: $PAR +find pivot col ($pivot1col, $value1c, $optimum1) $PAR +find pivot row ($pivot1row, $value1r, $unbounded1) $PAR +next pivot:(row col optimum unbounded)= +( \{join(",",$tableau1->find_next_pivot('min'))\} ) + $PAR +next basis: (\{$tableau1->basis( +$tableau1->find_next_basis_from_pivot( +($tableau1->find_next_pivot('min'))[0], +($tableau1->find_next_pivot('min'))[1] +)->value +)\}) $PAR +new tableau: $PAR +\[ \{ $tableau1->current_tableau \} \]$PAR + +$HR + +second pivot of phase2: $PAR +next pivot:(row col optimum unbounded)= +( \{join(",",$tableau1->find_next_pivot('min'))\} ) + $PAR +next basis: (\{$tableau1->basis( +$tableau1->find_next_basis_from_pivot( +($tableau1->find_next_pivot('min'))[0], +($tableau1->find_next_pivot('min'))[1] +)->value +)\}) $PAR +new tableau: $PAR +\[ \{ $tableau1->current_tableau \} \]$PAR + +$HR + +third pivot of phase2: $PAR +next pivot:(row col optimum unbounded)= +( \{join(",",$tableau1->find_next_pivot('min'))\} ) +optimum so we stop here + +END_TEXT +Context()->normalStrings; + + +############################################################## +# +# Answers +# +# + + + +ENDDOCUMENT(); # This should be the last executable line in the problem. \ No newline at end of file diff --git a/t/matrix_tableau_tests/test_scalar_mult.pg b/t/matrix_tableau_tests/test_scalar_mult.pg index fc74906686..0be93bcc4b 100644 --- a/t/matrix_tableau_tests/test_scalar_mult.pg +++ b/t/matrix_tableau_tests/test_scalar_mult.pg @@ -36,21 +36,25 @@ Context("Matrix"); # # -#$m = Matrix([[0,1,0,0,0,0,0,0,0,0.423077],[0.00153846,0,1,-0.00230769,0.00384615,0.00230769,-0.00384615,0,0,6.46154]]); + +# in the current develop branch (10/14/2017) there should be no errors generated. + + +$m = Matrix([[0,1,0,0,0,0,0,0,0,0.423077],[0.00153846,0,1,-0.00230769,0.00384615,0.00230769,-0.00384615,0,0,6.46154]]); $c = Real(1300000); # this gives "can't convert a Real Number to a Matrix" when executed inside texStrings mode; # $c = Real(130000); # this works #$c = Real(1300000)->value; # this also works -$m = Matrix([[1,2],[3,4]]); +#$m = Matrix([[1,2],[3,4]]); $string = $c*$m; # this does not cause errors. Context()->texStrings; -#$string = $c*$m; # this causes errors. +$string = $c*$m; # this causes errors. BEGIN_TEXT \[ $string \] $PAR -\[\{ $c*$m # this causes errors if you remove this comment \} \] +\[\{ $c*$m \} \] END_TEXT Context()->normalStrings; From e965e7f5219daaee036f891401f94bdd48579754 Mon Sep 17 00:00:00 2001 From: Michael Gage Date: Sun, 15 Oct 2017 19:58:42 -0400 Subject: [PATCH 15/30] More changes to how variables are passed internally Added two more test .pg files --- macros/tableau.pl | 113 ++++-- .../tableau_row_operations_testB.pg | 323 ++++++++++++++++++ .../tableau_row_operations_testC.pg | 177 ++++++++++ 3 files changed, 578 insertions(+), 35 deletions(-) create mode 100644 t/matrix_tableau_tests/tableau_row_operations_testB.pg create mode 100644 t/matrix_tableau_tests/tableau_row_operations_testC.pg diff --git a/macros/tableau.pl b/macros/tableau.pl index 3dea9a1b97..81adc5ef68 100755 --- a/macros/tableau.pl +++ b/macros/tableau.pl @@ -276,6 +276,8 @@ sub new { B => undef, # square invertible matrix corresponding to the current basis columns M => undef, # matrix of consisting of all columns and all rows except for the objective function row current_constraint_matrix=>undef, + current_coeff=>undef, + current_b => undef, obj_col_index => undef, # an array reference indicating the columns (e.g 1 or n+m+1) for the objective value or values constraint_labels => undef, problem_var_labels => undef, @@ -308,13 +310,15 @@ sub initialize { # main::DEBUG_MESSAGE("m $m, n $n"); $self->{S} = Value::Matrix->I($m); $self->{basis} = [($n+1)...($n+$m)] unless ref($self->{basis})=~/ARRAY/; + my @rows = $self->assemble_matrix; # main::DEBUG_MESSAGE("rows", map {ref($_)?$_->value :$_} map {@$_} @rows); - $self->{M} = _Matrix([@rows]); + $self->{M} = _Matrix([@rows]); #original matrix $self->{current_constraint_matrix}= $self->{M}; $self->{data}= $self->{M}->data; $self->{B} = $self->{M}->submatrix(rows=>[1..($self->{m})],columns=>$self->{basis}); $self->{obj_row} = _Matrix(@{$self->objective_row()}); + $self->basis($self->basis()->value); return(); } @@ -349,27 +353,35 @@ sub objective_row { return \@last_row; } -# return a matrix containing the entire tableau +=item current_tableau + + Useage: + $MathObjectmatrix = $self->current_tableau + $MathObjectmatrix = $self->current_tableau(3,4) #updates basis to (3,4) + +=cut sub current_tableau { my $self = shift; + my @basis = @_; + if (@basis) { + $self->basis(@basis); + } # find the coefficients associated with the basis columns my $c_B = $self->{obj_row}->extract_columns($self->{basis} ); my $c_B2 = Value::Vector->new([ map {$_->value} @$c_B]); - my $current_constraint_matrix = $self->{current_constraint_matrix}; - my $B = $self->{B}; +# my $current_constraint_matrix = $self->{current_constraint_matrix}; +# my $B = $self->{B}; #main::DEBUG_MESSAGE( "basis: current_constraint_matrix $current_constraint_matrix"); #main::DEBUG_MESSAGE( "current_tableau: matrix is $current_constraint_matrix"); #main::DEBUG_MESSAGE( "current_basis matrix: $B"); - my $correction_coeff = ($c_B2*($self->{current_constraint_matrix}) )->row(1); +# my $correction_coeff = ($c_B2*($self->{current_constraint_matrix}) )->row(1); # subtract the correction coefficients from the obj_row # this essentially extends Gauss reduction applied to the obj_row - #my $B = $self->{B}; - #main::DEBUG_MESSAGE( "det is $B det=", $self->{B}->det->value); - my $obj_row_normalized = abs($self->{B}->det->value)*$self->{obj_row}; +# my $obj_row_normalized = abs($self->{B}->det->value)*$self->{obj_row}; #main::DEBUG_MESSAGE(" normalized obj row ",$obj_row_normalized->value); #main::DEBUG_MESSAGE(" correction coeff ", $correction_coeff->value); - my $current_coeff = $obj_row_normalized-$correction_coeff ; - $self->{current_coeff}= $current_coeff; +# my $current_coeff = $obj_row_normalized-$correction_coeff ; +# $self->{current_coeff}= $current_coeff; #main::DEBUG_MESSAGE("subtract these two ", (($self->{B}->det) *$self->{obj_row}), " | ", ($c_B*$current_tableau)->dimensions); #main::DEBUG_MESSAGE("all coefficients", join('|', $self->{obj_row}->value ) ); @@ -397,7 +409,10 @@ sub basis { } else { # input is assumed to be an array $new_basis = \@input; } - $self->{basis}= $new_basis; + $self->{basis}= $new_basis; # this should always be an ARRAY + WARN_MESSAGE("basis $new_basis was not stored as an array reference") + unless ref($new_basis)=~/ARRAY/; + $self->{B} = $self->{M}->submatrix(rows=>[1..($self->{m})],columns=>$self->{basis}); my $B = $self->{B}; @@ -406,13 +421,15 @@ sub basis { my $M = $self->{M}; my ($row_dim, $col_dim) = $M->dimensions; my $current_constraint_matrix = $Badj*$M; - $self->{data} = $current_constraint_matrix->data; - $self->{current_constraint_matrix} = $current_constraint_matrix; my $c_B = $self->{obj_row}->extract_columns($self->{basis} ); my $c_B2 = Value::Vector->new([ map {$_->value} @$c_B]); - my $correction_coeff = ($c_B2*($self->{current_constraint_matrix}) )->row(1); + my $correction_coeff = ($c_B2*($current_constraint_matrix) )->row(1); my $obj_row_normalized = abs($self->{B}->det->value)*$self->{obj_row}; my $current_coeff = $obj_row_normalized-$correction_coeff ; + # updates + $self->{B}= $B; + $self->{data} = $current_constraint_matrix->data; + $self->{current_constraint_matrix} = $current_constraint_matrix; $self->{current_coeff}= $current_coeff; $self->{current_b} = $current_constraint_matrix->column($col_dim); # the A | S | obj | b @@ -445,9 +462,10 @@ sub find_pivot_column { Value::Error( "The optimization method must be 'max' or 'min'. |$max_or_min| is not defined."); } - #FIXME is this a 1 by n or an n dimensional matrix?? my $obj_row_matrix = $self->{current_coeff}; - my $obj_col_dim = $obj_row_matrix->dimensions; + #FIXME $obj_row_matrix is this a 1 by n or an n dimensional matrix?? + my ($obj_col_dim) = $obj_row_matrix->dimensions; + my $obj_row_dim = 1; $obj_col_dim=$obj_col_dim-2; #sanity check row if (not defined($obj_row_index) ) { @@ -459,14 +477,13 @@ sub find_pivot_column { my @obj_row = @{$obj_row_matrix->extract_rows($obj_row_index)}; - my $index = undef; + my $index = -1; my $optimum = 1; my $value = undef; - main::DEBUG_MESSAGE(" coldim: $obj_col_dim , - row: $obj_row_index obj_matrix: $obj_row_matrix ",ref($obj_row_matrix) ); - main::DEBUG_MESSAGE(" \@obj_row ", join(' ', @obj_row ) ); +# main::DEBUG_MESSAGE(" coldim: $obj_col_dim , row: $obj_row_index obj_matrix: $obj_row_matrix ".ref($obj_row_matrix) ); +# main::DEBUG_MESSAGE(" \@obj_row ", join(' ', @obj_row ) ); for (my $k=1; $k<=$obj_col_dim; $k++) { - main::DEBUG_MESSAGE("find pivot column: k $k, ", $obj_row_matrix->element($k)->value); +# main::DEBUG_MESSAGE("find pivot column: k $k, " .$obj_row_matrix->element($k)->value); if ( ($obj_row_matrix->element($k) < 0 and $max_or_min eq 'max') or ($obj_row_matrix->element($k) > 0 and $max_or_min eq 'min') ) { $index = $k; #memorize index @@ -499,17 +516,17 @@ sub find_pivot_row { unless (1<=$column_index and $column_index <= $col_dim) { Value::Error( "Column index must be between 1 and $col_dim" ); } - main::DEBUG_MESSAGE("dim = ($row_dim, $col_dim)"); + # main::DEBUG_MESSAGE("dim = ($row_dim, $col_dim)"); my $value = undef; - my $index = undef; + my $index = -1; my $unbounded = 1; for (my $k=1; $k<=$row_dim; $k++) { my $m = $self->{current_constraint_matrix}->element($k,$column_index); - main::DEBUG_MESSAGE(" m[$k,$column_index] is ", $m->value); + # main::DEBUG_MESSAGE(" m[$k,$column_index] is ", $m->value); next if $m <=0; my $b = $self->{current_b}->element($k,1); - main::DEBUG_MESSAGE(" b[$k] is ", $b->value); - main::DEBUG_MESSAGE("finding pivot row in column $column_index, row: $k ", ($b/$m)->value); + # main::DEBUG_MESSAGE(" b[$k] is ", $b->value); + # main::DEBUG_MESSAGE("finding pivot row in column $column_index, row: $k ", ($b/$m)->value); if ( not defined($value) or $b/$m < $value) { $value = $b/$m; $index = $k; # memorize index @@ -630,6 +647,32 @@ sub find_short_cut_column { =cut +=item find_next_basis + + ($row, $col,$optimum,$unbounded) = $self->find_next_basis (max/minm row_num) + +=cut + + +sub find_next_basis { + my $self = shift; + my $max_or_min = shift; + my $row_num = shift; + my ( $row_index, $col_index, $optimum, $unbounded)= + $self->find_next_pivot($max_or_min, $row_num); + my $flag; + my $basis; + if ($optimum or $unbounded) { + $basis=$self->basis(); + } else { + $flag = ''; + $basis =$self->find_next_basis_from_pivot($row_index,$col_index); + + } + return( $basis->value, $optimum,$unbounded ); + +} + =item find_next_pivot ($row, $col,$optimum,$unbounded) = $self->find_next_pivot (max/minm row_num) @@ -647,13 +690,13 @@ sub find_next_pivot { my $row_num =shift; # sanity check max or min in find pivot column - my ($col_index, $value, $row_index, $optimum, $unbounded) = (undef,undef,undef,undef); + my ($col_index, $value, $row_index, $optimum, $unbounded) = ('','','',''); ($col_index, $value, $optimum) = $self->find_pivot_column($max_or_min, $row_num); - main::DEBUG_MESSAGE("find_next_pivot: col: $col_index, value: $value opt: $optimum "); +# main::DEBUG_MESSAGE("find_next_pivot: col: $col_index, value: $value opt: $optimum "); return ( $row_index, $col_index, $optimum, $unbounded) if $optimum; ($row_index, $value, $unbounded) = $self->find_pivot_row($col_index); - main::DEBUG_MESSAGE("find_next pivot row: $row_index, value: $value unbound: $unbounded"); - return ( $row_index, $col_index, $optimum,$unbounded); +# main::DEBUG_MESSAGE("find_next pivot row: $row_index, value: $value unbound: $unbounded"); + return($row_index, $col_index, $optimum, $unbounded); } @@ -673,11 +716,11 @@ sub find_next_basis_from_pivot { # sanity check max or min in find pivot column my $basis = main::Set($self->{basis}); my ($leaving_col_index, $value) = $self->find_leaving_column($row_index); - $basis = $basis - main::Set($leaving_col_index); - main::DEBUG_MESSAGE( "basis is $basis, leaving index $leaving_col_index - entering index is $col_index"); - $basis = [$basis->value, main::Real($col_index)]; - return (main::List($basis)); + $basis = main::Set( $basis - main::Set($leaving_col_index) + main::Set($col_index)); + # main::DEBUG_MESSAGE( "basis is $basis, leaving index $leaving_col_index + # entering index is $col_index"); + #$basis = [$basis->value, main::Real($col_index)]; + return ($basis); } diff --git a/t/matrix_tableau_tests/tableau_row_operations_testB.pg b/t/matrix_tableau_tests/tableau_row_operations_testB.pg new file mode 100644 index 0000000000..58986618e0 --- /dev/null +++ b/t/matrix_tableau_tests/tableau_row_operations_testB.pg @@ -0,0 +1,323 @@ +##DESCRIPTION + + + +##ENDDESCRIPTION + + +DOCUMENT(); # This should be the first executable line in the problem. + +loadMacros( + "PGstandard.pl", # Standard macros for PG language + "MathObjects.pl", + "MatrixReduce.pl", + #"AppletObjects.pl", + "PGessaymacros.pl", + "PGmatrixmacros.pl", + "PGML.pl", + "LinearProgramming.pl", + "parserLinearInequality.pl", + "quickMatrixEntry.pl", + #"scaffold.pl", + "tableau.pl", + #"gage_matrix_ops.pl", + "PGinfo.pl", + "source.pl", + "PGcourse.pl", +); + +TEXT(beginproblem()); +$showPartialCorrectAnswers = 1; + +############################################################## +# +# Setup +# +# +Context("Matrix"); + +Context()->flags->set( + zeroLevel=>0.001, + zeroLevelTol=>.001 +); +INITIALIZE_QUICK_MATRIX_ENTRY(); + +$money_total = 6000; +$time_total = 600; + +# Bill +$bill_money_commitment = 5000; #dollars +$bill_time_commitment = 400; # hours +$bill_profit = 4700; +# Steve +$steve_money_commitment = 3000; +$steve_time_commitment = 500; +$steve_profit = 4500; + + +#constraint matrix +$A = Matrix([[-1,-$bill_money_commitment,-$bill_time_commitment, -1, 0], + [ -1,-$steve_money_commitment,-$steve_time_commitment, 0, -1, ]]); +$b = Matrix([[-$bill_profit,-$steve_profit]])->transpose; +$c = Matrix([1,0,0,0,0]); + +$tableau1 = Tableau->new(A=>$A, b=>$b, c=>$c); +$m = $tableau1->{M}; +$m2 = $tableau1->{current_constraint_matrix}; +$tableau_orig = $tableau1->current_tableau; +$basis_orig = $tableau1->basis; +#HR +$tableau1->basis(1,2); +$m3 = $tableau1->{M}; +$m4 = $tableau1->{current_constraint_matrix}; +$tableau_orig3 = $tableau1->current_tableau; +$basis_orig3 = $tableau1->basis; +#HR +$tableau1->basis(6,7); +$t1 = $tableau1->current_tableau; +($index1, $value1, $feasible1) = $tableau1->find_short_cut_row(); +($index2, $value2, $infeasible2) = $tableau1->find_short_cut_column(1); +# search row 1 +($index3, $value3) = $tableau1->find_leaving_column(1); +$basis17 = $tableau1->find_next_basis_from_pivot(1,1); +$basis17=Set($basis17)->sort ; + +$tableau1->basis($basis17->value); # basis should be able to accept Set and List objects +$basis17check = $tableau1->basis; +$tableau17= $tableau1->current_tableau; +$matrix17 = $tableau1->{current_constraint_matrix}; # M = A | S |b +#HR last part of phase 1 +($index11, $value11, $feasible11) = $tableau1->find_short_cut_row(); + +#HR first pivot of phase2 +($pivot1col, $value1c, $optimum1a) = $tableau1->find_pivot_column('min'); +($pivot1row, $value1r, $unbounded1a) = $tableau1->find_pivot_row(2); +#($row2,$col2,$optimum2,$unbounded2) = $tableau1->find_next_pivot('min'); +#$basis2 = $tableau1->find_next_basis_from_pivot($row2, $col2); +#TEXT("basis2 has type ".ref($basis2)); +#$tableau1->basis($basis2->value); +#$tableaudisplay1 = $tableau1->current_tableau; + +#HR second pivot of phase2 +#($pivot3col, $value3c, $optimum3a) = $tableau1->find_pivot_column('min'); +#($pivot3row, $value3r, $unbounded3a) = $tableau1->find_pivot_row(3); +#($row3,$col3,$optimum3,$unbounded3) = $tableau1->find_next_pivot('min'); +#$basis3 = $tableau1->find_next_basis_from_pivot($row3, $col3); +#$tableau1->basis($basis3->value); +#$tableaudisplay2 = $tableau1->current_tableau; + +#HR third pivot of phase2 +#($pivot4col, $value4c, $optimum4a) = $tableau1->find_pivot_column('min'); +#($pivot4row, $value4r, $unbounded4a) = $tableau1->find_pivot_row(3); +#($row4,$col4,$optimum4,$unbounded4) = $tableau1->find_next_pivot('min'); +#optimum is reached so can't go forward from here +#$basis4 = $tableau1->find_next_basis_from_pivot($row4, $col4); +#$tableau1->basis($basis4->value); +#$tableaudisplay3 = $tableau1->current_tableau; + +### experimental +# basis calculation +TEXT(" start with basis (6,7) and pivot on (1,1)"); + $basis = Set(6,7); + $basis = $basis - Set(6); + $basis = List( 1, $basis->value, ); +TEXT( "new basis is $basis"); + +############################################################## +# +# Text +# +# +$tableau1->basis(6,7); +@output_find_next_basis=@{$tableau1->find_next_basis('min')}; + +Context()->texStrings; +BEGIN_TEXT +matrix: \[ $m \quad $m2 \] $PAR +tableau: \[ $tableau_orig \] $PAR +original basis: $basis_orig $PAR +$HR +new basis (3,2) +\[\{$tableau1->current_tableau(3,2)\}\] +basis \{$tableau1->basis()\} ,\{List($tableau1->{basis})\} $PAR +basis matrix \[\{$tableau1->{current_constraint_matrix}-> + submatrix(rows=>[1..($tableau1->{m})],columns=>$tableau1->{basis})\}\]; +$PAR +new basis (2,3) +\[\{$tableau1->current_tableau(2,3)\}\] +basis \{$tableau1->basis()\}, \{List($tableau1->{basis})\}$PAR +basis matrix \[\{$tableau1->{current_constraint_matrix}->submatrix(rows=>[1..($tableau1->{m})],columns=>$tableau1->{basis})\}\]; +$PAR +Should figure out why these don't reverse although internally they do. +$HR +Figure out outputs: $PAR +find_next_basis: @output_find_next_basis $PAR +test1 \{[$tableau1->find_next_basis('max')]->[0]\},$PAR +test2 \{[$tableau1->find_next_basis('max')]->[1] \},$PAR + + +$HR +next pivot 1 : \{ join(",",$tableau1->find_next_pivot('max'))\}$PAR +next basis \{ join(",",$tableau1->find_next_basis('max')->[0])\}$PAR +tableau: $PAR +\[\{$tableau1->current_tableau( + [$tableau1->find_next_basis('max')]->[0], + [$tableau1->find_next_basis('max')]->[1] +)\}\] +$HR +next pivot 2 : \{ join(",",$tableau1->find_next_pivot('max'))\}$PAR +next basis : \{ join(",",$tableau1->find_next_basis('max'))\}$PAR +tableau: $PAR +\[\{$tableau1->current_tableau( + [$tableau1->find_next_basis('max')]->[0], + [$tableau1->find_next_basis('max')]->[1] +)\}\] +$HR +next pivot : \(\{List($tableau1->find_next_pivot('max')) \}\)$PAR +next basis = \(\{List($tableau1->find_next_basis('max')) \}\)$PAR +tableau: $PAR +\[\{$tableau1->current_tableau( + [$tableau1->find_next_basis('max')]->[0], + [$tableau1->find_next_basis('max')]->[1] +)\}\] +$HR + +Unit tests $PAR + +next pivot column \{join(",",$tableau1->find_pivot_column('max'))\}$PAR +next pivot row \{ join(",",$tableau1->find_pivot_row(6))\}$PAR +next pivot \{ join(",",$tableau1->find_next_pivot('max'))\}$PAR +next basis \{ join(",",$tableau1->find_next_basis('max'))\}$PAR +test pivot \{ ($tableau1->find_next_pivot('max'))[2]\}$PAR + +LOP is unbounded $PAR +$HR +matrix: \[ $m3 \quad $m4 \] $PAR +tableau: \[ $tableau_orig3 \] $PAR +new basis: $basis_orig3 $PAR +$HR +\[ $t1\] +find shortcut row: (index, value, feasible) = ($index1, $value1, $feasible1) $PAR +Since it is not feasible yet we'll search row 1 for a short cut pivot $PAR +find shortcut column: (index, value, infeasible) = ($index2, $value2, $infeasible2) $PAR +find leaving_column for row 1: (col_index,value) = ($index3, $value3) $PAR +find new basis from pivot at (1,1): \($basis17\) $PAR +check basis: ($basis17check), \($basis17check\) $PAR +tableau17, matrix17: \[ $tableau17 \qquad $matrix17 \] $PAR +$HR +last part of phase 1 $PAR +find shortcut row: (row $index11,value $value11,feasible $feasible11) $PAR +since the tableau is now feasible we move on to phase2 +$HR + +first pivot of phase 2: $PAR +find pivot col ($pivot1col, $value1c, $optimum1) $PAR +find pivot row ($pivot1row, $value1r, $unbounded1) $PAR +next pivot:(row col optimum unbounded)= +( \{join(",",$tableau1->find_next_pivot('min'))\} ) + $PAR +next basis: (\{$tableau1->basis( +$tableau1->find_next_basis_from_pivot( +[$tableau1->find_next_pivot('min')]->[0], +[$tableau1->find_next_pivot('min')]->[1] +)->value +)\}) $PAR +new tableau: $PAR +\[ \{ $tableau1->current_tableau \} \]$PAR + +$HR + +second pivot of phase2: $PAR +next pivot:(row col optimum unbounded)= +( \{join(",",$tableau1->find_next_pivot('min'))\} ) + $PAR +next basis: (\{$tableau1->basis( +$tableau1->find_next_basis_from_pivot( +[$tableau1->find_next_pivot('min')]->[0], +[$tableau1->find_next_pivot('min')]->[1] +)->value +)\}) $PAR +new tableau: $PAR +\[ \{ $tableau1->current_tableau \} \]$PAR + +$HR + +third pivot of phase2: $PAR +next pivot:(row col optimum unbounded)= +( \{join(",",$tableau1->find_next_pivot('min'))\} ) +optimum so we stop here and start going toward the maximum: + + + +$HR + +$HR + + + +next pivot1 : \(\{List($tableau1->find_next_pivot('max')) \}\)$PAR +next basis = \(\{List($tableau1->find_next_basis('max')) \}\)$PAR +tableau: $PAR +\[\{$tableau1->current_tableau( + [$tableau1->find_next_basis('max')]->[0], + [$tableau1->find_next_basis('max')]->[1] +)\}\] +$HR +next pivot2 : \(\{List($tableau1->find_next_pivot('max')) \}\)$PAR +next basis = \(\{List($tableau1->find_next_basis('max')) \}\)$PAR +tableau: $PAR +\[\{$tableau1->current_tableau( + [$tableau1->find_next_basis('max')]->[0], + [$tableau1->find_next_basis('max')]->[1] +)\}\] +$HR +next pivot3 : \(\{List($tableau1->find_next_pivot('max')) \}\)$PAR +next basis = \(\{List($tableau1->find_next_basis('max')) \}\)$PAR +tableau: $PAR +\[\{$tableau1->current_tableau( + [$tableau1->find_next_basis('max')]->[0], + [$tableau1->find_next_basis('max')]->[1] +)\}\] +$HR +next pivot4 ; (\{ join(",", $tableau1->find_next_pivot('max') ) \} )$PAR +next pivot4 : \(\{List($tableau1->find_next_pivot('max')) \}\)$PAR +[~~$tableau1->find_next_pivot('max')]->[3] $PAR +element 4 unbounded : \(\{[$tableau1->find_next_pivot('max')]->[3] \}\)$PAR +element 3 optimal : \(\{[$tableau1->find_next_pivot('max')]->[2] \}\)$PAR +This means we should stop $PAR +next basis = \(\{List($tableau1->find_next_basis('max')) \}\)$PAR +tableau: $PAR +\[\{$tableau1->current_tableau( + [$tableau1->find_next_basis('max')]->[0], + [$tableau1->find_next_basis('max')]->[1] +)\}\] +$HR +next pivot : \(\{List($tableau1->find_next_pivot('max')) \}\)$PAR +next basis = \(\{List($tableau1->find_next_basis('max')) \}\)$PAR +tableau: $PAR +\[\{$tableau1->current_tableau( + [$tableau1->find_next_basis('max')]->[0], + [$tableau1->find_next_basis('max')]->[1] +)\}\] +$HR +next pivot : \(\{List($tableau1->find_next_pivot('max')) \}\)$PAR +next basis = \(\{List($tableau1->find_next_basis('max')) \}\)$PAR +tableau: $PAR +\[\{$tableau1->current_tableau( + [$tableau1->find_next_basis('max')]->[0], + [$tableau1->find_next_basis('max')]->[1] +)\}\] + +END_TEXT +Context()->normalStrings; + + +############################################################## +# +# Answers +# +# + + + +ENDDOCUMENT(); # This should be the last executable line in the problem. \ No newline at end of file diff --git a/t/matrix_tableau_tests/tableau_row_operations_testC.pg b/t/matrix_tableau_tests/tableau_row_operations_testC.pg new file mode 100644 index 0000000000..897710285b --- /dev/null +++ b/t/matrix_tableau_tests/tableau_row_operations_testC.pg @@ -0,0 +1,177 @@ +##DESCRIPTION + + + +##ENDDESCRIPTION + + +DOCUMENT(); # This should be the first executable line in the problem. + +loadMacros( + "PGstandard.pl", # Standard macros for PG language + "MathObjects.pl", + "MatrixReduce.pl", + #"AppletObjects.pl", + "PGessaymacros.pl", + "PGmatrixmacros.pl", + "PGML.pl", + "LinearProgramming.pl", + "parserLinearInequality.pl", + "quickMatrixEntry.pl", + #"scaffold.pl", + "tableau.pl", + #"gage_matrix_ops.pl", + "PGinfo.pl", + "source.pl", + "PGcourse.pl", +); + +TEXT(beginproblem()); +$showPartialCorrectAnswers = 1; + +############################################################## +# +# Setup +# +# +Context("Matrix"); + +Context()->flags->set( + zeroLevel=>0.001, + zeroLevelTol=>.001 +); +INITIALIZE_QUICK_MATRIX_ENTRY(); + +$money_total = 6000; +$time_total = 600; + +# Bill +$bill_money_commitment = 5000; #dollars +$bill_time_commitment = 400; # hours +$bill_profit = 4700; +# Steve +$steve_money_commitment = 3000; +$steve_time_commitment = 500; +$steve_profit = 4500; + + +#constraint matrix +$A = Matrix([[-1,-$bill_money_commitment,-$bill_time_commitment, -1, 0], + [ -1,-$steve_money_commitment,-$steve_time_commitment, 0, -1, ]]); +$b = Matrix([[-$bill_profit,-$steve_profit]])->transpose; +$c = Matrix([1,0,0,0,0]); + +$tableau1 = Tableau->new(A=>$A, b=>$b, c=>$c); +$m = $tableau1->{M}; +$m2 = $tableau1->{current_constraint_matrix}; +$tableau_orig = $tableau1->current_tableau; +$basis_orig = $tableau1->basis; +#HR +$tableau1->basis(1,2); +$m3 = $tableau1->{M}; +$m4 = $tableau1->{current_constraint_matrix}; +$tableau_orig3 = $tableau1->current_tableau; +$basis_orig3 = $tableau1->basis; +#HR +$tableau1->basis(6,7); +$t1 = $tableau1->current_tableau; +($index1, $value1, $feasible1) = $tableau1->find_short_cut_row(); +($index2, $value2, $infeasible2) = $tableau1->find_short_cut_column(1); +# search row 1 +($index3, $value3) = $tableau1->find_leaving_column(1); +$basis17 = $tableau1->find_next_basis_from_pivot(1,1); +$basis17=Set($basis17)->sort ; + +$tableau1->basis($basis17->value); # basis should be able to accept Set and List objects +$basis17check = $tableau1->basis; +$tableau17= $tableau1->current_tableau; +$matrix17 = $tableau1->{current_constraint_matrix}; # M = A | S |b +#HR last part of phase 1 +($index11, $value11, $feasible11) = $tableau1->find_short_cut_row(); + +#HR first pivot of phase2 +($pivot1col, $value1c, $optimum1a) = $tableau1->find_pivot_column('min'); +($pivot1row, $value1r, $unbounded1a) = $tableau1->find_pivot_row(2); +#($row2,$col2,$optimum2,$unbounded2) = $tableau1->find_next_pivot('min'); +#$basis2 = $tableau1->find_next_basis_from_pivot($row2, $col2); +#TEXT("basis2 has type ".ref($basis2)); +#$tableau1->basis($basis2->value); +#$tableaudisplay1 = $tableau1->current_tableau; + +#HR second pivot of phase2 +#($pivot3col, $value3c, $optimum3a) = $tableau1->find_pivot_column('min'); +#($pivot3row, $value3r, $unbounded3a) = $tableau1->find_pivot_row(3); +#($row3,$col3,$optimum3,$unbounded3) = $tableau1->find_next_pivot('min'); +#$basis3 = $tableau1->find_next_basis_from_pivot($row3, $col3); +#$tableau1->basis($basis3->value); +#$tableaudisplay2 = $tableau1->current_tableau; + +#HR third pivot of phase2 +#($pivot4col, $value4c, $optimum4a) = $tableau1->find_pivot_column('min'); +#($pivot4row, $value4r, $unbounded4a) = $tableau1->find_pivot_row(3); +#($row4,$col4,$optimum4,$unbounded4) = $tableau1->find_next_pivot('min'); +#optimum is reached so can't go forward from here +#$basis4 = $tableau1->find_next_basis_from_pivot($row4, $col4); +#$tableau1->basis($basis4->value); +#$tableaudisplay3 = $tableau1->current_tableau; + +### experimental +# basis calculation +TEXT(" start with basis (6,7) and pivot on (1,1)"); + $basis = Set(6,7); + $basis = $basis - Set(6); + $basis = List( 1, $basis->value, ); +TEXT( "new basis is $basis"); + +############################################################## +# +# Text +# +# + +Context()->texStrings; +BEGIN_TEXT +matrix: \[ $m \quad $m2 \] $PAR +tableau: \[ $tableau_orig \] $PAR +original basis: $basis_orig $PAR +$HR +new basis (3,2) +\[\{ $tableau1->current_tableau\}\] +basis \{$tableau1->basis()\} ,\{List($tableau1->{basis})\} $PAR +basis matrix \[\{$tableau1->{current_constraint_matrix}-> + submatrix(rows=>[1..($tableau1->{m})],columns=>$tableau1->{basis})\}\]; +$PAR +new basis (2,3) +\[\{$tableau1->current_tableau(2,3)\}\] +basis \{$tableau1->basis()\}, \{List($tableau1->{basis})\}$PAR +basis matrix \[\{$tableau1->{current_constraint_matrix}->submatrix(rows=>[1..($tableau1->{m})],columns=>$tableau1->{basis})\}\]; +$PAR +Should figure out why these don't reverse although internally they do. +$HR +Figure out outputs: $PAR +\[\{$tableau1->current_tableau(1,7) \} \]$PAR +test0 ( \(\{join(" |", $tableau1->find_next_basis('min'))\}\) )$PAR +test1 ( \{join(" |", @{ List($tableau1->find_next_basis('min'))->data} )\} )$PAR +test2 ( \{ [$tableau1->find_next_basis('min')]->[0]->value \}, +\{ [$tableau1->find_next_basis('min')]->[1]->value \} )$PAR + +find next pivot column ( \{ List( $tableau1->find_pivot_column('min')) \} ) +----(\{ join ",", map {ref($_)} ( $tableau1->find_pivot_column('min')) \}) +$PAR +find next pivot row for col 2: ( \{ List($tableau1->find_pivot_row(2)) \} ) +----(\{ join ",", map {ref($_)} ( $tableau1->find_pivot_row(2)) \}) +$PAR +next pivot \( ( \{ List( $tableau1->find_next_pivot('min')) \} )\) +----(\{ join ",", map {ref($_)} ( $tableau1->find_next_pivot('min')) \}) +$PAR +next basis \( ( \{ List( $tableau1->find_next_basis('min')) \} ) \) +----(\{ join ",", map {ref($_)} ( $tableau1->find_next_basis('min')) \}) +$PAR +test pivot \( ( \{ List($tableau1->find_next_pivot('min')) \} ) \) +$PAR +END_TEXT + +Context()->normalStrings; + +ENDDOCUMENT(); + From 4061f4e0fa532f5031df081cffc1620850c18418 Mon Sep 17 00:00:00 2001 From: Michael Gage Date: Sat, 21 Oct 2017 07:09:48 -0400 Subject: [PATCH 16/30] Add pivoting commands , find_next_pivot, find_next_basis and support. Add more .pg test cases. Add a Test::More test module. --- macros/tableau.pl | 270 ++++++++++++------ t/matrix_tableau_tests/tableau-test6e.pg | 109 +++++++ .../tableau_row_operations_testB.pg | 8 +- .../tableau_row_operations_testC.pg | 20 +- t/matrix_tableau_tests/tableau_test1.pg | 6 +- t/matrix_tableau_tests/test_tableau_more.pl | 69 +++++ 6 files changed, 377 insertions(+), 105 deletions(-) create mode 100644 t/matrix_tableau_tests/tableau-test6e.pg create mode 100755 t/matrix_tableau_tests/test_tableau_more.pl diff --git a/macros/tableau.pl b/macros/tableau.pl index 81adc5ef68..b571ff3bc5 100755 --- a/macros/tableau.pl +++ b/macros/tableau.pl @@ -108,7 +108,7 @@ =head2 Package tableau =item Tableau->new(A=>Matrix, b=>Vector or Matrix, c=>Vector or Matrix) - A => undef, # constraint matrix MathObjectMatrix + A => undef, # original constraint matrix MathObjectMatrix b => undef, # constraint constants Vector or MathObjectMatrix 1 by n c => undef, # coefficients for objective function Vector or MathObjectMatrix 1 by n obj_row => undef, # contains the negative of the coefficients of the objective function. @@ -116,13 +116,21 @@ =head2 Package tableau n => undef, # dimension of problem variables (columns in A) m => undef, # dimension of slack variables (rows in A) S => undef, # square m by m matrix for slack variables - basis => undef, # list describing the current basis columns corresponding to determined variables. - B => undef, # square invertible matrix corresponding to the current basis columns - M => undef, # matrix of consisting of all columns and all rows except for the objective function row - obj_col_num => undef, - current_constraint_matrix=>undef, + M => undef, # matrix of consisting of all original columns and all + rows except for the objective function row + obj_col_num => undef, + basis => undef, # list describing the current basis columns corresponding + to determined variables. + current_basis_matrix => undef, # square invertible matrix + corresponding to the current basis columns + + current_constraint_matrix=>undef, # the current version of [A | S] + current_b, # the current version of the constraint constants b + # current_basis_matrix # (should be new name for B above + # # a square invertible matrix corresponding to the + # # current basis columns) # flag indicating the column (1 or n+m+1) for the objective value - constraint_labels => undef, + constraint_labels => undef, (not sure if this remains relevant after pivots) problem_var_labels => undef, slack_var_labels => undef, @@ -194,12 +202,18 @@ =head3 References: sub _tableau_init {}; # don't reload this file -loadMacros("tableau_main_subroutines.pl"); +# loadMacros("tableau_main_subroutines.pl"); =head4 Subroutines added to the main:: Package +=cut + +=item tableauEquivalence + + + =cut sub tableauEquivalence { @@ -249,6 +263,32 @@ sub linebreak_at_commas { }; } +=item linebreak_at_commas + + Useage: + + lop_display($tableau, align=>'cccc|cc|c|c', toplevel=>[qw(x1,x2,x3,x4,s1,s2,P,b)]) + +Pretty prints the output of a matrix as a LOP with separating labels and +variable labels. + +=cut +sub lop_display { + my $tableau = shift; + %options = @_; + $options{alignment} = ($options{alignment})? $options{alignment}:"|ccccc|cc|c|c|"; + @toplevel = (); + if (exists( ($options{toplevel})) ) { + @toplevel = @{$options{toplevel}}; + $toplevel[0]=[$toplevel[0],headerrow=>1, midrule=>1]; + } + @matrix = $tableau1->current_tableau->value; + $last_row = $#matrix; # last row is objective coefficients + $matrix[$last_row-1]->[0]=[$matrix[$last_row-1]->[0],midrule=>1]; + $matrix[$last_row]->[0]=[$matrix[$last_row]->[0],midrule=>1]; + DataTable([[@toplevel],@matrix],align=>$options{alignment}); +} + ################################################## package Tableau; @@ -301,8 +341,8 @@ sub initialize { unless (ref($self->{A}) =~ /Value::Matrix/ && ref($self->{b}) =~ /Value::Vector|Value::Matrix/ && ref($self->{c}) =~ /Value::Vector|Value::Matrix/){ - main::WARN_MESSAGE("Error: Required inputs: Tableau(A=> Matrix, b=>Vector, c=>Vector)"); - return; + Value::Error ("Error: Required inputs for creating tableau:\n". + "Tableau(A=> Matrix, b=>ColumnVector or Matrix, c=>Vector or Matrix)"); } my ($m, $n)=($self->{A}->dimensions); $self->{n}=$self->{n}//$n; @@ -316,7 +356,7 @@ sub initialize { $self->{M} = _Matrix([@rows]); #original matrix $self->{current_constraint_matrix}= $self->{M}; $self->{data}= $self->{M}->data; - $self->{B} = $self->{M}->submatrix(rows=>[1..($self->{m})],columns=>$self->{basis}); + $self->{current_basis_matrix} = $self->{M}->submatrix(rows=>[1..($self->{m})],columns=>$self->{basis}); $self->{obj_row} = _Matrix(@{$self->objective_row()}); $self->basis($self->basis()->value); return(); @@ -327,6 +367,17 @@ sub assemble_matrix { my @rows =(); my $m = $self->{m}; my $n = $self->{n}; + # sanity check for b; + if (ref($self->{b}) =~/Vector/) { + # replace by n by 1 matrix + $self->{b}=Value::Matrix->new([[$self->{b}->value]])->transpose; + } + my ($constraint_rows, $constraint_cols) = $self->{b}->dimensions; + unless ($constraint_rows== $m and $constraint_cols == 1 ) { + Value::Error("constraint matrix b is $constraint_rows by $constraint_cols but should + be $m by 1 to match the constraint matrix A "); + } + foreach my $i (1..$m) { my @current_row=(); foreach my $j (1..$n) { @@ -346,10 +397,13 @@ sub assemble_matrix { sub objective_row { my $self = shift; + # sanity check for objective row + + my @last_row=(); push @last_row, ( -($self->{c}) )->value; # add the negative coefficients of the obj function foreach my $i (1..($self->{m})) { push @last_row, 0 }; # add 0s for the slack variables - push @last_row, 1, 0; # add the 1 for the objective value and 0 for the initial valu + push @last_row, 1, 0; # add the 1 for the objective value and 0 for the initial value return \@last_row; } @@ -359,6 +413,9 @@ sub objective_row { $MathObjectmatrix = $self->current_tableau $MathObjectmatrix = $self->current_tableau(3,4) #updates basis to (3,4) +Returns the current constraint matrix, including the constraint constants AND the +row containing the objective function coefficients. + =cut sub current_tableau { my $self = shift; @@ -370,20 +427,20 @@ sub current_tableau { my $c_B = $self->{obj_row}->extract_columns($self->{basis} ); my $c_B2 = Value::Vector->new([ map {$_->value} @$c_B]); # my $current_constraint_matrix = $self->{current_constraint_matrix}; -# my $B = $self->{B}; +# my $B = $self->{current_basis_matrix}; #main::DEBUG_MESSAGE( "basis: current_constraint_matrix $current_constraint_matrix"); #main::DEBUG_MESSAGE( "current_tableau: matrix is $current_constraint_matrix"); #main::DEBUG_MESSAGE( "current_basis matrix: $B"); # my $correction_coeff = ($c_B2*($self->{current_constraint_matrix}) )->row(1); # subtract the correction coefficients from the obj_row # this essentially extends Gauss reduction applied to the obj_row -# my $obj_row_normalized = abs($self->{B}->det->value)*$self->{obj_row}; +# my $obj_row_normalized = abs($self->{current_basis_matrix}->det->value)*$self->{obj_row}; #main::DEBUG_MESSAGE(" normalized obj row ",$obj_row_normalized->value); #main::DEBUG_MESSAGE(" correction coeff ", $correction_coeff->value); # my $current_coeff = $obj_row_normalized-$correction_coeff ; # $self->{current_coeff}= $current_coeff; - #main::DEBUG_MESSAGE("subtract these two ", (($self->{B}->det) *$self->{obj_row}), " | ", ($c_B*$current_tableau)->dimensions); + #main::DEBUG_MESSAGE("subtract these two ", (($self->{current_basis_matrix}->det) *$self->{obj_row}), " | ", ($c_B*$current_tableau)->dimensions); #main::DEBUG_MESSAGE("all coefficients", join('|', $self->{obj_row}->value ) ); #main::DEBUG_MESSAGE("current coefficients", join('|', @current_coeff) ); #main::DEBUG_MESSAGE("type of $self->{basis}", ref($self->{basis}) ); @@ -414,20 +471,20 @@ sub basis { unless ref($new_basis)=~/ARRAY/; - $self->{B} = $self->{M}->submatrix(rows=>[1..($self->{m})],columns=>$self->{basis}); - my $B = $self->{B}; + $self->{current_basis_matrix} = $self->{M}->submatrix(rows=>[1..($self->{m})],columns=>$self->{basis}); + my $B = $self->{current_basis_matrix}; #main::DEBUG_MESSAGE("basis: B is $B" ); - my $Badj = abs($self->{B}->det->value) * ($self->{B}->inverse); + my $Badj = abs($self->{current_basis_matrix}->det->value) * ($self->{current_basis_matrix}->inverse); my $M = $self->{M}; my ($row_dim, $col_dim) = $M->dimensions; my $current_constraint_matrix = $Badj*$M; my $c_B = $self->{obj_row}->extract_columns($self->{basis} ); my $c_B2 = Value::Vector->new([ map {$_->value} @$c_B]); my $correction_coeff = ($c_B2*($current_constraint_matrix) )->row(1); - my $obj_row_normalized = abs($self->{B}->det->value)*$self->{obj_row}; + my $obj_row_normalized = abs($self->{current_basis_matrix}->det->value)*$self->{obj_row}; my $current_coeff = $obj_row_normalized-$correction_coeff ; # updates - $self->{B}= $B; + $self->{current_basis_matrix}= $B; $self->{data} = $current_constraint_matrix->data; $self->{current_constraint_matrix} = $current_constraint_matrix; $self->{current_coeff}= $current_coeff; @@ -439,11 +496,96 @@ sub basis { return Value::List->new($self->{basis}); } + +=item find_next_basis + + ($row, $col,$optimum,$unbounded) = $self->find_next_basis (max/min, obj_row_number) + +In phase 2 of the simplex method calculates the next basis. +$optimum or $unbounded is set +if the process has found on the optimum value, or the column +$col gives a certificate of unboundedness. + + +=cut + + +sub find_next_basis { + my $self = shift; + my $max_or_min = shift; + my $obj_row_number = shift; + my ( $row_index, $col_index, $optimum, $unbounded)= + $self->find_next_pivot($max_or_min, $obj_row_number); + my $flag; + my $basis; + if ($optimum or $unbounded) { + $basis=$self->basis(); + } else { + $flag = ''; + $basis =$self->find_next_basis_from_pivot($row_index,$col_index); + + } + return( $basis->value, $optimum,$unbounded ); + +} + +=item find_next_pivot + + ($row, $col,$optimum,$unbounded) = $self->find_next_pivot (max/minm obj_row_number) + +This is used in phase2 so the possible outcomes are only $optimum and $unbounded. +$infeasible is not possible. Use the lowest index strategy to find the next pivot +point. This calls find_pivot_row and find_pivot_column. $row and $col are undefined if +either $optimum or $unbounded is set. + +=cut + +sub find_next_pivot { + my $self = shift; + my $max_or_min = shift; + my $obj_row_number =shift; + + # sanity check max or min in find pivot column + my ($col_index, $value, $row_index, $optimum, $unbounded) = ('','','',''); + ($col_index, $value, $optimum) = $self->find_pivot_column($max_or_min, $obj_row_number); +# main::DEBUG_MESSAGE("find_next_pivot: col: $col_index, value: $value opt: $optimum "); + return ( $row_index, $col_index, $optimum, $unbounded) if $optimum; + ($row_index, $value, $unbounded) = $self->find_pivot_row($col_index); +# main::DEBUG_MESSAGE("find_next pivot row: $row_index, value: $value unbound: $unbounded"); + return($row_index, $col_index, $optimum, $unbounded); +} + + + +=item find_next_basis_from_pivot + + List(basis) = $self->find_next_basis (row_index, col_index) + +Calculate the next basis from the current basis given the pivot position. + +=cut + +sub find_next_basis_from_pivot { + my $self = shift; + my $row_index = shift; + my $col_index =shift; + # sanity check max or min in find pivot column + my $basis = main::Set($self->{basis}); + my ($leaving_col_index, $value) = $self->find_leaving_column($row_index); + $basis = main::Set( $basis - main::Set($leaving_col_index) + main::Set($col_index)); + # main::DEBUG_MESSAGE( "basis is $basis, leaving index $leaving_col_index + # entering index is $col_index"); + #$basis = [$basis->value, main::Real($col_index)]; + return ($basis); +} + + + =item find_pivot_column - ($index, $value, $optimum) = $self->find_pivot_column (max/min, row_number) + ($index, $value, $optimum) = $self->find_pivot_column (max/min, obj_row_number) -This find the left most obj function coefficient that is negative (for maximizing) +This finds the left most obj function coefficient that is negative (for maximizing) or positive (for minimizing) and returns the value and the index. Only the index is really needed for this method. The row number is included because there might be more than one objective function in the table (for example when using @@ -541,7 +683,7 @@ sub find_pivot_row { =item find_leaving_column - ($index, $value) = $self->find_leaving_column(row_number) + ($index, $value) = $self->find_leaving_column(obj_row_number) Finds the non-basis column with a non-zero entry in the given row. When called with the pivot row number this index gives the column which will @@ -574,6 +716,7 @@ sub find_leaving_column { } return( $index, $value); } + =item find_short_cut_row ($index, $value, $feasible)=$self->find_short_cut_row @@ -606,6 +749,7 @@ sub find_short_cut_row { } return ( $index, $value, $feasible); } + =item find_short_cut_column ($index, $value, $infeasible) = $self->find_short_cut_column(row number) @@ -644,85 +788,21 @@ sub find_short_cut_column { =item find_next_short_cut_pivot ($row, $col, $feasible, $infeasible) = $self->find_next_short_cut_pivot -=cut - - -=item find_next_basis - - ($row, $col,$optimum,$unbounded) = $self->find_next_basis (max/minm row_num) - -=cut - - -sub find_next_basis { - my $self = shift; - my $max_or_min = shift; - my $row_num = shift; - my ( $row_index, $col_index, $optimum, $unbounded)= - $self->find_next_pivot($max_or_min, $row_num); - my $flag; - my $basis; - if ($optimum or $unbounded) { - $basis=$self->basis(); - } else { - $flag = ''; - $basis =$self->find_next_basis_from_pivot($row_index,$col_index); - - } - return( $basis->value, $optimum,$unbounded ); -} - -=item find_next_pivot - - ($row, $col,$optimum,$unbounded) = $self->find_next_pivot (max/minm row_num) -This is used in phase2 so the possible outcomes are only $optimum and $unbounded. -$infeasible is not possible. Use the lowest index strategy to find the next pivot -point. This calls find_pivot_row and find_pivot_column. $row and $col are undefined if -either $optimum or $unbounded is set. - -=cut +Following the short-cut algorithm this chooses the next pivot by choosing the row +with the most negative constraint constant entry (top most first in case of tie) and +then the left most negative entry in that row. -sub find_next_pivot { - my $self = shift; - my $max_or_min = shift; - my $row_num =shift; - - # sanity check max or min in find pivot column - my ($col_index, $value, $row_index, $optimum, $unbounded) = ('','','',''); - ($col_index, $value, $optimum) = $self->find_pivot_column($max_or_min, $row_num); -# main::DEBUG_MESSAGE("find_next_pivot: col: $col_index, value: $value opt: $optimum "); - return ( $row_index, $col_index, $optimum, $unbounded) if $optimum; - ($row_index, $value, $unbounded) = $self->find_pivot_row($col_index); -# main::DEBUG_MESSAGE("find_next pivot row: $row_index, value: $value unbound: $unbounded"); - return($row_index, $col_index, $optimum, $unbounded); -} +The process stops with either $feasible=1 (state variables give a feasible point for the +constraints) or $infeasible=1 (a row in the tableau shows that the LOP has empty domain.) +=cut +=item find_next_short_cut_basis -=item find_next_basis_from_pivot - - List(basis) = $self->find_next_basis (row_index, col_index) - -Calculate the next basis from the current basis given the pivot position. - -=cut - -sub find_next_basis_from_pivot { - my $self = shift; - my $row_index = shift; - my $col_index =shift; - # sanity check max or min in find pivot column - my $basis = main::Set($self->{basis}); - my ($leaving_col_index, $value) = $self->find_leaving_column($row_index); - $basis = main::Set( $basis - main::Set($leaving_col_index) + main::Set($col_index)); - # main::DEBUG_MESSAGE( "basis is $basis, leaving index $leaving_col_index - # entering index is $col_index"); - #$basis = [$basis->value, main::Real($col_index)]; - return ($basis); -} +=cut # eventually these routines should be included in the Value::Matrix # module? diff --git a/t/matrix_tableau_tests/tableau-test6e.pg b/t/matrix_tableau_tests/tableau-test6e.pg new file mode 100644 index 0000000000..1b86699989 --- /dev/null +++ b/t/matrix_tableau_tests/tableau-test6e.pg @@ -0,0 +1,109 @@ +DOCUMENT(); +loadMacros( +"PGstandard.pl", +"MathObjects.pl", +"PGmatrixmacros.pl", +"niceTables.pl", +"tableau.pl", +"PGinfo.pl", +"source.pl", +"PGcourse.pl", +); + +TEXT(beginproblem()); +TEXT($BEGIN_ONE_COLUMN); +$showPartialCorrectAnswers = 1; + + +$money_total = 6000; +$time_total = 600; + +# Bill +$bill_money_commitment = 5000; #dollars +$bill_time_commitment = 400; # hours +$bill_profit = 4700; +# Steve +$steve_money_commitment = 3000; +$steve_time_commitment = 500; +$steve_profit = 4500; + + + +#### problem starts here: + +# need error checking to make sure that tableau->new checks +# that inputs are matrices + +$A = Matrix([[-$bill_money_commitment,-$bill_time_commitment, -1, 0], + [ -$steve_money_commitment,-$steve_time_commitment, 0, -1 ]]); +$b = Vector([-$bill_profit,-$steve_profit]); # need vertical vector +$c = ColumnVector([$money_total,$time_total,0,0]); + +BEGIN_TEXT +A => $A $PAR +b => $b $PAR +c => $c $PAR +END_TEXT +# $c = Matrix([[$money_total,$time_total,0,0],[-1,0,0,0,0]]); +# not ready for multiple objective rows + +$tableau1 = Tableau->new(A=>$A, b=>$b, c=>$c); +# slack variables are automatically added + + + +$toprow = [qw(y1 y2 y3 y4 s1 s2 P b) ]; +$align = "cccc|cc|c|c"; + +BEGIN_TEXT +tableau is $BR \(\{$tableau1\}\)$BR +tableau->current_tableau is $BR +\(\{$tableau1->current_tableau\}\)$BR +tableau {current_constraint_matrix} $BR \(\{$tableau1->{current_constraint_matrix}\}\)$BR +tableau {M} $BR \(\{$tableau1->{M}\}\) $BR +current objective row coefficients of tableau $BR \(\{$tableau1->{current_coeff}\}\)$PAR +lop_display() pretty prints the value of tableau->current_tableau with optional decorations. +The usual decorations are an alignment scheme which allows one to add vertical lines +and labels for the rows of the matrix (on the top). The last row is automatically separated +off as the line storing the (negative of) the objective coefficients. + +lop_display(tableau) with no options set $BR +\{lop_display($tableau1)\}$BR +lop_display(tableau, alignment=>"$align", toplevel=>[qw(y1 y2 y3 y4 s1 s2 P b) ])$BR +\{lop_display($tableau1,alignment=>$align,toplevel=>$toprow)\}$PAR + +ok? +$HR +$PAR + +$PAR +END_TEXT + +BEGIN_TEXT +$PAR +OTHER RANDOM TESTS +$PAR + +obj row \{$tableau1->{current_coeff}\} +$PAR +tableau looks like \{pretty_print( $tableau1 )\} + + + + + +$PAR +with datatable +$PAR +\{DataTable([@matrix,[[3,midrule=>1],2,0,0,0,0,1,0]], caption=>"Values",align=>"ccccc|cc|c|c") \} + +$PAR +\{LayoutTable([@matrix,[[3,midrule=>1],2,0,0,0,0,1,0]], caption=>"Values",align=>"ccccc|cc|c|c") \} + +$PAR +\{DataTable( [[1,2,3],[4,5,6]], caption=>"Values") \} + +END_TEXT +Context()->normalStrings; +TEXT($END_ONE_COLUMN); +ENDDOCUMENT(); diff --git a/t/matrix_tableau_tests/tableau_row_operations_testB.pg b/t/matrix_tableau_tests/tableau_row_operations_testB.pg index 58986618e0..6d681714e9 100644 --- a/t/matrix_tableau_tests/tableau_row_operations_testB.pg +++ b/t/matrix_tableau_tests/tableau_row_operations_testB.pg @@ -303,9 +303,11 @@ $HR next pivot : \(\{List($tableau1->find_next_pivot('max')) \}\)$PAR next basis = \(\{List($tableau1->find_next_basis('max')) \}\)$PAR tableau: $PAR -\[\{$tableau1->current_tableau( - [$tableau1->find_next_basis('max')]->[0], - [$tableau1->find_next_basis('max')]->[1] +\[\{lp_display_mm( + $tableau1->current_tableau( + [$tableau1->find_next_basis('max')]->[0], + [$tableau1->find_next_basis('max')]->[1] + ) )\}\] END_TEXT diff --git a/t/matrix_tableau_tests/tableau_row_operations_testC.pg b/t/matrix_tableau_tests/tableau_row_operations_testC.pg index 897710285b..c207571f00 100644 --- a/t/matrix_tableau_tests/tableau_row_operations_testC.pg +++ b/t/matrix_tableau_tests/tableau_row_operations_testC.pg @@ -135,19 +135,31 @@ matrix: \[ $m \quad $m2 \] $PAR tableau: \[ $tableau_orig \] $PAR original basis: $basis_orig $PAR $HR -new basis (3,2) + \[\{ $tableau1->current_tableau\}\] basis \{$tableau1->basis()\} ,\{List($tableau1->{basis})\} $PAR -basis matrix \[\{$tableau1->{current_constraint_matrix}-> +basis matrix \[\{$tableau1->{M}-> submatrix(rows=>[1..($tableau1->{m})],columns=>$tableau1->{basis})\}\]; $PAR new basis (2,3) \[\{$tableau1->current_tableau(2,3)\}\] -basis \{$tableau1->basis()\}, \{List($tableau1->{basis})\}$PAR -basis matrix \[\{$tableau1->{current_constraint_matrix}->submatrix(rows=>[1..($tableau1->{m})],columns=>$tableau1->{basis})\}\]; +basis \{$tableau1->basis()\}, \{join(",",@{$tableau1->{basis}})\}$PAR +basis matrix \[\{$tableau1->{M}->submatrix(rows=>[1..($tableau1->{m})],columns=>$tableau1->{basis})\}\]; $PAR +new basis (3,2) +\[\{$tableau1->current_tableau(3,2)\}\] +basis \{$tableau1->basis()\}, \{join(",",@{$tableau1->{basis}})\}$PAR +basis matrix \[\{$tableau1->{M}->submatrix(rows=>[1..($tableau1->{m})],columns=>$tableau1->{basis})\}\]; Should figure out why these don't reverse although internally they do. $HR +submatrix (1,2) by (1,2) +\[\{$tableau1->{M}->submatrix(rows=>[1,2],columns=>[1,2])\}\]; +\[\[\{$tableau1->{B}\}\]; +submatrix (1,2) by (2,1) + +\[\{$tableau1->{M}->submatrix(rows=>[1,2],columns=>[2,1])\}\]; +\[\[\{$tableau1->{B}\}\]; +$HR Figure out outputs: $PAR \[\{$tableau1->current_tableau(1,7) \} \]$PAR test0 ( \(\{join(" |", $tableau1->find_next_basis('min'))\}\) )$PAR diff --git a/t/matrix_tableau_tests/tableau_test1.pg b/t/matrix_tableau_tests/tableau_test1.pg index 1a36ca7be2..0983a57977 100644 --- a/t/matrix_tableau_tests/tableau_test1.pg +++ b/t/matrix_tableau_tests/tableau_test1.pg @@ -49,11 +49,11 @@ $c_B2 = Value::Vector->new(map {$_->value} @$c_B); $c_4 = $t->current_tableau; -my $Badj = ($t->{B}->det) * ($t->{B}->inverse); +my $Badj = ($t->{current_basis_matrix}->det) * ($t->{current_basis_matrix}->inverse); my $current_tableau = $Badj * $t->{M}; # the A | S | obj | b $correction_coeff = ($c_B2*$current_tableau)->row(1); -$obj_row_normalized = ($t->{B}->det) *$t->{obj_row}; +$obj_row_normalized = ($t->{current_basis_matrix}->det) *$t->{obj_row}; $current_coeff = $obj_row_normalized-$correction_coeff ; TEXT("obj_row ", $t->{obj_row}, $BR ); @@ -71,7 +71,7 @@ original tableau is [`[$t->{M}]`] and basis is [$t->basis] -B is [`[$t->{B}]`] with determinant [$t->{B}->det] +B is [`[$t->{current_basis_matrix}]`] with determinant [$t->{current_basis_matrix}->det] the objective row is [@ $t->{obj_row} @] diff --git a/t/matrix_tableau_tests/test_tableau_more.pl b/t/matrix_tableau_tests/test_tableau_more.pl new file mode 100755 index 0000000000..3a1873a312 --- /dev/null +++ b/t/matrix_tableau_tests/test_tableau_more.pl @@ -0,0 +1,69 @@ +#!/usr/bin/perl -w + +use lib "/Volumes/WW_test/opt/webwork/pg_2014/lib"; +use lib "/Volumes/WW_test/opt/webwork/pg_2014/macros"; +use lib "/Volumes/WW_test/opt/webwork/webwork2/lib"; + +use Test::More; +use Parser; +use Value; +require "tableau.pl"; +require "Value.pl"; + + + +$money_total = 6000; +$time_total = 600; + +# Bill +$bill_money_commitment = 5000; #dollars +$bill_time_commitment = 400; # hours +$bill_profit = 4700; +# Steve +$steve_money_commitment = 3000; +$steve_time_commitment = 500; +$steve_profit = 4500; + + + +#### problem starts here: + +# need error checking to make sure that tableau->new checks +# that inputs are matrices +$ra_matrix = [[-$bill_money_commitment,-$bill_time_commitment, -1, 0, 1,0,0,-$bill_profit], + [-$steve_money_commitment,-$steve_time_commitment, 0, -1, 0,1,0,-$steve_profit], + [-$money_total,-$time_total,0,0, 0,0, 1,0]]; + +$A = Value::Matrix->new([[-$bill_money_commitment,-$bill_time_commitment, -1, 0], + [ -$steve_money_commitment,-$steve_time_commitment, 0, -1 ]]); +$b = Value::Vector->new([-$bill_profit,-$steve_profit]); # need vertical vector +$c = Value::Vector->new([$money_total,$time_total,0,0]); + +$tableau1 = Tableau->new(A=>$A, b=>$b, c=>$c); + +ok (1==1, "trivial first test"); +ok (defined($tableau1), 'tableau has been defined and loaded'); +is ($tableau1->{m}, 2, 'number of constraints is 2'); +is ($tableau1->{n}, 4, 'number of variables is 4'); +is_deeply ( [$tableau1->{m},$tableau1->{n}], [$tableau1->{A}->dimensions], '{m},{n} match dimensions of A'); +is_deeply ($tableau1->{A}, $A, 'constraint matrix'); +is_deeply ($tableau1->{b}, Matrix([$b])->transpose, 'constraint constants is m by 1 matrix'); +is_deeply ($tableau1->{c}, $c, 'objective function constants'); +my $test_constraint_matrix = Matrix($ra_matrix->[0],$ra_matrix->[1]); +is_deeply ($tableau1->{current_constraint_matrix}, $test_constraint_matrix, + 'initialization of current_constraint_matrix'); +is_deeply ($tableau1->{current_b}, $tableau1->{b}, + 'initialization of current_b'); +my $obj_row_test = [ ((-$c)->value, 0,0,1,0) ]; +is_deeply ($tableau1->objective_row, $obj_row_test, + 'initialization of $tableau->{obj_row}'); +is_deeply( ref($tableau1->objective_row), 'ARRAY', '->objective_row has type ARRAY'); +is_deeply( ref($tableau1->{obj_row}), 'Value::Matrix', '->{obj_row} has type Value::Matrix'); + +is_deeply( $tableau1->objective_row, [$tableau1->{obj_row}->value], 'access to {obj_row}'); +is_deeply($tableau1->current_tableau, Matrix($ra_matrix), 'entire tableau including obj coeff row'); +is(ref($tableau1->current_tableau), 'Value::Matrix', '-> current_tableau is Value::Matrix'); + +# check accessors? Use antlers? + +done_testing(); \ No newline at end of file From 2e80c72c35e069c7669f5c71bb55afc563123a4f Mon Sep 17 00:00:00 2001 From: Michael Gage Date: Mon, 23 Oct 2017 20:35:25 -0400 Subject: [PATCH 17/30] Added tests for pivot operations --- macros/tableau.pl | 498 +++++++++++++++----- t/matrix_tableau_tests/test_tableau_more.pl | 201 +++++++- 2 files changed, 574 insertions(+), 125 deletions(-) diff --git a/macros/tableau.pl b/macros/tableau.pl index b571ff3bc5..f8a88564a6 100755 --- a/macros/tableau.pl +++ b/macros/tableau.pl @@ -57,8 +57,6 @@ =head2 DESCRIPTION =head2 Package main -=cut - =item tableauEquivalence @@ -73,6 +71,7 @@ =head2 Package main =cut + =item get_tableau_variable_values Parameters: ($MathObjectMatrix_tableau, $MathObjectSet_basis) @@ -103,10 +102,16 @@ =head2 Package main Used most often when $constraints is a LinearInequality math object. +=cut + =head2 Package tableau -=item Tableau->new(A=>Matrix, b=>Vector or Matrix, c=>Vector or Matrix) +=cut + +=item new + + Tableau->new(A=>Matrix, b=>Vector or Matrix, c=>Vector or Matrix) A => undef, # original constraint matrix MathObjectMatrix b => undef, # constraint constants Vector or MathObjectMatrix 1 by n @@ -133,31 +138,61 @@ =head2 Package tableau constraint_labels => undef, (not sure if this remains relevant after pivots) problem_var_labels => undef, slack_var_labels => undef, + + +=cut -=item $self->current_tableau +=item current_tableau + + $self->current_tableau Parameters: () - Returns: A MathObjectMatrix_tableau + Returns: A MathObjectMatrix This represents the current version of the tableau -=item $self->objective_row +=cut + +=item objective_row + + $self->objective_row Parameters: () Returns: -=item $self->basis +=cut + +=item basis_columns + + ARRAY reference = $self->basis_columns() + [3,4] = $self->basis_columns([3,4]) + + Sets or returns the basis_columns as an ARRAY reference + +=cut + +=item basis + + $self->basis Parameter: ARRAY or ARRAY_ref or () Returns: MathObject_list FiXME -- this should accept a MathObject_List (or MO_Set?) + +=cut =head3 Package Tableau (eventually package Matrix?) -=item $self->row_slice +=item row_slice + + $self->row_slice Parameter: @slice or \@slice Return: MathObject matrix + +=cut + +=item extract_rows -=item $self->extract_rows + $self->extract_rows Parameter: @slice or \@slice Return: two dimensional array ref @@ -167,28 +202,37 @@ =head3 Package Tableau (eventually package Matrix?) Parameter: @slice or \@slice Return: MathObject List of row references -=item $self->extract_columns +=item extract_columns + + $self->extract_columns Parameter: @slice or \@slice Return: two dimensional array ref -=item $self->column_slice +=item column_slice + + $self->column_slice Parameter: @slice or \@slice Return: MathObject Matrix -=item $self->extract_columns_to_list +=item extract_columns_to_list + + $self->extract_columns_to_list Parameter: @slice or \@slice Return: MathObject List of Matrix references ? -=item $self->submatrix +=item submatrix + + $self->submatrix Parameter:(rows=>\@row_slice,columns=>\@column_slice) Return: MathObject matrix =cut + =head3 References: MathObject Matrix methods: L @@ -212,7 +256,7 @@ =head4 Subroutines added to the main:: Package =item tableauEquivalence - + $tableau->cmp(checker=>tableauEquivalence()) =cut @@ -273,6 +317,7 @@ sub linebreak_at_commas { variable labels. =cut + sub lop_display { my $tableau = shift; %options = @_; @@ -282,7 +327,7 @@ sub lop_display { @toplevel = @{$options{toplevel}}; $toplevel[0]=[$toplevel[0],headerrow=>1, midrule=>1]; } - @matrix = $tableau1->current_tableau->value; + @matrix = $tableau->current_tableau->value; $last_row = $#matrix; # last row is objective coefficients $matrix[$last_row-1]->[0]=[$matrix[$last_row-1]->[0],midrule=>1]; $matrix[$last_row]->[0]=[$matrix[$last_row]->[0],midrule=>1]; @@ -292,7 +337,16 @@ sub lop_display { ################################################## package Tableau; -our @ISA = qw(Value::Matrix Value); +our @ISA = qw(Class::Accessor Value::Matrix Value ); +Tableau->mk_accessors(qw( + A b c obj_row z n m S basis_columns B M current_constraint_matrix + current_objective_coeffs current_b current_basis_matrix current_basis_coeff + obj_col_index constraint_labels + problem_var_labels slack_var_labels + +)); + +our $zeroLevelFraction = Value::Real->new(1E-10); sub class {"Matrix"}; @@ -303,7 +357,8 @@ sub _Matrix { # can we just import this? sub new { my $self = shift; my $class = ref($self) || $self; my $context = (Value::isContext($_[0]) ? shift : $self->context); - my $tableau = { + # these labels are passed only to document what the mutators do + my $tableau = Class::Accessor->new({ A => undef, # constraint matrix MathObjectMatrix b => undef, # constraint constants Vector or MathObjectMatrix 1 by n c => undef, # coefficients for objective function MathObjectMatrix 1 by n or 2 by n matrix @@ -312,11 +367,11 @@ sub new { n => undef, # dimension of problem variables (columns in A) m => undef, # dimension of slack variables (rows in A) S => undef, # square m by m matrix for slack variables - basis => undef, # list describing the current basis columns corresponding to determined variables. + basis_columns => undef, # list describing the current basis columns corresponding to determined variables. B => undef, # square invertible matrix corresponding to the current basis columns M => undef, # matrix of consisting of all columns and all rows except for the objective function row current_constraint_matrix=>undef, - current_coeff=>undef, + current_objective_coeffs=>undef, current_b => undef, obj_col_index => undef, # an array reference indicating the columns (e.g 1 or n+m+1) for the objective value or values constraint_labels => undef, @@ -324,18 +379,13 @@ sub new { slack_var_labels => undef, @_, - }; + }); bless $tableau, $class; $tableau->initialize(); return $tableau; } -# the following are used to construct the tableau -# initialize -# assemble_matrix -# objective_row - sub initialize { $self= shift; unless (ref($self->{A}) =~ /Value::Matrix/ && @@ -345,28 +395,26 @@ sub initialize { "Tableau(A=> Matrix, b=>ColumnVector or Matrix, c=>Vector or Matrix)"); } my ($m, $n)=($self->{A}->dimensions); - $self->{n}=$self->{n}//$n; - $self->{m}=$self->{m}//$m; - # main::DEBUG_MESSAGE("m $m, n $n"); - $self->{S} = Value::Matrix->I($m); - $self->{basis} = [($n+1)...($n+$m)] unless ref($self->{basis})=~/ARRAY/; - - my @rows = $self->assemble_matrix; - # main::DEBUG_MESSAGE("rows", map {ref($_)?$_->value :$_} map {@$_} @rows); - $self->{M} = _Matrix([@rows]); #original matrix - $self->{current_constraint_matrix}= $self->{M}; - $self->{data}= $self->{M}->data; - $self->{current_basis_matrix} = $self->{M}->submatrix(rows=>[1..($self->{m})],columns=>$self->{basis}); - $self->{obj_row} = _Matrix(@{$self->objective_row()}); - $self->basis($self->basis()->value); - return(); + $self->n( ($self->n) //$n ); + $self->m( ($self->m) //$m ); + $self->{S} = Value::Matrix->I($m); + $self->{basis_columns} = [($n+1)...($n+$m)] unless ref($self->{basis_columns})=~/ARRAY/; + my @rows = $self->assemble_matrix; + $self->M( _Matrix([@rows]) ); #original matrix + $self->{data}= $self->M->data; + $self->{obj_row} = _Matrix(@{$self->objective_row()}); + # update everything else: + # current_basis_matrix, current_constraint_matrix,current_b + $self->basis($self->basis->value); + + return(); } sub assemble_matrix { my $self = shift; my @rows =(); - my $m = $self->{m}; - my $n = $self->{n}; + my $m = $self->m; + my $n = $self->n; # sanity check for b; if (ref($self->{b}) =~/Vector/) { # replace by n by 1 matrix @@ -402,7 +450,7 @@ sub objective_row { my @last_row=(); push @last_row, ( -($self->{c}) )->value; # add the negative coefficients of the obj function - foreach my $i (1..($self->{m})) { push @last_row, 0 }; # add 0s for the slack variables + foreach my $i (1..($self->m)) { push @last_row, 0 }; # add 0s for the slack variables push @last_row, 1, 0; # add the 1 for the objective value and 0 for the initial value return \@last_row; } @@ -413,87 +461,98 @@ sub objective_row { $MathObjectmatrix = $self->current_tableau $MathObjectmatrix = $self->current_tableau(3,4) #updates basis to (3,4) -Returns the current constraint matrix, including the constraint constants AND the +Returns the current constraint matrix as a MathObjectMatrix, +including the constraint constants, +problem variable coefficients, slack variable coefficients AND the row containing the objective function coefficients. + +If a list of basis columns is passed as an argument then $self->basis() +is called to switch the tableau to the new basis before returning +the tableau. =cut + sub current_tableau { my $self = shift; + Value::Error( "call current_tableau as a Tableau method") unless ref($self)=~/Tableau/; my @basis = @_; if (@basis) { $self->basis(@basis); } - # find the coefficients associated with the basis columns - my $c_B = $self->{obj_row}->extract_columns($self->{basis} ); - my $c_B2 = Value::Vector->new([ map {$_->value} @$c_B]); -# my $current_constraint_matrix = $self->{current_constraint_matrix}; -# my $B = $self->{current_basis_matrix}; - #main::DEBUG_MESSAGE( "basis: current_constraint_matrix $current_constraint_matrix"); - #main::DEBUG_MESSAGE( "current_tableau: matrix is $current_constraint_matrix"); - #main::DEBUG_MESSAGE( "current_basis matrix: $B"); -# my $correction_coeff = ($c_B2*($self->{current_constraint_matrix}) )->row(1); - # subtract the correction coefficients from the obj_row - # this essentially extends Gauss reduction applied to the obj_row -# my $obj_row_normalized = abs($self->{current_basis_matrix}->det->value)*$self->{obj_row}; - #main::DEBUG_MESSAGE(" normalized obj row ",$obj_row_normalized->value); - #main::DEBUG_MESSAGE(" correction coeff ", $correction_coeff->value); -# my $current_coeff = $obj_row_normalized-$correction_coeff ; -# $self->{current_coeff}= $current_coeff; - - #main::DEBUG_MESSAGE("subtract these two ", (($self->{current_basis_matrix}->det) *$self->{obj_row}), " | ", ($c_B*$current_tableau)->dimensions); - #main::DEBUG_MESSAGE("all coefficients", join('|', $self->{obj_row}->value ) ); - #main::DEBUG_MESSAGE("current coefficients", join('|', @current_coeff) ); - #main::DEBUG_MESSAGE("type of $self->{basis}", ref($self->{basis}) ); - #main::DEBUG_MESSAGE("current basis",join("|", @{$self->{basis}})); - #main::DEBUG_MESSAGE("CURRENT STATE ", $current_tableau); - return _Matrix( @{$self->{current_constraint_matrix}->extract_rows},$self->{current_coeff} ); - #return( $self->{current_coeff} ); + return _Matrix( @{$self->current_constraint_matrix->extract_rows}, + $self->current_objective_coeffs ); } +=item basis + + ListObjectList = $self->basis + ListObjectList = $self->basis(3,4) + ListObjectList = $self->basis([3,4]) + ListObjectList = $self->basis(Set(3,4)) + + to obtain ARRAY reference use + [3,4]== $self->basis(Set3,4)->value + +Returns a MathObjectList containing the current basis columns. If basis columns +are provided as arguments it resets all elements of the tableau to present +the view corresponding to the new choice of basis columns. + +=cut + sub basis { my $self = shift; #update basis # basis is stored as an ARRAY reference. # basis is exported as a list # FIXME should basis be sorted? - Value::Error( "call basis as a method") unless ref($self)=~/Tableau/; + Value::Error( "call basis as a Tableau method") unless ref($self)=~/Tableau/; my @input = @_; - return Value::List->new($self->{basis}) unless @input; #return basis if no input + return Value::List->new($self->{basis_columns}) unless @input; #return basis if no input my $new_basis; if (ref( $input[0]) =~/ARRAY/) { $new_basis=$input[0]; } elsif (ref( $input[0]) =~/List|Set/){ - $new_basis = $input[0]->value; + $new_basis = [$input[0]->value]; } else { # input is assumed to be an array $new_basis = \@input; } - $self->{basis}= $new_basis; # this should always be an ARRAY - WARN_MESSAGE("basis $new_basis was not stored as an array reference") + $self->{basis_columns}= $new_basis; # this should always be an ARRAY + main::WARN_MESSAGE("basis $new_basis was not stored as an array reference") unless ref($new_basis)=~/ARRAY/; + # form new basis + my $matrix = $self->M->submatrix(rows=>[1..($self->m)],columns=>$self->basis_columns); + my $basis_det = $matrix->det; + if ($basis_det == 0 ){ + Value::Error("The columns ", join(",",$self->basis_columns)." cannot form a basis"); + } + $self->current_basis_matrix( $matrix ); + $self->current_basis_coeff(abs($basis_det)); - $self->{current_basis_matrix} = $self->{M}->submatrix(rows=>[1..($self->{m})],columns=>$self->{basis}); - my $B = $self->{current_basis_matrix}; + #my $B = $self->current_basis_matrix; #deprecate B + #$self->{current_basis_matrix}= $B; #main::DEBUG_MESSAGE("basis: B is $B" ); - my $Badj = abs($self->{current_basis_matrix}->det->value) * ($self->{current_basis_matrix}->inverse); + + my $Badj = ($self->current_basis_coeff) * ($self->current_basis_matrix->inverse); my $M = $self->{M}; my ($row_dim, $col_dim) = $M->dimensions; my $current_constraint_matrix = $Badj*$M; - my $c_B = $self->{obj_row}->extract_columns($self->{basis} ); + my $c_B = $self->obj_row->extract_columns($self->basis_columns ); my $c_B2 = Value::Vector->new([ map {$_->value} @$c_B]); my $correction_coeff = ($c_B2*($current_constraint_matrix) )->row(1); my $obj_row_normalized = abs($self->{current_basis_matrix}->det->value)*$self->{obj_row}; - my $current_coeff = $obj_row_normalized-$correction_coeff ; + my $current_objective_coeffs = $obj_row_normalized-$correction_coeff ; # updates - $self->{current_basis_matrix}= $B; $self->{data} = $current_constraint_matrix->data; $self->{current_constraint_matrix} = $current_constraint_matrix; - $self->{current_coeff}= $current_coeff; + $self->{current_objective_coeffs}= $current_objective_coeffs; $self->{current_b} = $current_constraint_matrix->column($col_dim); - # the A | S | obj | b + + # the A | S | obj | b # main::DEBUG_MESSAGE( "basis: current_constraint_matrix $current_constraint_matrix ". # ref($self->{current_constraint_matrix}) ); - # main::DEBUG_MESSAGE("basis self ",ref($self), "---", ref($self->{basis})); - return Value::List->new($self->{basis}); + # main::DEBUG_MESSAGE("basis self ",ref($self), "---", ref($self->{basis_columns})); + + return Value::List->new($self->{basis_columns}); } @@ -511,7 +570,7 @@ sub basis { sub find_next_basis { - my $self = shift; + my $self = shift;Value::Error( "call find_next_basis as a Tableau method") unless ref($self)=~/Tableau/; my $max_or_min = shift; my $obj_row_number = shift; my ( $row_index, $col_index, $optimum, $unbounded)= @@ -542,6 +601,7 @@ sub find_next_basis { sub find_next_pivot { my $self = shift; + Value::Error( "call find_next_pivot as a Tableau method") unless ref($self)=~/Tableau/; my $max_or_min = shift; my $obj_row_number =shift; @@ -559,23 +619,28 @@ sub find_next_pivot { =item find_next_basis_from_pivot - List(basis) = $self->find_next_basis (row_index, col_index) + List(basis) = $self->find_next_basis (pivot_row, pivot_column) -Calculate the next basis from the current basis given the pivot position. +Calculate the next basis from the current basis +given the pivot position. =cut sub find_next_basis_from_pivot { my $self = shift; + Value::Error( "call find_next_basis_from_pivot as a Tableau method") unless ref($self)=~/Tableau/; my $row_index = shift; my $col_index =shift; + if (Value::Set->new( $self->basis_columns)->contains(Value::Set->new($col_index))){ + Value::Error(" pivot point should not be in a basis column ($row_index, $col_index) ") + } # sanity check max or min in find pivot column - my $basis = main::Set($self->{basis}); + my $basis = main::Set($self->{basis_columns}); my ($leaving_col_index, $value) = $self->find_leaving_column($row_index); - $basis = main::Set( $basis - main::Set($leaving_col_index) + main::Set($col_index)); + $basis = main::Set( $basis - Value::Set->new($leaving_col_index) + main::Set($col_index)); # main::DEBUG_MESSAGE( "basis is $basis, leaving index $leaving_col_index # entering index is $col_index"); - #$basis = [$basis->value, main::Real($col_index)]; + #$basis = [$basis->value, Value::Real->new($col_index)]; return ($basis); } @@ -597,6 +662,7 @@ sub find_next_basis_from_pivot { sub find_pivot_column { my $self = shift; + Value::Error( "call find_pivot_column as a Tableau method") unless ref($self)=~/Tableau/; my $max_or_min = shift; my $obj_row_index = shift; # sanity check @@ -604,7 +670,7 @@ sub find_pivot_column { Value::Error( "The optimization method must be 'max' or 'min'. |$max_or_min| is not defined."); } - my $obj_row_matrix = $self->{current_coeff}; + my $obj_row_matrix = $self->{current_objective_coeffs}; #FIXME $obj_row_matrix is this a 1 by n or an n dimensional matrix?? my ($obj_col_dim) = $obj_row_matrix->dimensions; my $obj_row_dim = 1; @@ -622,14 +688,17 @@ sub find_pivot_column { my $index = -1; my $optimum = 1; my $value = undef; + my $zeroLevelTol = $zeroLevelFraction * ($self->current_basis_coeff); # main::DEBUG_MESSAGE(" coldim: $obj_col_dim , row: $obj_row_index obj_matrix: $obj_row_matrix ".ref($obj_row_matrix) ); # main::DEBUG_MESSAGE(" \@obj_row ", join(' ', @obj_row ) ); for (my $k=1; $k<=$obj_col_dim; $k++) { # main::DEBUG_MESSAGE("find pivot column: k $k, " .$obj_row_matrix->element($k)->value); - if ( ($obj_row_matrix->element($k) < 0 and $max_or_min eq 'max') or - ($obj_row_matrix->element($k) > 0 and $max_or_min eq 'min') ) { + + if ( ($obj_row_matrix->element($k) < -$zeroLevelTol and $max_or_min eq 'max') or + ($obj_row_matrix->element($k) > $zeroLevelTol and $max_or_min eq 'min') ) { $index = $k; #memorize index $value = $obj_row_matrix->element($k); + # main::diag("value is $value : is zero:=", (main::Real($value) == main::Real(0))?1:0); $optimum = 0; last; # found first coefficient with correct sign } @@ -651,6 +720,7 @@ sub find_pivot_column { sub find_pivot_row { my $self = shift; + Value::Error( "call find_pivot_row as a Tableau method") unless ref($self)=~/Tableau/; my $column_index = shift; my ($row_dim, $col_dim) = $self->{M}->dimensions; $col_dim = $col_dim-2; # omit the obj_value and constraint columns @@ -662,10 +732,11 @@ sub find_pivot_row { my $value = undef; my $index = -1; my $unbounded = 1; + my $zeroLevelTol = $zeroLevelFraction * ($self->current_basis_coeff); for (my $k=1; $k<=$row_dim; $k++) { my $m = $self->{current_constraint_matrix}->element($k,$column_index); # main::DEBUG_MESSAGE(" m[$k,$column_index] is ", $m->value); - next if $m <=0; + next if $m <=$zeroLevelTol; my $b = $self->{current_b}->element($k,1); # main::DEBUG_MESSAGE(" b[$k] is ", $b->value); # main::DEBUG_MESSAGE("finding pivot row in column $column_index, row: $k ", ($b/$m)->value); @@ -694,16 +765,17 @@ sub find_pivot_row { sub find_leaving_column { my $self = shift; + Value::Error( "call find_leaving_column as a Tableau method") unless ref($self)=~/Tableau/; my $row_index = shift; my ($row_dim,$col_dim) = $self->{current_constraint_matrix}->dimensions; $col_dim= $col_dim - 1; # both problem and slack variables are included # but not the constraint column or the obj_value column(s) (the latter are zero) - # FIXME + #sanity check row index; unless (1<=$row_index and $row_index <= $row_dim) { Value::Error("The row number must be between 1 and $row_dim" ); } - my $basis = main::Set($self->{basis}); + my $basis = main::Set($self->{basis_columns}); my $index = 0; my $value = undef; foreach my $k (1..$col_dim) { @@ -717,6 +789,61 @@ sub find_leaving_column { return( $index, $value); } +=item find_next_short_cut_pivot + + ($row, $col, $feasible, $infeasible) = $self->find_next_short_cut_pivot + + +Following the short-cut algorithm this chooses the next pivot by choosing the row +with the most negative constraint constant entry (top most first in case of tie) and +then the left most negative entry in that row. + +The process stops with either $feasible=1 (state variables give a feasible point for the +constraints) or $infeasible=1 (a row in the tableau shows that the LOP has empty domain.) + +=cut + +sub find_next_short_cut_pivot { + my $self = shift; + Value::Error( "call find_next_short_cut_pivot as a Tableau method") unless ref($self)=~/Tableau/; + + my ($col_index, $value, $row_index, $feasible_point, $infeasible_lop) = ('','','',''); + ($row_index, $value, $feasible_point) = $self->find_short_cut_row(); + if ($feasible_point) { + $row_index=undef; $col_index=undef; $infeasible_lop=0; + } else { + ($col_index, $value, $infeasible_lop) = $self->find_short_cut_column($row_index); + if ($infeasible_lop){ + $row_index=undef; $col_index=undef; $feasible_point=0; + } + } + return($row_index, $col_index, $feasible_point, $infeasible_lop); +} + +=item find_next_short_cut_basis + + +FIXME -- this needs to be written ? +just find_next_basis_from_pivot should work? + +=cut + + +sub find_next_short_cut_basis { + my $self = shift;Value::Error( "call find_next_short_cut_basis as a Tableau method") unless ref($self)=~/Tableau/; + + my ( $row_index, $col_index, $feasible_point, $infeasible_lop)= + $self->find_next_short_cut_pivot(); + my $basis; + if ($feasible_point or $infeasible_lop) { + $basis=$self->basis(); + } else { + $basis =$self->find_next_basis_from_pivot($row_index,$col_index); + } + return( $basis->value, $feasible_point,$infeasible_lop ); + +} + =item find_short_cut_row ($index, $value, $feasible)=$self->find_short_cut_row @@ -729,18 +856,15 @@ sub find_leaving_column { sub find_short_cut_row { my $self = shift; -# my @b = map {$_->value} @{ $self->{b}->column(1)->data }; #get raw numbers -# main::DEBUG_MESSAGE("b is ", join(" ", @b )); - my ($row_dim, $col_dim) = $self->{b}->dimensions; -# main::DEBUG_MESSAGE($self->{b}->dimensions); -# main::DEBUG_MESSAGE("dimensions of b: $row_dim, $col_dim "); - my $col_index = 1; + Value::Error( "call find_short_cut_row as a Tableau method") unless ref($self)=~/Tableau/; + my ($row_dim, $col_dim) = $self->{current_b}->dimensions; + my $col_index = 1; # =$col_dim my $index = undef; my $value = undef; my $feasible = 1; for (my $k=1; $k<=$row_dim; $k++) { - my $b_k1 = $self->{current_b}->element($k,$col_index); - # main::DEBUG_MESSAGE("b[$k] = $b_k1"); + my $b_k1 = $self->current_b->element($k,$col_index); + #main::diag("b[$k] = $b_k1"); next if $b_k1>=0; #skip positive entries; $index =$k; $value = $b_k1; @@ -752,7 +876,7 @@ sub find_short_cut_row { =item find_short_cut_column - ($index, $value, $infeasible) = $self->find_short_cut_column(row number) + ($index, $value, $infeasible) = $self->find_short_cut_column(row_index) Find the left most negative entry in the specified row. If all coefficients are positive then the tableau represents an infeasible LOP, the $infeasible flag is set, @@ -762,6 +886,7 @@ sub find_short_cut_row { sub find_short_cut_column { my $self = shift; + Value::Error( "call find_short_cut_column as a Tableau method") unless ref($self)=~/Tableau/; my $row_index = shift; my ($row_dim,$col_dim) = $self->{M}->dimensions; $col_dim = $col_dim - 1; # omit constraint column @@ -785,38 +910,63 @@ sub find_short_cut_column { return( $index, $value, $infeasible); } -=item find_next_short_cut_pivot - ($row, $col, $feasible, $infeasible) = $self->find_next_short_cut_pivot - - -Following the short-cut algorithm this chooses the next pivot by choosing the row -with the most negative constraint constant entry (top most first in case of tie) and -then the left most negative entry in that row. -The process stops with either $feasible=1 (state variables give a feasible point for the -constraints) or $infeasible=1 (a row in the tableau shows that the LOP has empty domain.) - -=cut -=item find_next_short_cut_basis -=cut +=item tableau_pivot + + Tableau = $self->tableau_pivot(3,4) + MathObjectMatrix = $self->tableau_pivot(3,4)->current_tableau + +FIXME -- this needs to be written + +Pivot the tableau to a new basis at the given pivot point. +Maintain integer status if the original contains integers. + +Returns tableau object? + +=cut # eventually these routines should be included in the Value::Matrix # module? + +=pod + +These are generic matrix routines. Perhaps some or all of these should +be added to the file Value::Matrix? + +=cut + package Value::Matrix; sub _Matrix { Value::Matrix->new(@_); } +=item row_slice + + MathObjectMatrix = $self->row_slice(3,4) + MathObjectMatrix = $self->row_slice([3,4]) + +Similar to $self->extract_rows (or $self->rows) but returns a MathObjectmatrix + +=cut + sub row_slice { my $self = shift; @slice = @_; return _Matrix( $self->extract_rows(@slice) ); } + +=item extract_rows + + ARRAY reference = $self->extract_rows(@slice) + ARRAY reference = $self->extract_rows([@slice]) + +=cut + sub extract_rows { my $self = shift; my @slice = @_; @@ -831,6 +981,14 @@ sub column_slice { my $self = shift; return _Matrix( $self->extract_columns(@_) )->transpose; # matrix is built as rows then transposed. } + +=item extract_columns + + ARRAY reference = $self->extract_columns(@slice) + ARRAY reference = $self->extract_columns([@slice]) + +=cut + sub extract_columns { my $self = shift; my @slice = @_; @@ -844,15 +1002,41 @@ sub extract_columns { # if you pull columns directly you get an array of 1 by n column vectors. # prefer to pass references when possible } + +=item extract_rows_to_list + + MathObjectList = $self->extract_rows_to_list(@slice) + MathObjectList = $self->extract_rows_to_list([@slice]) + +=cut + sub extract_rows_to_list { my $self = shift; Value::List->new($self->extract_rows(@_)); } + +=item extract_columns_to_list + + ARRAY reference = $self->extract_columns_to_list(@slice) + ARRAY reference = $self->extract_columns_to_list([@slice]) + +=cut + sub extract_columns_to_list { my $self = shift; Value::List->new($self->extract_columns(@_) ); } +=item submatrix + + MathObjectMatrix = $self->submatrix([[1,2,3],[2,4,5]]) + +Extracts a submatrix from a Matrix and returns it as MathObjectMatrix. + +Indices for MathObjectMatrices start at 1. + +=cut + sub submatrix { my $self = shift; my %options = @_; @@ -862,6 +1046,84 @@ sub submatrix { return $self->row_slice($row_slice)->column_slice($col_slice); } +=item row_reduce + + $self->row_reduce(3,4) + +Row reduce matrix so that column 4 is a basis column. Used in +pivoting for simplex method + +=cut +sub row_reduce { + my $self = shift; + Value::Error( "call row_reduce as a Tableau method") unless ref($self)=~/Tableau/; + my ($row_index, $col_index, $basisCoeff); + # FIXME is $basisCoeff needed? isn't it always the same as $self->current_basis_coeff? + my @input = @_; + if (ref( $input[0]) =~/ARRAY/) { + ($row_index, $col_index) = @{$input[0]}; + } elsif (ref( $input[0]) =~/List|Set/){ + ($row_index, $col_index) = @{$input[0]->value}; + } else { # input is assumed to be an array + ($row_index, $col_index)=@input; + } + # calculate new basis + my $new_basis_columns = $self->find_next_basis_from_pivot($row_index,$col_index); + # form new basis + my $basis_matrix = $self->M->submatrix(rows=>[1..($self->m)],columns=>$self->$new_basis_columns); + my $basis_det = $basis_matrix->det; + if ($basis_det == 0 ){ + Value::Error("The columns ", join(",", @$new_basis_columns)." cannot form a basis"); + } + # updates + $self->basis_columns($new_basis_columns); + $self->current_basis_coeff($basis_det); + # this should always be an ARRAY + $basisCoeff=$basisCoeff || $self->{current_basis_coeff} || 1; + #basis_coeff should never be zero. + Value::Error( "need to specify the pivot point for row_reduction") unless $row_index && $col_index; + my $matrix = $self->current_constraint_matrix; + my $pivot_value = $matrix->entry($row_index,$col_index); + Value::Error( "pivot value cannot be zero") if $matrix->entry($row_index,$col_index)==0; + # make pivot value positive + if($pivot_value < 0) { + foreach my $j (1..$self->m) { + $matrix->entry($row_index, $j) *= -1; + } + } + # perform row reduction to clear out column $col_index + foreach my $i (1..$self->m){ + if ($i !=$row_index) { # skip pivot row + my $row_value_in_pivot_col = $matrix->entry($i,$col_index); + foreach my $j (1..$self->n){ + my $new_value = ( + ($pivot_value)*($matrix->entry($i,$j)) + -$row_value_in_pivot_col*($matrix->entry($row_index,$j)) + )/$basisCoeff; + $matrix->change_matrix_entry($i,$j, $new_value); + } + } + + } + $self->{basis_coeff} = $pivot_value; +} + +=item change_matrix_entry + + + +=cut +# This was written by Davide Cervone. +# http://webwork.maa.org/moodle/mod/forum/discuss.php?d=2970 +# taken from MatrixReduce.pl from Paul Pearson + +sub change_matrix_entry { + my $self = shift; my $index = shift; my $x = shift; + my $i = shift(@$index) - 1; + if (scalar(@$index)) {change_matrix_entry($self->{data}[$i],$index,$x);} + else {$self->{data}[$i] = Value::makeValue($x); + } +} 1; diff --git a/t/matrix_tableau_tests/test_tableau_more.pl b/t/matrix_tableau_tests/test_tableau_more.pl index 3a1873a312..c06e00903f 100755 --- a/t/matrix_tableau_tests/test_tableau_more.pl +++ b/t/matrix_tableau_tests/test_tableau_more.pl @@ -5,13 +5,42 @@ use lib "/Volumes/WW_test/opt/webwork/webwork2/lib"; use Test::More; +use Test::Exception; use Parser; use Value; +use Class::Accessor; +#use PGcore; + require "tableau.pl"; -require "Value.pl"; +require "Value.pl"; #gives us Real() etc. +#require "Parser.pl"; #gives us Context() but also uses loadMacros(); +require "niceTables.pl"; +sub Context {Parser::Context->current(\%context,@_)} +unless (%context && $context{current}) { + # ^variable our %context + %context = (); # Locally defined contexts, including 'current' context + # ^uses Context + Context(); # Initialize context (for persistent mod_perl) +} +sub WARN_MESSAGE{ + warn("WARN MESSAGE: ", @_); +} +Context("Matrix"); + +Context()->flags->set( + zeroLevel=>1E-5, + zeroLevelTol=>1E-5 + ); + + $A = Real(.0000005); + $B = Real(0); + + is($A, $B, "test zeroLevel tolerance"); + ok($A==$B, "test zeroLevel tolerance with ok"); + $money_total = 6000; $time_total = 600; @@ -33,37 +62,195 @@ $ra_matrix = [[-$bill_money_commitment,-$bill_time_commitment, -1, 0, 1,0,0,-$bill_profit], [-$steve_money_commitment,-$steve_time_commitment, 0, -1, 0,1,0,-$steve_profit], [-$money_total,-$time_total,0,0, 0,0, 1,0]]; - $A = Value::Matrix->new([[-$bill_money_commitment,-$bill_time_commitment, -1, 0], [ -$steve_money_commitment,-$steve_time_commitment, 0, -1 ]]); $b = Value::Vector->new([-$bill_profit,-$steve_profit]); # need vertical vector $c = Value::Vector->new([$money_total,$time_total,0,0]); $tableau1 = Tableau->new(A=>$A, b=>$b, c=>$c); +############################################################### +# Check mutators +# +# +############################################################### ok (1==1, "trivial first test"); ok (defined($tableau1), 'tableau has been defined and loaded'); +is (ref($tableau1), "Tableau", 'has type Tableau' ); is ($tableau1->{m}, 2, 'number of constraints is 2'); is ($tableau1->{n}, 4, 'number of variables is 4'); is_deeply ( [$tableau1->{m},$tableau1->{n}], [$tableau1->{A}->dimensions], '{m},{n} match dimensions of A'); is_deeply ($tableau1->{A}, $A, 'constraint matrix'); is_deeply ($tableau1->{b}, Matrix([$b])->transpose, 'constraint constants is m by 1 matrix'); is_deeply ($tableau1->{c}, $c, 'objective function constants'); +is_deeply ($tableau1->{A}, $tableau1->A, '{A} original constraint matrix accessor'); +is_deeply ($tableau1->{b}, $tableau1->b, '{b} orginal constraint constants accessor'); +is_deeply ($tableau1->{c}, $tableau1->c, '{c} original objective function constants accessor'); + my $test_constraint_matrix = Matrix($ra_matrix->[0],$ra_matrix->[1]); is_deeply ($tableau1->{current_constraint_matrix}, $test_constraint_matrix, 'initialization of current_constraint_matrix'); +is_deeply($tableau1->{current_constraint_matrix}, $tableau1->current_constraint_matrix, + 'current_constraint_matrix accessor'); is_deeply ($tableau1->{current_b}, $tableau1->{b}, 'initialization of current_b'); +is_deeply ($tableau1->{current_b}, $tableau1->current_b, + 'current_b accessor'); +is_deeply ([$tableau1->current_b->dimensions], [2,1], 'dimensions of current_b'); my $obj_row_test = [ ((-$c)->value, 0,0,1,0) ]; is_deeply ($tableau1->objective_row, $obj_row_test, 'initialization of $tableau->{obj_row}'); -is_deeply( ref($tableau1->objective_row), 'ARRAY', '->objective_row has type ARRAY'); -is_deeply( ref($tableau1->{obj_row}), 'Value::Matrix', '->{obj_row} has type Value::Matrix'); +is( ref($tableau1->{obj_row}), 'Value::Matrix', '->{obj_row} has type Value::Matrix'); +is(ref($tableau1->obj_row), 'Value::Matrix', '->obj_row has type Value::Matrix'); +is_deeply($tableau1->obj_row, $tableau1->{obj_row}, 'verify mutator for {obj_row}'); +is_deeply( ref($tableau1->objective_row), 'ARRAY', '->objective_row has type ARRAY'); is_deeply( $tableau1->objective_row, [$tableau1->{obj_row}->value], 'access to {obj_row}'); -is_deeply($tableau1->current_tableau, Matrix($ra_matrix), 'entire tableau including obj coeff row'); +is_deeply( $tableau1->objective_row, [$tableau1->obj_row->value], 'objective_row is obj_row->value = ARRAY'); + is(ref($tableau1->current_tableau), 'Value::Matrix', '-> current_tableau is Value::Matrix'); +is_deeply($tableau1->current_tableau, Matrix($ra_matrix), 'entire tableau including obj coeff row'); + +is(ref($tableau1->S), "Value::Matrix", 'slack variables are a Value::Matrix'); +is_deeply($tableau1->S, $tableau1->I($tableau1->m), 'slack variables are identity matrix'); + + +# test basis +is_deeply(ref($tableau1->basis_columns), "ARRAY", "{basis_column} has type ARRAY"); +is_deeply($tableau1->basis_columns, [5,6], "initialization of basis"); +is(ref($tableau1->current_basis_matrix), ref(Value::Matrix->I($tableau1->m) ), + "current_basis_matrix type is MathObjectMatrix"); +is_deeply($tableau1->current_basis_matrix, Value::Matrix->I($tableau1->m), + "initialization of basis"); + +# change basis and test again +$tableau1->basis(2,3); +is_deeply(ref($tableau1->basis_columns), "ARRAY", "{basis_column} has type ARRAY"); +is_deeply($tableau1->basis_columns, [2,3], " basis columns set to {2,3}"); +is(ref($tableau1->current_basis_matrix), ref( $test_constraint_matrix->column_slice(2,3) ), + "current_basis_matrix type is MathObjectMatrix"); +is_deeply($tableau1->current_basis_matrix, $test_constraint_matrix->column_slice(2,3), + "basis_matrix for columns {2,3} is correct" ); +is_deeply( $tableau1->basis(Set(2,3)), List([2,3]), "->basis(Set(2,3))" ); +is_deeply( $tableau1->basis(List(2,3)), List([2,3]), "->basis(List(2,3))" ); +is_deeply( $tableau1->basis([2,3]), List([2,3]), "->basis([2,3])" ); + +# find basis column index corresponding to row index (and value of the basis coefficient) + +$tableau1->basis(5,6); +diag("\nbasis is", $tableau1->basis(5,6)); +diag(print $tableau1->current_tableau,"\n"); +is_deeply([$tableau1->find_leaving_column(1)], [5,1], "find_leaving_column returns [col_index, pivot_value] " ); +is_deeply([$tableau1->find_leaving_column(2)], [6,1], "find_leaving_column returns [col_index, pivot_value] " ); + +is_deeply($tableau1->find_next_basis_from_pivot(1,2), Set(2,6), + "find next basis from pivot (1,2)"); +is_deeply($tableau1->find_next_basis_from_pivot(1,3), Set(3,6), + "find next basis from pivot (1,3)"); +is_deeply($tableau1->find_next_basis_from_pivot(2,1), Set(1,5), + "find next basis from pivot (2,1)"); + is_deeply($tableau1->find_next_basis_from_pivot(1,1), Set(1,6), + "find next basis from pivot (1,1)"); + +throws_ok(sub {$tableau1->find_next_basis_from_pivot(2,5)}, qr/pivot point should not be in a basis column/, + "can't pivot in basis column (2,5)"); # probably shouldn't be doing this. +throws_ok(sub {$tableau1->find_next_basis_from_pivot(1,6)}, qr/pivot point should not be in a basis column/, + "can't pivot in basis column (2,6)"); # probably shouldn't be doing this. +is_deeply($tableau1->find_next_basis_from_pivot(2,1), Set(1,5), + "find next basis from pivot (2,1)"); +throws_ok(sub {$tableau1->find_next_basis_from_pivot(2,6)}, qr/pivot point should not be in a basis column/, + "can't pivot in basis column (2,6)"); # probably shouldn't be doing this. + + +$tableau1->basis(2,3); +diag("\nbasis is", $tableau1->basis()); +diag(print $tableau1->current_tableau,"\n"); +is_deeply([$tableau1->find_leaving_column(1)], [2,500], "find_leaving_column returns [col_index, pivot_value] " ); +is_deeply([$tableau1->find_leaving_column(2)], [3,500], "find_leaving_column returns [col_index, pivot_value] " ); + +throws_ok(sub {$tableau1->find_next_basis_from_pivot(1,2)}, qr/pivot point should not be in a basis column/, + "can't pivot in basis column (1,2)"); # probably shouldn't be doing this either. +throws_ok(sub {$tableau1->find_next_basis_from_pivot(1,3)}, qr/pivot point should not be in a basis column.*/, + "can't pivot in basis column (1,3)"); # probably shouldn't be doing this. +is_deeply($tableau1->find_next_basis_from_pivot(2,1), Set(1,2), + "find next basis from pivot (2,1)"); + is_deeply($tableau1->find_next_basis_from_pivot(1,1), Set(1,3), + "find next basis from pivot (1,1)"); + +$tableau1->basis(5,6); +diag("\nbasis is ", $tableau1->basis()); +diag($tableau1->current_tableau,"\n"); +diag("find next short cut pivots"); +# ($row_index, $value, $feasible_point) = $self->find_short_cut_row() +is_deeply([$tableau1->find_short_cut_row()], [1,-4700,0], "row 1"); +is_deeply([$tableau1->find_short_cut_column(1)], [1,-5000,0], "column 1 "); +is_deeply([$tableau1->find_next_short_cut_pivot()], [1,1,0,0], "pivot (1,1)"); +is_deeply([$tableau1->find_next_short_cut_basis()],[1,6,0,0], "new basis {1,6} continue"); +$tableau1->current_tableau(1,6); +diag($tableau1->current_tableau); + +is_deeply([$tableau1->find_short_cut_row],[2,Value::Real->new(-8.4E+06),0], "find short cut row"); +is_deeply([$tableau1->find_short_cut_column(2)], [2,Value::Real->new(-1.3E+06),0], "find short cut col 2 "); +is_deeply([$tableau1->find_next_short_cut_pivot()], [2,2,0,0], "pivot (2,2)"); +is_deeply([$tableau1->find_next_short_cut_basis()],[1,2,0,0], "new basis {1,2} continue"); + +$tableau1->current_tableau(1,2); +diag($tableau1->current_tableau); + + + +is_deeply([$tableau1->find_next_short_cut_pivot()],[undef,undef,1,0], "feasible point found"); +is_deeply([$tableau1->find_next_short_cut_basis()],[1,2,1,0], + "all constraints positive at basis {1,2} --start phase2"); +is_deeply([$tableau1->find_pivot_column('max')], [5,Value::Real->new(-1.2E+06),0], "col 5"); +is_deeply([$tableau1->find_pivot_row(5)], [2,Value::Real->new(8.4E06/3000),0], "row 2 "); +is_deeply([$tableau1->find_next_pivot('max')], [2,5,0,0], "pivot (2,5)"); +is_deeply([$tableau1->find_next_basis('max')],[1,5,0,0], "new basis {1,5} continue"); + +$tableau1->current_tableau(1,5); +diag($tableau1->current_tableau); +is_deeply([$tableau1->find_pivot_column('max')], [6,Value::Real->new(-6000),0], "col 6"); +is_deeply([$tableau1->find_pivot_row(6)], [-1,undef,1], "unbounded "); + +is_deeply([$tableau1->find_next_pivot('max')], [-1,6,0,1], "unbounded"); +# this is ok -- we're looking at the dual of the bill and steve problem +# and the original test was to minimize it not to maximize it +# recheck the original problem with websim!!!! + +# regularize the output for row and column definitions if one of the flags is set. +# can we always set those to undefined? +# can we change the flag notification to +# "unbounded, feasible_point, infeasible_tableau, optimal"? +# it might be easier to remember. + +diag("reset tableau to feasible point and try to minimize it for phase2"); +$tableau1->current_tableau(1,2); +diag($tableau1->current_tableau); +is_deeply([$tableau1->find_next_short_cut_pivot()],[undef,undef,1,0], "feasible point found"); +is_deeply([$tableau1->find_next_short_cut_basis()],[1,2,1,0], + "all constraints positive at basis {1,2} --start phase2"); + +is_deeply([$tableau1->find_pivot_column('min')], [3,Value::Real->new(1.2E+06),0], "col 3"); +is_deeply([$tableau1->find_pivot_row(3)], [1,Value::Real->new(550000/500),0], "row 1 "); +is_deeply([$tableau1->find_next_pivot('min')], [1,3,0,0], "pivot (1,3)"); +is_deeply([$tableau1->find_next_basis('min')],[2,3,0,0], "new basis {2,3} continue"); + + + +$tableau1->current_tableau(2,3); +diag($tableau1->current_tableau); + +is_deeply([$tableau1->find_pivot_column('min')], [4,Value::Real->new(600),0], "col 4"); +is_deeply([$tableau1->find_pivot_row(4)], [1,Value::Real->new(4500),0], "row 1 "); +is_deeply([$tableau1->find_next_pivot('min')], [1,4,0,0], "pivot (1,4)"); +is_deeply([$tableau1->find_next_basis('min')],[3,4,0,0], "new basis {3,4} continue"); + +$tableau1->current_tableau(3,4); +diag($tableau1->current_tableau); + +is_deeply([$tableau1->find_pivot_column('min')], [-1,undef,1], "optimum"); +#is_deeply([$tableau1->find_pivot_row(4)], [1,Value::Real->new(4500),0], "row 1 "); +is_deeply([$tableau1->find_next_pivot('min')], ['',-1,1,undef], "pivot (1,4) optimum"); +is_deeply([$tableau1->find_next_basis('min')],[3,4,1,undef], " basis {3,4} optimum"); -# check accessors? Use antlers? - done_testing(); \ No newline at end of file From dad45b915a41712a9ae1fd9726251d27e536501b Mon Sep 17 00:00:00 2001 From: Michael Gage Date: Mon, 23 Oct 2017 21:15:30 -0400 Subject: [PATCH 18/30] Corrected the output of find_next_pivot() and find_next_short_cut_pivot output is ($basis->value, $flag) e.g ( 3 ,4, 5, 'optimum'). $flag can be 'optimal', 'unbounded', 'feasible_point', 'infeasible_lop' or undef. --- macros/tableau.pl | 45 ++++++++++++++++----- t/matrix_tableau_tests/test_tableau_more.pl | 16 ++++---- 2 files changed, 42 insertions(+), 19 deletions(-) diff --git a/macros/tableau.pl b/macros/tableau.pl index f8a88564a6..7d0dd51c29 100755 --- a/macros/tableau.pl +++ b/macros/tableau.pl @@ -558,13 +558,16 @@ sub basis { =item find_next_basis - ($row, $col,$optimum,$unbounded) = $self->find_next_basis (max/min, obj_row_number) + ($basis->value,$flag) = $self->find_next_basis (max/min, obj_row_number) In phase 2 of the simplex method calculates the next basis. $optimum or $unbounded is set if the process has found on the optimum value, or the column $col gives a certificate of unboundedness. +$flag can be either 'optimum' or 'unbounded' in which case the basis returned is the current basis. +$basis->value is an ARRAY of column numbers. + =cut @@ -575,17 +578,20 @@ sub find_next_basis { my $obj_row_number = shift; my ( $row_index, $col_index, $optimum, $unbounded)= $self->find_next_pivot($max_or_min, $obj_row_number); - my $flag; + my $flag = undef; my $basis; if ($optimum or $unbounded) { $basis=$self->basis(); + if ($optimum) { + $flag = 'optimum' + } elsif ($unbounded) { + $flag = 'unbounded'} } else { - $flag = ''; - $basis =$self->find_next_basis_from_pivot($row_index,$col_index); - + Value::Error("At least part of the pivot index (row,col) is not defined") unless + defined($row_index) and defined($col_index); + $basis =$self->find_next_basis_from_pivot($row_index,$col_index); } - return( $basis->value, $optimum,$unbounded ); - + return( $basis->value, $flag ); } =item find_next_pivot @@ -619,7 +625,7 @@ sub find_next_pivot { =item find_next_basis_from_pivot - List(basis) = $self->find_next_basis (pivot_row, pivot_column) + List(basis) = $self->find_next_basis_from_pivot (pivot_row, pivot_column) Calculate the next basis from the current basis given the pivot position. @@ -822,9 +828,18 @@ sub find_next_short_cut_pivot { =item find_next_short_cut_basis + ($basis->value, $flag) = $self->find_next_short_cut_basis() + +In phase 1 of the simplex method calculates the next basis for the short cut method. +$flag is set to 'feasible_point' if the basis and its corresponding tableau is associated with a basic feasible point +(a point on a corner of the domain of the LOP). The tableau is ready for phase 2 processing. +$flag is set to 'infeasible_lop' which means that the tableau has +a row which demonstrates that the LOP constraints are inconsistent and the domain is empty. +In these cases the basis returned is the current basis of the tableau object. + +Otherwise the $basis->value returned is the next basis that should be used in the short_cut method +and $flag contains undef. -FIXME -- this needs to be written ? -just find_next_basis_from_pivot should work? =cut @@ -835,12 +850,20 @@ sub find_next_short_cut_basis { my ( $row_index, $col_index, $feasible_point, $infeasible_lop)= $self->find_next_short_cut_pivot(); my $basis; + $flag = undef; if ($feasible_point or $infeasible_lop) { $basis=$self->basis(); + if ($feasible_point) { + $flag = 'feasible_point'; + } elsif ($infeasible_lop){ + $flag = 'infeasible_lop'; + } } else { + Value::Error("At least part of the pivot index (row,col) is not defined") unless + defined($row_index) and defined($col_index); $basis =$self->find_next_basis_from_pivot($row_index,$col_index); } - return( $basis->value, $feasible_point,$infeasible_lop ); + return( $basis->value, $flag ); } diff --git a/t/matrix_tableau_tests/test_tableau_more.pl b/t/matrix_tableau_tests/test_tableau_more.pl index c06e00903f..ebd630f9b6 100755 --- a/t/matrix_tableau_tests/test_tableau_more.pl +++ b/t/matrix_tableau_tests/test_tableau_more.pl @@ -185,14 +185,14 @@ sub WARN_MESSAGE{ is_deeply([$tableau1->find_short_cut_row()], [1,-4700,0], "row 1"); is_deeply([$tableau1->find_short_cut_column(1)], [1,-5000,0], "column 1 "); is_deeply([$tableau1->find_next_short_cut_pivot()], [1,1,0,0], "pivot (1,1)"); -is_deeply([$tableau1->find_next_short_cut_basis()],[1,6,0,0], "new basis {1,6} continue"); +is_deeply([$tableau1->find_next_short_cut_basis()],[1,6,undef], "new basis {1,6} continue"); $tableau1->current_tableau(1,6); diag($tableau1->current_tableau); is_deeply([$tableau1->find_short_cut_row],[2,Value::Real->new(-8.4E+06),0], "find short cut row"); is_deeply([$tableau1->find_short_cut_column(2)], [2,Value::Real->new(-1.3E+06),0], "find short cut col 2 "); is_deeply([$tableau1->find_next_short_cut_pivot()], [2,2,0,0], "pivot (2,2)"); -is_deeply([$tableau1->find_next_short_cut_basis()],[1,2,0,0], "new basis {1,2} continue"); +is_deeply([$tableau1->find_next_short_cut_basis()],[1,2,undef], "new basis {1,2} continue"); $tableau1->current_tableau(1,2); diag($tableau1->current_tableau); @@ -200,12 +200,12 @@ sub WARN_MESSAGE{ is_deeply([$tableau1->find_next_short_cut_pivot()],[undef,undef,1,0], "feasible point found"); -is_deeply([$tableau1->find_next_short_cut_basis()],[1,2,1,0], +is_deeply([$tableau1->find_next_short_cut_basis()],[1,2,'feasible_point'], "all constraints positive at basis {1,2} --start phase2"); is_deeply([$tableau1->find_pivot_column('max')], [5,Value::Real->new(-1.2E+06),0], "col 5"); is_deeply([$tableau1->find_pivot_row(5)], [2,Value::Real->new(8.4E06/3000),0], "row 2 "); is_deeply([$tableau1->find_next_pivot('max')], [2,5,0,0], "pivot (2,5)"); -is_deeply([$tableau1->find_next_basis('max')],[1,5,0,0], "new basis {1,5} continue"); +is_deeply([$tableau1->find_next_basis('max')],[1,5,undef], "new basis {1,5} continue"); $tableau1->current_tableau(1,5); diag($tableau1->current_tableau); @@ -227,13 +227,13 @@ sub WARN_MESSAGE{ $tableau1->current_tableau(1,2); diag($tableau1->current_tableau); is_deeply([$tableau1->find_next_short_cut_pivot()],[undef,undef,1,0], "feasible point found"); -is_deeply([$tableau1->find_next_short_cut_basis()],[1,2,1,0], +is_deeply([$tableau1->find_next_short_cut_basis()],[1,2,'feasible_point'], "all constraints positive at basis {1,2} --start phase2"); is_deeply([$tableau1->find_pivot_column('min')], [3,Value::Real->new(1.2E+06),0], "col 3"); is_deeply([$tableau1->find_pivot_row(3)], [1,Value::Real->new(550000/500),0], "row 1 "); is_deeply([$tableau1->find_next_pivot('min')], [1,3,0,0], "pivot (1,3)"); -is_deeply([$tableau1->find_next_basis('min')],[2,3,0,0], "new basis {2,3} continue"); +is_deeply([$tableau1->find_next_basis('min')],[2,3,undef], "new basis {2,3} continue"); @@ -243,7 +243,7 @@ sub WARN_MESSAGE{ is_deeply([$tableau1->find_pivot_column('min')], [4,Value::Real->new(600),0], "col 4"); is_deeply([$tableau1->find_pivot_row(4)], [1,Value::Real->new(4500),0], "row 1 "); is_deeply([$tableau1->find_next_pivot('min')], [1,4,0,0], "pivot (1,4)"); -is_deeply([$tableau1->find_next_basis('min')],[3,4,0,0], "new basis {3,4} continue"); +is_deeply([$tableau1->find_next_basis('min')],[3,4,undef], "new basis {3,4} continue"); $tableau1->current_tableau(3,4); diag($tableau1->current_tableau); @@ -251,6 +251,6 @@ sub WARN_MESSAGE{ is_deeply([$tableau1->find_pivot_column('min')], [-1,undef,1], "optimum"); #is_deeply([$tableau1->find_pivot_row(4)], [1,Value::Real->new(4500),0], "row 1 "); is_deeply([$tableau1->find_next_pivot('min')], ['',-1,1,undef], "pivot (1,4) optimum"); -is_deeply([$tableau1->find_next_basis('min')],[3,4,1,undef], " basis {3,4} optimum"); +is_deeply([$tableau1->find_next_basis('min')],[3,4,'optimum'], " basis {3,4} optimum"); done_testing(); \ No newline at end of file From 32233a14efb4d65c3b8d6d7cd13cbe2268ffa7d3 Mon Sep 17 00:00:00 2001 From: Michael Gage Date: Tue, 24 Oct 2017 10:30:20 -0400 Subject: [PATCH 19/30] Reorganize POD documentation --- macros/tableau.pl | 378 +++++++++----------- t/matrix_tableau_tests/test_tableau_more.pl | 13 +- 2 files changed, 189 insertions(+), 202 deletions(-) diff --git a/macros/tableau.pl b/macros/tableau.pl index 7d0dd51c29..1df59fa23d 100755 --- a/macros/tableau.pl +++ b/macros/tableau.pl @@ -28,7 +28,7 @@ =head2 DESCRIPTION # uniquely from the parameter variables. # The non-basis (parameter) variables are set to zero. # - # state variables (assuming parameter variables are zero or when given parameter variables) + # state variables (assuming parameter variables are zero or when given parameter variables) # create the methods for updating the various containers # create the method for printing the tableau with all its decorations # possibly with switches to turn the decorations on and off. @@ -104,6 +104,26 @@ =head2 Package main =cut +=item lop_display + + Useage: + + lop_display($tableau, align=>'cccc|cc|c|c', toplevel=>[qw(x1,x2,x3,x4,s1,s2,P,b)]) + +Pretty prints the output of a matrix as a LOP with separating labels and +variable labels. + +=cut + +=head3 References: + +MathObject Matrix methods: L +MathObject Contexts: L +CPAN RealMatrix docs: L + +More references: L + +=cut =head2 Package tableau @@ -134,7 +154,8 @@ =head2 Package tableau # current_basis_matrix # (should be new name for B above # # a square invertible matrix corresponding to the # # current basis columns) - # flag indicating the column (1 or n+m+1) for the objective value + + # flag indicating the column (1 or n+m+1) for the objective value constraint_labels => undef, (not sure if this remains relevant after pivots) problem_var_labels => undef, slack_var_labels => undef, @@ -142,106 +163,6 @@ =head2 Package tableau =cut -=item current_tableau - - $self->current_tableau - Parameters: () - Returns: A MathObjectMatrix - -This represents the current version of the tableau - -=cut - -=item objective_row - - $self->objective_row - Parameters: () - Returns: - -=cut - -=item basis_columns - - ARRAY reference = $self->basis_columns() - [3,4] = $self->basis_columns([3,4]) - - Sets or returns the basis_columns as an ARRAY reference - -=cut - -=item basis - - $self->basis - Parameter: ARRAY or ARRAY_ref or () - Returns: MathObject_list - - FiXME -- this should accept a MathObject_List (or MO_Set?) - -=cut - -=head3 Package Tableau (eventually package Matrix?) - -=item row_slice - - $self->row_slice - - Parameter: @slice or \@slice - Return: MathObject matrix - -=cut - -=item extract_rows - - $self->extract_rows - - Parameter: @slice or \@slice - Return: two dimensional array ref - -=item extract_rows_to_list - - Parameter: @slice or \@slice - Return: MathObject List of row references - -=item extract_columns - - $self->extract_columns - - Parameter: @slice or \@slice - Return: two dimensional array ref - -=item column_slice - - $self->column_slice - - Parameter: @slice or \@slice - Return: MathObject Matrix - -=item extract_columns_to_list - - $self->extract_columns_to_list - - Parameter: @slice or \@slice - Return: MathObject List of Matrix references ? - -=item submatrix - - $self->submatrix - - Parameter:(rows=>\@row_slice,columns=>\@column_slice) - Return: MathObject matrix - -=cut - - -=head3 References: - -MathObject Matrix methods: L -MathObject Contexts: L -CPAN RealMatrix docs: L - -More references: L - -=cut sub _tableau_init {}; # don't reload this file @@ -249,16 +170,11 @@ =head3 References: # loadMacros("tableau_main_subroutines.pl"); -=head4 Subroutines added to the main:: Package -=cut +# ANS( $tableau->cmp(checker=>tableauEquivalence()) ); -=item tableauEquivalence - $tableau->cmp(checker=>tableauEquivalence()) - -=cut sub tableauEquivalence { return sub { @@ -281,16 +197,10 @@ sub tableauEquivalence { } } -=item linebreak_at_commas - - Useage: - $foochecker = $constraints->cmp()->withPostFilter( - linebreak_at_commas() - ); - -=cut - +# $foochecker = $constraints->cmp()->withPostFilter( +# linebreak_at_commas() +# ); sub linebreak_at_commas { return sub { @@ -307,16 +217,10 @@ sub linebreak_at_commas { }; } -=item linebreak_at_commas - Useage: - - lop_display($tableau, align=>'cccc|cc|c|c', toplevel=>[qw(x1,x2,x3,x4,s1,s2,P,b)]) - -Pretty prints the output of a matrix as a LOP with separating labels and -variable labels. - -=cut +# lop_display($tableau, align=>'cccc|cc|c|c', toplevel=>[qw(x1,x2,x3,x4,s1,s2,P,b)]) +# Pretty prints the output of a matrix as a LOP with separating labels and +# variable labels. sub lop_display { my $tableau = shift; @@ -354,6 +258,8 @@ sub _Matrix { # can we just import this? Value::Matrix->new(@_); } +# tableau constructor Tableau->new(A=>Matrix, b=>Vector or Matrix, c=>Vector or Matrix) + sub new { my $self = shift; my $class = ref($self) || $self; my $context = (Value::isContext($_[0]) ? shift : $self->context); @@ -443,6 +349,28 @@ sub assemble_matrix { # is not in this part of the matrix } +=head2 Accessors and mutators + +=item basis_columns + + ARRAY reference = $self->basis_columns() + [3,4] = $self->basis_columns([3,4]) + + Sets or returns the basis_columns as an ARRAY reference + +=cut + + +=item objective_row + + $self->objective_row + Parameters: () + Returns: + +=cut + + + sub objective_row { my $self = shift; # sanity check for objective row @@ -457,6 +385,10 @@ sub objective_row { =item current_tableau + $self->current_tableau + Parameters: () or (list) + Returns: A MathObjectMatrix + Useage: $MathObjectmatrix = $self->current_tableau $MathObjectmatrix = $self->current_tableau(3,4) #updates basis to (3,4) @@ -465,6 +397,11 @@ sub objective_row { including the constraint constants, problem variable coefficients, slack variable coefficients AND the row containing the objective function coefficients. + ------------- + |A | S |0| b| + ------------- + | c |z|z*| + ------------- If a list of basis columns is passed as an argument then $self->basis() is called to switch the tableau to the new basis before returning @@ -485,16 +422,17 @@ sub current_tableau { =item basis - ListObjectList = $self->basis - ListObjectList = $self->basis(3,4) - ListObjectList = $self->basis([3,4]) - ListObjectList = $self->basis(Set(3,4)) + MathObjectList = $self->basis + MathObjectList = $self->basis(3,4) + MathObjectList = $self->basis([3,4]) + MathObjectList = $self->basis(Set(3,4)) + MathObjectList = $self->basis(List(3,4)) to obtain ARRAY reference use [3,4]== $self->basis(Set3,4)->value Returns a MathObjectList containing the current basis columns. If basis columns -are provided as arguments it resets all elements of the tableau to present +are provided as arguments it updates all elements of the tableau to present the view corresponding to the new choice of basis columns. =cut @@ -938,20 +876,72 @@ sub find_short_cut_column { -=item tableau_pivot - - Tableau = $self->tableau_pivot(3,4) - MathObjectMatrix = $self->tableau_pivot(3,4)->current_tableau +=item row_reduce -FIXME -- this needs to be written +(or tableau pivot???) -Pivot the tableau to a new basis at the given pivot point. -Maintain integer status if the original contains integers. + Tableau = $self->row_reduce(3,4) + MathObjectMatrix = $self->row_reduce(3,4)->current_tableau -Returns tableau object? + +Row reduce matrix so that column 4 is a basis column. Used in +pivoting for simplex method. Returns tableau object. =cut - +sub row_reduce { + my $self = shift; + Value::Error( "call row_reduce as a Tableau method") unless ref($self)=~/Tableau/; + my ($row_index, $col_index, $basisCoeff); + # FIXME is $basisCoeff needed? isn't it always the same as $self->current_basis_coeff? + my @input = @_; + if (ref( $input[0]) =~/ARRAY/) { + ($row_index, $col_index) = @{$input[0]}; + } elsif (ref( $input[0]) =~/List|Set/){ + ($row_index, $col_index) = @{$input[0]->value}; + } else { # input is assumed to be an array + ($row_index, $col_index)=@input; + } + # calculate new basis + my $new_basis_columns = $self->find_next_basis_from_pivot($row_index,$col_index); + # form new basis + my $basis_matrix = $self->M->submatrix(rows=>[1..($self->m)],columns=>$self->$new_basis_columns); + my $basis_det = $basis_matrix->det; + if ($basis_det == 0 ){ + Value::Error("The columns ", join(",", @$new_basis_columns)." cannot form a basis"); + } + # updates + $self->basis_columns($new_basis_columns); + $self->current_basis_coeff($basis_det); + # this should always be an ARRAY + $basisCoeff=$basisCoeff || $self->{current_basis_coeff} || 1; + #basis_coeff should never be zero. + Value::Error( "need to specify the pivot point for row_reduction") unless $row_index && $col_index; + my $matrix = $self->current_constraint_matrix; + my $pivot_value = $matrix->entry($row_index,$col_index); + Value::Error( "pivot value cannot be zero") if $matrix->entry($row_index,$col_index)==0; + # make pivot value positive + if($pivot_value < 0) { + foreach my $j (1..$self->m) { + $matrix->entry($row_index, $j) *= -1; + } + } + # perform row reduction to clear out column $col_index + foreach my $i (1..$self->m){ + if ($i !=$row_index) { # skip pivot row + my $row_value_in_pivot_col = $matrix->entry($i,$col_index); + foreach my $j (1..$self->n){ + my $new_value = ( + ($pivot_value)*($matrix->entry($i,$j)) + -$row_value_in_pivot_col*($matrix->entry($row_index,$j)) + )/$basisCoeff; + $matrix->change_matrix_entry($i,$j, $new_value); + } + } + + } + $self->{basis_coeff} = $pivot_value; + return $self; +} # eventually these routines should be included in the Value::Matrix # module? @@ -970,6 +960,11 @@ sub _Matrix { =item row_slice + $self->row_slice + + Parameter: @slice or \@slice + Return: MathObject matrix + MathObjectMatrix = $self->row_slice(3,4) MathObjectMatrix = $self->row_slice([3,4]) @@ -985,6 +980,11 @@ sub row_slice { =item extract_rows + $self->extract_rows + + Parameter: @slice or \@slice + Return: two dimensional array ref + ARRAY reference = $self->extract_rows(@slice) ARRAY reference = $self->extract_rows([@slice]) @@ -1000,6 +1000,19 @@ sub extract_rows { } return [map {$self->row($_)} @slice ]; #prefer to pass references when possible } + +=item column_slice + + $self->column_slice() + + Parameter: @slice or \@slice + Return: two dimensional array ref + + ARRAY reference = $self->extract_rows(@slice) + ARRAY reference = $self->extract_rows([@slice]) + +=cut + sub column_slice { my $self = shift; return _Matrix( $self->extract_columns(@_) )->transpose; # matrix is built as rows then transposed. @@ -1007,6 +1020,11 @@ sub column_slice { =item extract_columns + $self->extract_columns + + Parameter: @slice or \@slice + Return: two dimensional array ref + ARRAY reference = $self->extract_columns(@slice) ARRAY reference = $self->extract_columns([@slice]) @@ -1028,6 +1046,9 @@ sub extract_columns { =item extract_rows_to_list + Parameter: @slice or \@slice + Return: MathObject List of row references + MathObjectList = $self->extract_rows_to_list(@slice) MathObjectList = $self->extract_rows_to_list([@slice]) @@ -1040,6 +1061,11 @@ sub extract_rows_to_list { =item extract_columns_to_list + $self->extract_columns_to_list + + Parameter: @slice or \@slice + Return: MathObject List of Matrix references ? + ARRAY reference = $self->extract_columns_to_list(@slice) ARRAY reference = $self->extract_columns_to_list([@slice]) @@ -1052,6 +1078,11 @@ sub extract_columns_to_list { =item submatrix + $self->submatrix + + Parameter:(rows=>\@row_slice,columns=>\@column_slice) + Return: MathObject matrix + MathObjectMatrix = $self->submatrix([[1,2,3],[2,4,5]]) Extracts a submatrix from a Matrix and returns it as MathObjectMatrix. @@ -1069,73 +1100,18 @@ sub submatrix { return $self->row_slice($row_slice)->column_slice($col_slice); } -=item row_reduce - $self->row_reduce(3,4) - -Row reduce matrix so that column 4 is a basis column. Used in -pivoting for simplex method - -=cut -sub row_reduce { - my $self = shift; - Value::Error( "call row_reduce as a Tableau method") unless ref($self)=~/Tableau/; - my ($row_index, $col_index, $basisCoeff); - # FIXME is $basisCoeff needed? isn't it always the same as $self->current_basis_coeff? - my @input = @_; - if (ref( $input[0]) =~/ARRAY/) { - ($row_index, $col_index) = @{$input[0]}; - } elsif (ref( $input[0]) =~/List|Set/){ - ($row_index, $col_index) = @{$input[0]->value}; - } else { # input is assumed to be an array - ($row_index, $col_index)=@input; - } - # calculate new basis - my $new_basis_columns = $self->find_next_basis_from_pivot($row_index,$col_index); - # form new basis - my $basis_matrix = $self->M->submatrix(rows=>[1..($self->m)],columns=>$self->$new_basis_columns); - my $basis_det = $basis_matrix->det; - if ($basis_det == 0 ){ - Value::Error("The columns ", join(",", @$new_basis_columns)." cannot form a basis"); - } - # updates - $self->basis_columns($new_basis_columns); - $self->current_basis_coeff($basis_det); - # this should always be an ARRAY - $basisCoeff=$basisCoeff || $self->{current_basis_coeff} || 1; - #basis_coeff should never be zero. - Value::Error( "need to specify the pivot point for row_reduction") unless $row_index && $col_index; - my $matrix = $self->current_constraint_matrix; - my $pivot_value = $matrix->entry($row_index,$col_index); - Value::Error( "pivot value cannot be zero") if $matrix->entry($row_index,$col_index)==0; - # make pivot value positive - if($pivot_value < 0) { - foreach my $j (1..$self->m) { - $matrix->entry($row_index, $j) *= -1; - } - } - # perform row reduction to clear out column $col_index - foreach my $i (1..$self->m){ - if ($i !=$row_index) { # skip pivot row - my $row_value_in_pivot_col = $matrix->entry($i,$col_index); - foreach my $j (1..$self->n){ - my $new_value = ( - ($pivot_value)*($matrix->entry($i,$j)) - -$row_value_in_pivot_col*($matrix->entry($row_index,$j)) - )/$basisCoeff; - $matrix->change_matrix_entry($i,$j, $new_value); - } - } - - } - $self->{basis_coeff} = $pivot_value; -} =item change_matrix_entry + $Matrix->change_matrix_entry([i,j,k],$value) - + Taken from MatrixReduce.pl. Written by Davide Cervone. + + perhaps "assign" would be a better name for this? + =cut + # This was written by Davide Cervone. # http://webwork.maa.org/moodle/mod/forum/discuss.php?d=2970 # taken from MatrixReduce.pl from Paul Pearson diff --git a/t/matrix_tableau_tests/test_tableau_more.pl b/t/matrix_tableau_tests/test_tableau_more.pl index ebd630f9b6..1c6a1464c0 100755 --- a/t/matrix_tableau_tests/test_tableau_more.pl +++ b/t/matrix_tableau_tests/test_tableau_more.pl @@ -9,13 +9,19 @@ use Parser; use Value; use Class::Accessor; -#use PGcore; +use PGcore; +#require "PG.pl"; +#require "PGbasicmacros.pl"; +#require "PGauxiliaryFunctions.pl"; require "tableau.pl"; require "Value.pl"; #gives us Real() etc. #require "Parser.pl"; #gives us Context() but also uses loadMacros(); require "niceTables.pl"; +ok(max(3,4)==4, "check max function"); + + sub Context {Parser::Context->current(\%context,@_)} unless (%context && $context{current}) { # ^variable our %context @@ -77,6 +83,11 @@ sub WARN_MESSAGE{ ok (1==1, "trivial first test"); ok (defined($tableau1), 'tableau has been defined and loaded'); is (ref($tableau1), "Tableau", 'has type Tableau' ); + +print "stringify tableau: ", $tableau1, "\n"; + +print lop_display($tableau1); + is ($tableau1->{m}, 2, 'number of constraints is 2'); is ($tableau1->{n}, 4, 'number of variables is 4'); is_deeply ( [$tableau1->{m},$tableau1->{n}], [$tableau1->{A}->dimensions], '{m},{n} match dimensions of A'); From 58b75de0222bf3c0cf4551162f08b4c2afb92775 Mon Sep 17 00:00:00 2001 From: Michael Gage Date: Thu, 2 Nov 2017 10:44:06 -0400 Subject: [PATCH 20/30] Add dual_lop -- to create a dual of the current tableau 1. fix typo in commenting out a line in IO.pm 2. update some of the unit test files --- lib/WeBWorK/PG/IO.pm | 2 +- macros/tableau.pl | 168 +++++++++++++++--- t/matrix_tableau_tests/matrix_test2.pg | 4 +- t/matrix_tableau_tests/print_tableau_Test.pg | 3 +- .../tableau_module_test1.pg | 21 ++- t/matrix_tableau_tests/test_tableau_more.pl | 81 +++++---- 6 files changed, 203 insertions(+), 76 deletions(-) diff --git a/lib/WeBWorK/PG/IO.pm b/lib/WeBWorK/PG/IO.pm index 8eb8c018c4..7801727a1d 100644 --- a/lib/WeBWorK/PG/IO.pm +++ b/lib/WeBWorK/PG/IO.pm @@ -261,7 +261,7 @@ sub path_is_course_subdir { sub query_sage_server { my ($python, $url, $accepted_tos, $setSeed, $webworkfunc, $debug, $curlCommand)=@_; # my $sagecall = qq{$curlCommand -i -k -sS -L --http1.1 --data-urlencode "accepted_tos=${accepted_tos}"}. - qq{ --data-urlencode 'user_expressions={"WEBWORK":"_webwork_safe_json(WEBWORK)"}' --data-urlencode "code=${setSeed}${webworkfunc}$python" $url}; +# qq{ --data-urlencode 'user_expressions={"WEBWORK":"_webwork_safe_json(WEBWORK)"}' --data-urlencode "code=${setSeed}${webworkfunc}$python" $url}; my $sagecall = qq{$curlCommand -i -k -sS -L --data-urlencode "accepted_tos=${accepted_tos}"}. qq{ --data-urlencode 'user_expressions={"WEBWORK":"_webwork_safe_json(WEBWORK)"}' --data-urlencode "code=${setSeed}${webworkfunc}$python" $url}; diff --git a/macros/tableau.pl b/macros/tableau.pl index 1df59fa23d..6722b289bf 100755 --- a/macros/tableau.pl +++ b/macros/tableau.pl @@ -45,11 +45,11 @@ =head2 DESCRIPTION ---------------------------------------------- Matrix A, the constraint matrix is n=num_problem_vars by m=num_slack_vars Matrix S, the slack variables is m by m - Matrix b, the constraint constants is n by 1 - Matrix c, the objective function coefficients matrix is 1 by n or 2 by n + Matrix b, the constraint constants is n by 1 (Matrix or ColumnVector) + Matrix c, the objective function coefficients matrix is 1 by n The next to the last column holds z or objective value z(...x^i...) = c_i* x^i (Einstein summation convention) - FIXME: allow c to be a 2 by n matrix so that you can do phase1 calculations easily + FIXME: ?? allow c to be a 2 by n matrix so that you can do phase1 calculations easily =cut @@ -134,47 +134,47 @@ =head2 Package tableau Tableau->new(A=>Matrix, b=>Vector or Matrix, c=>Vector or Matrix) A => undef, # original constraint matrix MathObjectMatrix - b => undef, # constraint constants Vector or MathObjectMatrix 1 by n + b => undef, # constraint constants ColumnVector or MathObjectMatrix 1 by n c => undef, # coefficients for objective function Vector or MathObjectMatrix 1 by n obj_row => undef, # contains the negative of the coefficients of the objective function. z => undef, # value for objective function n => undef, # dimension of problem variables (columns in A) m => undef, # dimension of slack variables (rows in A) S => undef, # square m by m matrix for slack variables - M => undef, # matrix of consisting of all original columns and all - rows except for the objective function row - obj_col_num => undef, - basis => undef, # list describing the current basis columns corresponding + M => undef, # matrix (m by m+n+1+1) consisting of all original columns and all + rows except for the objective function row. The m+n+1 column and + is the objective_value column. It is all zero with a pivot in the objective row. + + obj_col_num => undef, # have obj_col on the left or on the right? + basis => List, # list describing the current basis columns corresponding to determined variables. current_basis_matrix => undef, # square invertible matrix corresponding to the current basis columns - current_constraint_matrix=>undef, # the current version of [A | S] - current_b, # the current version of the constraint constants b - # current_basis_matrix # (should be new name for B above - # # a square invertible matrix corresponding to the - # # current basis columns) + current_constraint_matrix=>(m by n matrix), # the current version of [A | S] + current_b=> (1 by m matrix or Column vector) # the current version of the constraint constants b + current_basis_matrix => (m by m invertible matrix) a square invertible matrix + # corresponding to the current basis columns # flag indicating the column (1 or n+m+1) for the objective value constraint_labels => undef, (not sure if this remains relevant after pivots) problem_var_labels => undef, slack_var_labels => undef, - + Notes: (1 by m MathObjectMatrix) <= Value::Matrix->new($self->b->transpose->value ) =cut -sub _tableau_init {}; # don't reload this file - -# loadMacros("tableau_main_subroutines.pl"); - - # ANS( $tableau->cmp(checker=>tableauEquivalence()) ); +package main; +sub _tableau_init {}; # don't reload this file + +# loadMacros("tableau_main_subroutines.pl"); sub tableauEquivalence { return sub { @@ -202,6 +202,7 @@ sub tableauEquivalence { # linebreak_at_commas() # ); +package main; sub linebreak_at_commas { return sub { my $ans=shift; @@ -238,6 +239,47 @@ sub lop_display { DataTable([[@toplevel],@matrix],align=>$options{alignment}); } +sub get_tableau_variable_values { + my $mat = shift; # a MathObject matrix + my $basis =shift; # a MathObject set + # FIXME + # type check ref($mat)='Matrix'; ref($basis)='Set'; + # or check that $mat has dimensions, element methods; and $basis has a contains method + my ($n, $m) = $mat->dimensions; + @var = (); + #DEBUG_MESSAGE( "start new matrix"); + foreach my $j (1..$m-2) { # the last two columns of the tableau are object variable and constants + if (not $basis->contains($j)) { + #DEBUG_MESSAGE( "j= $j not in basis"); + $var[$j-1]=0; next; # non-basis variables (parameters) are set to 0. + + } else { + foreach my $i (1..$n-1) { # the last row is the objective function + # if this is a basis column there should be only one non-zero element(the pivot) + if ( not $mat->element($i,$j) == 0 ) { # should this have ->value????? + $var[$j-1] = ($mat->element($i,$m)/$mat->element($i,$j))->value; + #DEBUG_MESSAGE("i=$i j=$j var = $var[$j-1] "); + next; + } + + } + } + } # element($n, $m-1) is the coefficient of the objective value. + # this last variable is the value of the objective function + push @var , ($mat->element($n,$m)/$mat->element($n,$m-1))->value; + + @var; +} +#### Test -- assume matrix is this +# 1 2 1 0 0 | 0 | 3 +# 4 5 0 1 0 | 0 | 6 +# 7 8 0 0 1 | 0 | 9 +# -1 -2 0 0 0 | 1 | 10 # objective row +# and basis is {3,4,5} (start columns with 1) +# $n= 4; $m = 7 +# $x1=0; $x2=0; $x3=s1=3; $x4=s2=6; $x5=s3=9; w=10=objective value +# +# ################################################## package Tableau; @@ -298,7 +340,9 @@ sub initialize { ref($self->{b}) =~ /Value::Vector|Value::Matrix/ && ref($self->{c}) =~ /Value::Vector|Value::Matrix/){ Value::Error ("Error: Required inputs for creating tableau:\n". - "Tableau(A=> Matrix, b=>ColumnVector or Matrix, c=>Vector or Matrix)"); + "Tableau(A=> Matrix, b=>ColumnVector or Matrix, c=>Vector or Matrix)". + "not the arguments of type ". ref($self->{A}). " |".ref($self->{b})."| |".ref($self->{c}). + "|"); } my ($m, $n)=($self->{A}->dimensions); $self->n( ($self->n) //$n ); @@ -307,8 +351,9 @@ sub initialize { $self->{basis_columns} = [($n+1)...($n+$m)] unless ref($self->{basis_columns})=~/ARRAY/; my @rows = $self->assemble_matrix; $self->M( _Matrix([@rows]) ); #original matrix - $self->{data}= $self->M->data; - $self->{obj_row} = _Matrix(@{$self->objective_row()}); + $self->{data}= $self->M->data; + my $new_obj_row = $self->objective_row; + $self->{obj_row} = _Matrix($self->objective_row); # update everything else: # current_basis_matrix, current_constraint_matrix,current_b $self->basis($self->basis->value); @@ -374,10 +419,10 @@ =head2 Accessors and mutators sub objective_row { my $self = shift; # sanity check for objective row - - + Value::Error("The objective row coefficients (c) should be a 1 by n Matrix or a Vector of length n") + unless $self->n == $self->c->length; my @last_row=(); - push @last_row, ( -($self->{c}) )->value; # add the negative coefficients of the obj function + push @last_row, ( -($self->c) )->value; # add the negative coefficients of the obj function foreach my $i (1..($self->m)) { push @last_row, 0 }; # add 0s for the slack variables push @last_row, 1, 0; # add the 1 for the objective value and 0 for the initial value return \@last_row; @@ -420,6 +465,50 @@ sub current_tableau { $self->current_objective_coeffs ); } + +=item statevars + + + + + +=cut + +sub statevars { + my $self = shift; + my $matrix = $self->current_tableau; + my $basis =Value::Set->new($self->basis); # a MathObject set + # FIXME + # type check ref($mat)='Matrix'; ref($basis)='Set'; + # or check that $mat has dimensions, element methods; and $basis has a contains method + my ($m,$n) = $matrix->dimensions; + + @var = (); + #print( "start new matrix $m $n \n"); + #print "tableau is ", $matrix, "\n"; + foreach my $j (1..$n-2) { # the last two columns of the tableau are object variable and constants + if (not $basis->contains($j)) { + #DEBUG_MESSAGE( "j= $j not in basis"); + $var[$j-1]=0; next; # non-basis variables (parameters) are set to 0. + + } else { + foreach my $i (1..$m-1) { # the last row is the objective function + # if this is a basis column there should be only one non-zero element(the pivot) + if ( not $matrix->element($i,$j) == 0 ) { # should this have ->value????? + $var[$j-1] = ($matrix->element($i,$n)/$matrix->element($i,$j))->value; + #DEBUG_MESSAGE("i=$i j=$j var = $var[$j-1] "); + next; + } + + } + } + } # element($n, $m-1) is the coefficient of the objective value. + # this last variable is the value of the objective function + push @var , ($matrix->element($m,$n)/$matrix->element($m,$n-1))->value; + + [@var]; +} + =item basis MathObjectList = $self->basis @@ -474,7 +563,7 @@ sub basis { my $M = $self->{M}; my ($row_dim, $col_dim) = $M->dimensions; my $current_constraint_matrix = $Badj*$M; - my $c_B = $self->obj_row->extract_columns($self->basis_columns ); + my $c_B = $self->{obj_row}->extract_columns($self->basis_columns ); my $c_B2 = Value::Vector->new([ map {$_->value} @$c_B]); my $correction_coeff = ($c_B2*($current_constraint_matrix) )->row(1); my $obj_row_normalized = abs($self->{current_basis_matrix}->det->value)*$self->{obj_row}; @@ -550,7 +639,7 @@ sub find_next_pivot { my $obj_row_number =shift; # sanity check max or min in find pivot column - my ($col_index, $value, $row_index, $optimum, $unbounded) = ('','','',''); + my ($row_index, $col_index, $value, $optimum, $unbounded) = (undef,undef,undef, 0, 0); ($col_index, $value, $optimum) = $self->find_pivot_column($max_or_min, $obj_row_number); # main::DEBUG_MESSAGE("find_next_pivot: col: $col_index, value: $value opt: $optimum "); return ( $row_index, $col_index, $optimum, $unbounded) if $optimum; @@ -629,7 +718,7 @@ sub find_pivot_column { my @obj_row = @{$obj_row_matrix->extract_rows($obj_row_index)}; - my $index = -1; + my $index = undef; my $optimum = 1; my $value = undef; my $zeroLevelTol = $zeroLevelFraction * ($self->current_basis_coeff); @@ -674,7 +763,7 @@ sub find_pivot_row { } # main::DEBUG_MESSAGE("dim = ($row_dim, $col_dim)"); my $value = undef; - my $index = -1; + my $index = undef; my $unbounded = 1; my $zeroLevelTol = $zeroLevelFraction * ($self->current_basis_coeff); for (my $k=1; $k<=$row_dim; $k++) { @@ -945,6 +1034,27 @@ sub row_reduce { # eventually these routines should be included in the Value::Matrix # module? + +=item dual_problem + + TableauObject = $self->dual_lop + +Creates the tableau of the LOP which is dual to the linear optimization problem represented by the +current tableau. + +=cut + +sub dual_lop { + my $self = shift; + my $newA = $self->A->transpose; # converts m by n matrix to n by m matrix + my $newb = $self->c; # gives a 1 by n matrix + $newb = $newb->transpose; # converts to an n by 1 matrix + my $newc = $self->b; # gives an m by 1 matrix + $newc = _Matrix( $newc->transpose->value ); # convert to a 1 by m matrix + my $newt = Tableau->new(A=>$newA, b=>$newb, c=>$newc); + $newt; +} + =pod These are generic matrix routines. Perhaps some or all of these should diff --git a/t/matrix_tableau_tests/matrix_test2.pg b/t/matrix_tableau_tests/matrix_test2.pg index b4822b4f02..6ed30775ac 100644 --- a/t/matrix_tableau_tests/matrix_test2.pg +++ b/t/matrix_tableau_tests/matrix_test2.pg @@ -49,13 +49,13 @@ matrix [`[$m]`] vector [`[$v]`] -result [`[$m]`]*[`[$v]`] is [$m2] and tex version: [`[$m2]`] +result [`[$m]`] [`[$v]`] is [$m2] and tex version: [`[$m2]`] Convert the result to a vector [`[$v2]`] or to a column vector [`[$v2c]`] and then convert to a matrix again [$m2flat] [`[$m2flat]`] -Multiplication on the left of [`[$w]`] * [`[$m]`] is [$m3], +Multiplication on the left of [`[$w]`] [`[$m]`] is [$m3], the first row of which is [$m3->row(1)] and the tex version:[`[$m3]`] diff --git a/t/matrix_tableau_tests/print_tableau_Test.pg b/t/matrix_tableau_tests/print_tableau_Test.pg index 0ab1abddfc..6a2558ecaa 100644 --- a/t/matrix_tableau_tests/print_tableau_Test.pg +++ b/t/matrix_tableau_tests/print_tableau_Test.pg @@ -10,6 +10,7 @@ loadMacros( "parserLinearInequality.pl", "PGML.pl", "tableau.pl", + "tableau_main_subroutines.pl", #deprecated subroutines "PGmatrixmacros.pl", "LinearProgramming.pl", #"source.pl", # allows code to be displayed on certain sites. @@ -37,7 +38,7 @@ $m3 = matrix_from_submatrix($m, rows=>[1,4], columns=>[1,2]); $list = matrix_rows_to_list($m, 2,3); -$b = Matrix([1, 2, 3, 4]); +$b = Matrix([1, 2, 3, 4])->transpose; TEXT($BR, "vector", $b->data->[1]); $c = Matrix([5, 6, 7]); $t = Tableau->new(A=>$m,b=>$b, c=>$c); diff --git a/t/matrix_tableau_tests/tableau_module_test1.pg b/t/matrix_tableau_tests/tableau_module_test1.pg index 30cf72eb2f..b9a804acc6 100644 --- a/t/matrix_tableau_tests/tableau_module_test1.pg +++ b/t/matrix_tableau_tests/tableau_module_test1.pg @@ -55,13 +55,31 @@ Context()->texStrings; # $PAR # Enter tableau->{M} \{$t->{M}->ans_array\} +$t_dual = $t->dual_lop; + BEGIN_TEXT + +t is \[$t\] of type \{ref($t)\} $BR + +The tableau is \[\{$t->current_tableau\}\].$BR + +Dimension of b is \{$t->b->dimensions\} +$PAR +Dimension of c is \{$t->c->dimensions\} +$PAR +t_dual is \[$t_dual\] of type \{ref($t_dual)\} $BR +t_dual->current_tableau is \[\{$t_dual->current_tableau\}\] +$PAR m is \[$m\] + $PAR enter m = \{$m->ans_array\} -\{#$m->cmp->evaluate($m)->pretty_print\} +\{#$m->cmp->evaluate($m)->pretty_print()\} + + + END_TEXT Context()->normalStrings; @@ -72,7 +90,6 @@ Context()->normalStrings; # Answers # # -#ANS($t->{M}->cmp(checker=>tableauEquivalence()) ); ANS($m->cmp(checker=>tableauEquivalence())); diff --git a/t/matrix_tableau_tests/test_tableau_more.pl b/t/matrix_tableau_tests/test_tableau_more.pl index 1c6a1464c0..7f486f1743 100755 --- a/t/matrix_tableau_tests/test_tableau_more.pl +++ b/t/matrix_tableau_tests/test_tableau_more.pl @@ -17,9 +17,8 @@ require "tableau.pl"; require "Value.pl"; #gives us Real() etc. #require "Parser.pl"; #gives us Context() but also uses loadMacros(); -require "niceTables.pl"; +#require "niceTables.pl"; -ok(max(3,4)==4, "check max function"); sub Context {Parser::Context->current(\%context,@_)} @@ -67,11 +66,11 @@ sub WARN_MESSAGE{ # that inputs are matrices $ra_matrix = [[-$bill_money_commitment,-$bill_time_commitment, -1, 0, 1,0,0,-$bill_profit], [-$steve_money_commitment,-$steve_time_commitment, 0, -1, 0,1,0,-$steve_profit], - [-$money_total,-$time_total,0,0, 0,0, 1,0]]; + [-$money_total,-$time_total,-1,-1, 0,0, 1,0]]; $A = Value::Matrix->new([[-$bill_money_commitment,-$bill_time_commitment, -1, 0], [ -$steve_money_commitment,-$steve_time_commitment, 0, -1 ]]); $b = Value::Vector->new([-$bill_profit,-$steve_profit]); # need vertical vector -$c = Value::Vector->new([$money_total,$time_total,0,0]); +$c = Value::Vector->new([$money_total,$time_total,1,1]); $tableau1 = Tableau->new(A=>$A, b=>$b, c=>$c); ############################################################### @@ -86,7 +85,6 @@ sub WARN_MESSAGE{ print "stringify tableau: ", $tableau1, "\n"; -print lop_display($tableau1); is ($tableau1->{m}, 2, 'number of constraints is 2'); is ($tableau1->{n}, 4, 'number of variables is 4'); @@ -213,27 +211,40 @@ sub WARN_MESSAGE{ is_deeply([$tableau1->find_next_short_cut_pivot()],[undef,undef,1,0], "feasible point found"); is_deeply([$tableau1->find_next_short_cut_basis()],[1,2,'feasible_point'], "all constraints positive at basis {1,2} --start phase2"); -is_deeply([$tableau1->find_pivot_column('max')], [5,Value::Real->new(-1.2E+06),0], "col 5"); -is_deeply([$tableau1->find_pivot_row(5)], [2,Value::Real->new(8.4E06/3000),0], "row 2 "); -is_deeply([$tableau1->find_next_pivot('max')], [2,5,0,0], "pivot (2,5)"); -is_deeply([$tableau1->find_next_basis('max')],[1,5,undef], "new basis {1,5} continue"); +is_deeply([$tableau1->find_pivot_column('max')], [3,Value::Real->new(-100000),0], "col 3"); +is_deeply([$tableau1->find_pivot_row(3)], [1,Value::Real->new(550000/500),0], "row 1 "); +is_deeply([$tableau1->find_next_pivot('max')], [1,3,0,0], "pivot (1,3)"); +is_deeply([$tableau1->find_next_basis('max')],[2,3,undef], "new basis {2,3} continue"); -$tableau1->current_tableau(1,5); +$tableau1->current_tableau(2,3); diag($tableau1->current_tableau); -is_deeply([$tableau1->find_pivot_column('max')], [6,Value::Real->new(-6000),0], "col 6"); -is_deeply([$tableau1->find_pivot_row(6)], [-1,undef,1], "unbounded "); +is_deeply([$tableau1->find_pivot_column('max')], [4,Value::Real->new(-300),0], "col 4"); +is_deeply([$tableau1->find_pivot_row(4)], [1,4500,0], "row 2) "); -is_deeply([$tableau1->find_next_pivot('max')], [-1,6,0,1], "unbounded"); -# this is ok -- we're looking at the dual of the bill and steve problem -# and the original test was to minimize it not to maximize it -# recheck the original problem with websim!!!! +is_deeply([$tableau1->find_next_pivot('max')], [1,4,0,0], "pivot 1,4"); +is_deeply([$tableau1->find_next_basis('max')],[3,4,undef], "new basis {3,4} continue"); -# regularize the output for row and column definitions if one of the flags is set. -# can we always set those to undefined? -# can we change the flag notification to -# "unbounded, feasible_point, infeasible_tableau, optimal"? -# it might be easier to remember. +$tableau1->current_tableau(3,4); +diag($tableau1->current_tableau); +is_deeply([$tableau1->find_pivot_column('max')], [5,Value::Real->new(-1),0], "col 5"); +is_deeply([$tableau1->find_pivot_row(5)], [undef,undef,1], "row 2) "); + +is_deeply([$tableau1->find_next_pivot('max')], [undef,5,0,1], "unbounded -- no pivot"); +is_deeply([$tableau1->find_next_basis('max')],[3,4,'unbounded'], "basis 3,4 unbounded"); +# note that the column is returned from find_next_pivot so one can find a certificate +# of unboundedness (can return a line going off to infinity) + +# # this is ok -- we're looking at the dual of the bill and steve problem +# # and the original test was to minimize it not to maximize it +# # recheck the original problem with websim!!!! +# +# # regularize the output for row and column definitions if one of the flags is set. +# # can we always set those to undefined? +# # can we change the flag notification to +# # "unbounded, feasible_point, infeasible_tableau, optimal"? +# # it might be easier to remember. +# diag("reset tableau to feasible point and try to minimize it for phase2"); $tableau1->current_tableau(1,2); diag($tableau1->current_tableau); @@ -241,27 +252,15 @@ sub WARN_MESSAGE{ is_deeply([$tableau1->find_next_short_cut_basis()],[1,2,'feasible_point'], "all constraints positive at basis {1,2} --start phase2"); -is_deeply([$tableau1->find_pivot_column('min')], [3,Value::Real->new(1.2E+06),0], "col 3"); -is_deeply([$tableau1->find_pivot_row(3)], [1,Value::Real->new(550000/500),0], "row 1 "); -is_deeply([$tableau1->find_next_pivot('min')], [1,3,0,0], "pivot (1,3)"); -is_deeply([$tableau1->find_next_basis('min')],[2,3,undef], "new basis {2,3} continue"); - - +is_deeply([$tableau1->find_pivot_column('min')], [undef,undef,1], "all neg coeff"); +is_deeply([$tableau1->find_pivot_row(1)], [1,Value::Real->new(550000/1300000),0], "row 1 "); +is_deeply([$tableau1->find_next_pivot('min')], [undef,undef,1,0], "optimum"); +is_deeply([$tableau1->find_next_basis('min')],[1,2,'optimum'], "optimum"); +# +# -$tableau1->current_tableau(2,3); -diag($tableau1->current_tableau); - -is_deeply([$tableau1->find_pivot_column('min')], [4,Value::Real->new(600),0], "col 4"); -is_deeply([$tableau1->find_pivot_row(4)], [1,Value::Real->new(4500),0], "row 1 "); -is_deeply([$tableau1->find_next_pivot('min')], [1,4,0,0], "pivot (1,4)"); -is_deeply([$tableau1->find_next_basis('min')],[3,4,undef], "new basis {3,4} continue"); - -$tableau1->current_tableau(3,4); -diag($tableau1->current_tableau); + is_deeply($tableau1->statevars , # round off errors + [550000/1300000,8400000/1300000,0,0,0,0,8.339999999999999E9/1300000], "state variables"); -is_deeply([$tableau1->find_pivot_column('min')], [-1,undef,1], "optimum"); -#is_deeply([$tableau1->find_pivot_row(4)], [1,Value::Real->new(4500),0], "row 1 "); -is_deeply([$tableau1->find_next_pivot('min')], ['',-1,1,undef], "pivot (1,4) optimum"); -is_deeply([$tableau1->find_next_basis('min')],[3,4,'optimum'], " basis {3,4} optimum"); done_testing(); \ No newline at end of file From d167f6de2198cdde7649e20a2541f1b8b5ecde56 Mon Sep 17 00:00:00 2001 From: Michael Gage Date: Thu, 2 Nov 2017 10:55:51 -0400 Subject: [PATCH 21/30] Update unit tests. include deprecated subroutine lop_pivot_basis tableau_main_subroutines.pl contains non-object oriented functions that have for the most part been replaced by methods in tableau. --- macros/tableau_main_subroutines.pl | 13 +++++++++++++ t/matrix_tableau_tests/tableau_test3.pg | 1 + t/matrix_tableau_tests/tableau_test4.pg | 1 + t/matrix_tableau_tests/tableau_test5.pg | 1 + 4 files changed, 16 insertions(+) diff --git a/macros/tableau_main_subroutines.pl b/macros/tableau_main_subroutines.pl index 8eb3249854..5752b2e3de 100644 --- a/macros/tableau_main_subroutines.pl +++ b/macros/tableau_main_subroutines.pl @@ -9,6 +9,19 @@ sub isMatrix { sub matrix_column_slice{ matrix_from_matrix_cols(@_); } + +sub lp_basis_pivot { + my ($old_tableau,$old_basis,$pivot) = @_; # $pivot is a Value::Point + my $new_tableau= lp_clone($old_tableau); + main::lp_pivot($new_tableau, $pivot->extract(1)-1,$pivot->extract(2)-1); + my $new_matrix = Matrix($new_tableau); + my ($n,$m) = $new_matrix->dimensions; + my $param_size = $m-$n -1; #n=constraints+1, #m = $param_size + $constraints +2 + my $new_basis = ( $old_basis - ($pivot->extract(1)+$param_size) + ($pivot->extract(2)) )->sort; + my @statevars = get_tableau_variable_values($new_matrix, $new_basis); + return ( $new_tableau, Set($new_basis),\@statevars); #FIXME -- force to set (from type Union) to insure that ->data is an array and not a string. +} + sub matrix_from_matrix_cols { my $M = shift; # a MathObject matrix_columns my($n,$m) = $M->dimensions; diff --git a/t/matrix_tableau_tests/tableau_test3.pg b/t/matrix_tableau_tests/tableau_test3.pg index 705ad114e7..258bbbacef 100644 --- a/t/matrix_tableau_tests/tableau_test3.pg +++ b/t/matrix_tableau_tests/tableau_test3.pg @@ -13,6 +13,7 @@ loadMacros( "quickMatrixEntry.pl", #"scaffold.pl", "tableau.pl", +"tableau_main_subroutines.pl", #"gage_matrix_ops.pl", "PGinfo.pl", "source.pl", diff --git a/t/matrix_tableau_tests/tableau_test4.pg b/t/matrix_tableau_tests/tableau_test4.pg index 02448d246f..0026f3e4f9 100644 --- a/t/matrix_tableau_tests/tableau_test4.pg +++ b/t/matrix_tableau_tests/tableau_test4.pg @@ -13,6 +13,7 @@ loadMacros( "quickMatrixEntry.pl", #"scaffold.pl", "tableau.pl", +"tableau_main_subroutines.pl", #"gage_matrix_ops.pl", "PGinfo.pl", "source.pl", diff --git a/t/matrix_tableau_tests/tableau_test5.pg b/t/matrix_tableau_tests/tableau_test5.pg index 51648e8c7a..524ae9a34f 100644 --- a/t/matrix_tableau_tests/tableau_test5.pg +++ b/t/matrix_tableau_tests/tableau_test5.pg @@ -13,6 +13,7 @@ loadMacros( "quickMatrixEntry.pl", #"scaffold.pl", "tableau.pl", +"tableau_main_subroutines.pl", # deprecated routines #"gage_matrix_ops.pl", "PGinfo.pl", "source.pl", From 93b30add611087cbdd956dc7a0d2ca0369694ca7 Mon Sep 17 00:00:00 2001 From: Michael Gage Date: Tue, 7 Nov 2017 15:42:38 -0500 Subject: [PATCH 22/30] Add WWAccessor.pm, a version of Class::Accessor ver 0.34 which will work inside Safe. --- lib/WWAccessor.pm | 745 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 745 insertions(+) create mode 100644 lib/WWAccessor.pm diff --git a/lib/WWAccessor.pm b/lib/WWAccessor.pm new file mode 100644 index 0000000000..62051d4626 --- /dev/null +++ b/lib/WWAccessor.pm @@ -0,0 +1,745 @@ +package WWAccessor; +require 5.00502; +#use strict; +$WWAccessor::VERSION = '0.34'; +# Hacked version of Class::Accessor that will run? with WeBWorK. + +sub new { + my($proto, $fields) = @_; + my($class) = ref $proto || $proto; + + $fields = {} unless defined $fields; + + # make a copy of $fields. + bless {%$fields}, $class; +} + +sub mk_accessors { + my($self, @fields) = @_; + + $self->_mk_accessors('rw', @fields); +} + +if (eval { require Sub::Name }) { + Sub::Name->import; +} + +{ + no strict 'refs'; + + sub import { + my ($class, @what) = @_; + my $caller = caller; + for (@what) { + if (/^(?:antlers|moose-?like)$/i) { + *{"${caller}::has"} = sub { + my ($f, %args) = @_; + $caller->_mk_accessors(($args{is}||"rw"), $f); + }; + *{"${caller}::extends"} = sub { + @{"${caller}::ISA"} = @_; + unless (grep $_->can("_mk_accessors"), @_) { + push @{"${caller}::ISA"}, $class; + } + }; + # we'll use their @ISA as a default, in case it happens to be + # set already + &{"${caller}::extends"}(@{"${caller}::ISA"}); + } + } + } + + sub follow_best_practice { + my($self) = @_; + my $class = ref $self || $self; + *{"${class}::accessor_name_for"} = \&best_practice_accessor_name_for; + *{"${class}::mutator_name_for"} = \&best_practice_mutator_name_for; + } + + sub _mk_accessors { + my($self, $access, @fields) = @_; + my $class = ref $self || $self; + my $ra = $access eq 'rw' || $access eq 'ro'; + my $wa = $access eq 'rw' || $access eq 'wo'; + + foreach my $field (@fields) { + my $accessor_name = $self->accessor_name_for($field); + my $mutator_name = $self->mutator_name_for($field); + if( $accessor_name eq 'DESTROY' or $mutator_name eq 'DESTROY' ) { + $self->_carp("Having a data accessor named DESTROY in '$class' is unwise."); + } + if ($accessor_name eq $mutator_name) { + my $accessor; + if ($ra && $wa) { + $accessor = $self->make_accessor($field); + } elsif ($ra) { + $accessor = $self->make_ro_accessor($field); + } else { + $accessor = $self->make_wo_accessor($field); + } + my $fullname = "${class}::$accessor_name"; + my $subnamed = 0; + unless (defined &{$fullname}) { + subname($fullname, $accessor) if defined &subname; + $subnamed = 1; + *{$fullname} = $accessor; + } + if ($accessor_name eq $field) { + # the old behaviour + my $alias = "${class}::_${field}_accessor"; + subname($alias, $accessor) if defined &subname and not $subnamed; + *{$alias} = $accessor unless defined &{$alias}; + } + } else { + my $fullaccname = "${class}::$accessor_name"; + my $fullmutname = "${class}::$mutator_name"; + if ($ra and not defined &{$fullaccname}) { + my $accessor = $self->make_ro_accessor($field); + subname($fullaccname, $accessor) if defined &subname; + *{$fullaccname} = $accessor; + } + if ($wa and not defined &{$fullmutname}) { + my $mutator = $self->make_wo_accessor($field); + subname($fullmutname, $mutator) if defined &subname; + *{$fullmutname} = $mutator; + } + } + } + } + +} + +sub mk_ro_accessors { + my($self, @fields) = @_; + + $self->_mk_accessors('ro', @fields); +} + +sub mk_wo_accessors { + my($self, @fields) = @_; + + $self->_mk_accessors('wo', @fields); +} + +sub best_practice_accessor_name_for { + my ($class, $field) = @_; + return "get_$field"; +} + +sub best_practice_mutator_name_for { + my ($class, $field) = @_; + return "set_$field"; +} + +sub accessor_name_for { + my ($class, $field) = @_; + return $field; +} + +sub mutator_name_for { + my ($class, $field) = @_; + return $field; +} + +sub set { + my($self, $key) = splice(@_, 0, 2); + + if(@_ == 1) { + $self->{$key} = $_[0]; + } + elsif(@_ > 1) { + $self->{$key} = [@_]; + } + else { + $self->_croak("Wrong number of arguments received"); + } +} + +sub get { + my $self = shift; + + if(@_ == 1) { + return $self->{$_[0]}; + } + elsif( @_ > 1 ) { + return @{$self}{@_}; + } + else { + $self->_croak("Wrong number of arguments received"); + } +} + +sub make_accessor { + my ($class, $field) = @_; + + return sub { + my $self = shift; + + if(@_) { + return $self->set($field, @_); + } else { + return $self->get($field); + } + }; +} + +sub make_ro_accessor { + my($class, $field) = @_; + + return sub { + my $self = shift; + + if (@_) { + my $caller = caller; + $self->_croak("'$caller' cannot alter the value of '$field' on objects of class '$class'"); + } + else { + return $self->get($field); + } + }; +} + +sub make_wo_accessor { + my($class, $field) = @_; + + return sub { + my $self = shift; + + unless (@_) { + my $caller = caller; + $self->_croak("'$caller' cannot access the value of '$field' on objects of class '$class'"); + } + else { + return $self->set($field, @_); + } + }; +} + + +#use Carp (); + +sub _carp { + my ($self, $msg) = @_; + Carp::carp($msg || $self); + return; +} + +sub _croak { + my ($self, $msg) = @_; + Carp::croak($msg || $self); + return; +} + +1; + +__END__ + +=head1 NAME + + WWAccessor - Automated accessor generation + +=head1 SYNOPSIS + + package Foo; + use base qw(WWAccessor); + Foo->follow_best_practice; + Foo->mk_accessors(qw(name role salary)); + + # or if you prefer a Moose-like interface... + + package Foo; + use WWAccessor "antlers"; + has name => ( is => "rw", isa => "Str" ); + has role => ( is => "rw", isa => "Str" ); + has salary => ( is => "rw", isa => "Num" ); + + # Meanwhile, in a nearby piece of code! + # WWAccessor provides new(). + my $mp = Foo->new({ name => "Marty", role => "JAPH" }); + + my $job = $mp->role; # gets $mp->{role} + $mp->salary(400000); # sets $mp->{salary} = 400000 # I wish + + # like my @info = @{$mp}{qw(name role)} + my @info = $mp->get(qw(name role)); + + # $mp->{salary} = 400000 + $mp->set('salary', 400000); + + +=head1 DESCRIPTION + +This module automagically generates accessors/mutators for your class. + +Most of the time, writing accessors is an exercise in cutting and +pasting. You usually wind up with a series of methods like this: + + sub name { + my $self = shift; + if(@_) { + $self->{name} = $_[0]; + } + return $self->{name}; + } + + sub salary { + my $self = shift; + if(@_) { + $self->{salary} = $_[0]; + } + return $self->{salary}; + } + + # etc... + +One for each piece of data in your object. While some will be unique, +doing value checks and special storage tricks, most will simply be +exercises in repetition. Not only is it Bad Style to have a bunch of +repetitious code, but it's also simply not lazy, which is the real +tragedy. + +If you make your module a subclass of WWAccessor and declare your +accessor fields with mk_accessors() then you'll find yourself with a +set of automatically generated accessors which can even be +customized! + +The basic set up is very simple: + + package Foo; + use base qw(WWAccessor); + Foo->mk_accessors( qw(far bar car) ); + +Done. Foo now has simple far(), bar() and car() accessors +defined. + +Alternatively, if you want to follow Damian's I guidelines +you can use: + + package Foo; + use base qw(WWAccessor); + Foo->follow_best_practice; + Foo->mk_accessors( qw(far bar car) ); + +B you must call C before calling C. + +=head2 Moose-like + +By popular demand we now have a simple Moose-like interface. You can now do: + + package Foo; + use WWAccessor "antlers"; + has far => ( is => "rw" ); + has bar => ( is => "rw" ); + has car => ( is => "rw" ); + +Currently only the C attribute is supported. + +=head1 CONSTRUCTOR + +WWAccessor provides a basic constructor, C. It generates a +hash-based object and can be called as either a class method or an +object method. + +=head2 new + + my $obj = Foo->new; + my $obj = $other_obj->new; + + my $obj = Foo->new(\%fields); + my $obj = $other_obj->new(\%fields); + +It takes an optional %fields hash which is used to initialize the +object (handy if you use read-only accessors). The fields of the hash +correspond to the names of your accessors, so... + + package Foo; + use base qw(WWAccessor); + Foo->mk_accessors('foo'); + + my $obj = Foo->new({ foo => 42 }); + print $obj->foo; # 42 + +however %fields can contain anything, new() will shove them all into +your object. + +=head1 MAKING ACCESSORS + +=head2 follow_best_practice + +In Damian's Perl Best Practices book he recommends separate get and set methods +with the prefix set_ and get_ to make it explicit what you intend to do. If you +want to create those accessor methods instead of the default ones, call: + + __PACKAGE__->follow_best_practice + +B you call any of the accessor-making methods. + +=head2 accessor_name_for / mutator_name_for + +You may have your own crazy ideas for the names of the accessors, so you can +make those happen by overriding C and C in +your subclass. (I copied that idea from Class::DBI.) + +=head2 mk_accessors + + __PACKAGE__->mk_accessors(@fields); + +This creates accessor/mutator methods for each named field given in +@fields. Foreach field in @fields it will generate two accessors. +One called "field()" and the other called "_field_accessor()". For +example: + + # Generates foo(), _foo_accessor(), bar() and _bar_accessor(). + __PACKAGE__->mk_accessors(qw(foo bar)); + +See L +for details. + +=head2 mk_ro_accessors + + __PACKAGE__->mk_ro_accessors(@read_only_fields); + +Same as mk_accessors() except it will generate read-only accessors +(ie. true accessors). If you attempt to set a value with these +accessors it will throw an exception. It only uses get() and not +set(). + + package Foo; + use base qw(WWAccessor); + Foo->mk_ro_accessors(qw(foo bar)); + + # Let's assume we have an object $foo of class Foo... + print $foo->foo; # ok, prints whatever the value of $foo->{foo} is + $foo->foo(42); # BOOM! Naughty you. + + +=head2 mk_wo_accessors + + __PACKAGE__->mk_wo_accessors(@write_only_fields); + +Same as mk_accessors() except it will generate write-only accessors +(ie. mutators). If you attempt to read a value with these accessors +it will throw an exception. It only uses set() and not get(). + +B I'm not entirely sure why this is useful, but I'm sure someone +will need it. If you've found a use, let me know. Right now it's here +for orthoginality and because it's easy to implement. + + package Foo; + use base qw(WWAccessor); + Foo->mk_wo_accessors(qw(foo bar)); + + # Let's assume we have an object $foo of class Foo... + $foo->foo(42); # OK. Sets $self->{foo} = 42 + print $foo->foo; # BOOM! Can't read from this accessor. + +=head1 Moose! + +If you prefer a Moose-like interface to create accessors, you can use C by +importing this module like this: + + use WWAccessor "antlers"; + +or + + use WWAccessor "moose-like"; + +Then you can declare accessors like this: + + has alpha => ( is => "rw", isa => "Str" ); + has beta => ( is => "ro", isa => "Str" ); + has gamma => ( is => "wo", isa => "Str" ); + +Currently only the C attribute is supported. And our C also supports +the "wo" value to make a write-only accessor. + +If you are using the Moose-like interface then you should use the C +rather than tweaking your C<@ISA> directly. Basically, replace + + @ISA = qw/Foo Bar/; + +with + + extends(qw/Foo Bar/); + +=head1 DETAILS + +An accessor generated by WWAccessor looks something like +this: + + # Your foo may vary. + sub foo { + my($self) = shift; + if(@_) { # set + return $self->set('foo', @_); + } + else { + return $self->get('foo'); + } + } + +Very simple. All it does is determine if you're wanting to set a +value or get a value and calls the appropriate method. +WWAccessor provides default get() and set() methods which +your class can override. They're detailed later. + +=head2 Modifying the behavior of the accessor + +Rather than actually modifying the accessor itself, it is much more +sensible to simply override the two key methods which the accessor +calls. Namely set() and get(). + +If you -really- want to, you can override make_accessor(). + +=head2 set + + $obj->set($key, $value); + $obj->set($key, @values); + +set() defines how generally one stores data in the object. + +override this method to change how data is stored by your accessors. + +=head2 get + + $value = $obj->get($key); + @values = $obj->get(@keys); + +get() defines how data is retreived from your objects. + +override this method to change how it is retreived. + +=head2 make_accessor + + $accessor = __PACKAGE__->make_accessor($field); + +Generates a subroutine reference which acts as an accessor for the given +$field. It calls get() and set(). + +If you wish to change the behavior of your accessors, try overriding +get() and set() before you start mucking with make_accessor(). + +=head2 make_ro_accessor + + $read_only_accessor = __PACKAGE__->make_ro_accessor($field); + +Generates a subroutine refrence which acts as a read-only accessor for +the given $field. It only calls get(). + +Override get() to change the behavior of your accessors. + +=head2 make_wo_accessor + + $read_only_accessor = __PACKAGE__->make_wo_accessor($field); + +Generates a subroutine refrence which acts as a write-only accessor +(mutator) for the given $field. It only calls set(). + +Override set() to change the behavior of your accessors. + +=head1 EXCEPTIONS + +If something goes wrong WWAccessor will warn or die by calling Carp::carp +or Carp::croak. If you don't like this you can override _carp() and _croak() in +your subclass and do whatever else you want. + +=head1 EFFICIENCY + +WWAccessor does not employ an autoloader, thus it is much faster +than you'd think. Its generated methods incur no special penalty over +ones you'd write yourself. + + accessors: + Rate Basic Fast Faster Direct + Basic 367589/s -- -51% -55% -89% + Fast 747964/s 103% -- -9% -77% + Faster 819199/s 123% 10% -- -75% + Direct 3245887/s 783% 334% 296% -- + + mutators: + Rate Acc Fast Faster Direct + Acc 265564/s -- -54% -63% -91% + Fast 573439/s 116% -- -21% -80% + Faster 724710/s 173% 26% -- -75% + Direct 2860979/s 977% 399% 295% -- + +WWAccessor::Fast is faster than methods written by an average programmer +(where "average" is based on Schwern's example code). + +WWAccessor is slower than average, but more flexible. + +WWAccessor::Faster is even faster than WWAccessor::Fast. It uses an +array internally, not a hash. This could be a good or bad feature depending on +your point of view. + +Direct hash access is, of course, much faster than all of these, but it +provides no encapsulation. + +Of course, it's not as simple as saying "WWAccessor is slower than +average". These are benchmarks for a simple accessor. If your accessors do +any sort of complicated work (such as talking to a database or writing to a +file) the time spent doing that work will quickly swamp the time spend just +calling the accessor. In that case, WWAccessor and the ones you write +will be roughly the same speed. + + +=head1 EXAMPLES + +Here's an example of generating an accessor for every public field of +your class. + + package Altoids; + + use base qw(WWAccessor Class::Fields); + use fields qw(curiously strong mints); + Altoids->mk_accessors( Altoids->show_fields('Public') ); + + sub new { + my $proto = shift; + my $class = ref $proto || $proto; + return fields::new($class); + } + + my Altoids $tin = Altoids->new; + + $tin->curiously('Curiouser and curiouser'); + print $tin->{curiously}; # prints 'Curiouser and curiouser' + + + # Subclassing works, too. + package Mint::Snuff; + use base qw(Altoids); + + my Mint::Snuff $pouch = Mint::Snuff->new; + $pouch->strong('Blow your head off!'); + print $pouch->{strong}; # prints 'Blow your head off!' + + +Here's a simple example of altering the behavior of your accessors. + + package Foo; + use base qw(WWAccessor); + Foo->mk_accessors(qw(this that up down)); + + sub get { + my $self = shift; + + # Note every time someone gets some data. + print STDERR "Getting @_\n"; + + $self->SUPER::get(@_); + } + + sub set { + my ($self, $key) = splice(@_, 0, 2); + + # Note every time someone sets some data. + print STDERR "Setting $key to @_\n"; + + $self->SUPER::set($key, @_); + } + + +=head1 CAVEATS AND TRICKS + +WWAccessor has to do some internal wackiness to get its +job done quickly and efficiently. Because of this, there's a few +tricks and traps one must know about. + +Hey, nothing's perfect. + +=head2 Don't make a field called DESTROY + +This is bad. Since DESTROY is a magical method it would be bad for us +to define an accessor using that name. WWAccessor will +carp if you try to use it with a field named "DESTROY". + +=head2 Overriding autogenerated accessors + +You may want to override the autogenerated accessor with your own, yet +have your custom accessor call the default one. For instance, maybe +you want to have an accessor which checks its input. Normally, one +would expect this to work: + + package Foo; + use base qw(WWAccessor); + Foo->mk_accessors(qw(email this that whatever)); + + # Only accept addresses which look valid. + sub email { + my($self) = shift; + my($email) = @_; + + if( @_ ) { # Setting + require Email::Valid; + unless( Email::Valid->address($email) ) { + carp("$email doesn't look like a valid address."); + return; + } + } + + return $self->SUPER::email(@_); + } + +There's a subtle problem in the last example, and it's in this line: + + return $self->SUPER::email(@_); + +If we look at how Foo was defined, it called mk_accessors() which +stuck email() right into Foo's namespace. There *is* no +SUPER::email() to delegate to! Two ways around this... first is to +make a "pure" base class for Foo. This pure class will generate the +accessors and provide the necessary super class for Foo to use: + + package Pure::Organic::Foo; + use base qw(WWAccessor); + Pure::Organic::Foo->mk_accessors(qw(email this that whatever)); + + package Foo; + use base qw(Pure::Organic::Foo); + +And now Foo::email() can override the generated +Pure::Organic::Foo::email() and use it as SUPER::email(). + +This is probably the most obvious solution to everyone but me. +Instead, what first made sense to me was for mk_accessors() to define +an alias of email(), _email_accessor(). Using this solution, +Foo::email() would be written with: + + return $self->_email_accessor(@_); + +instead of the expected SUPER::email(). + + +=head1 AUTHORS + +Copyright 2009 Marty Pauley + +This program is free software; you can redistribute it and/or modify it under +the same terms as Perl itself. That means either (a) the GNU General Public +License or (b) the Artistic License. + +=head2 ORIGINAL AUTHOR + +Michael G Schwern + +=head2 THANKS + +Liz and RUZ for performance tweaks. + +Tels, for his big feature request/bug report. + +Various presenters at YAPC::Asia 2009 for criticising the non-Moose interface. + +=head1 SEE ALSO + +See L and L if speed is more +important than flexibility. + +These are some modules which do similar things in different ways +L, L, L, +L, L, L, L + +See L for an example of this module in use. + +=cut From 09fce1229f7aaaa759c1c54a49e4e296001e421d Mon Sep 17 00:00:00 2001 From: Michael Gage Date: Tue, 7 Nov 2017 15:45:14 -0500 Subject: [PATCH 23/30] Update tableau.pl Has some pivot operations coded now Add some more test files -- including a .pl file which uses Test::More for unit tests. --- macros/tableau.pl | 222 +++++++++++++++--- .../tableau_row_operations_testB.pg | 2 +- t/matrix_tableau_tests/tableau_test1.pg | 88 +++---- t/matrix_tableau_tests/test_tableau_more.pl | 14 ++ 4 files changed, 244 insertions(+), 82 deletions(-) diff --git a/macros/tableau.pl b/macros/tableau.pl index 6722b289bf..1316c9007d 100755 --- a/macros/tableau.pl +++ b/macros/tableau.pl @@ -10,7 +10,21 @@ =head1 NAME macros/tableau.pl +=head2 TODO + change find_next_basis_from_pivot to next_basis_from_pivot + add phase2 to some match phase1 for some of the pivots + add a generic _solve that solves tableau once it has a filled basis + add something that will fill the basis. + + regularize the use of m and n -- should they be the number of + constraints and the number of decision(problem) variables or should + they be the size of the complete tableau. + + current tableau returns the complete tableau minus the objective function + row. (do we need a second objective function row?) + +=cut =head2 DESCRIPTION @@ -104,16 +118,6 @@ =head2 Package main =cut -=item lop_display - - Useage: - - lop_display($tableau, align=>'cccc|cc|c|c', toplevel=>[qw(x1,x2,x3,x4,s1,s2,P,b)]) - -Pretty prints the output of a matrix as a LOP with separating labels and -variable labels. - -=cut =head3 References: @@ -175,14 +179,22 @@ package main; sub _tableau_init {}; # don't reload this file # loadMacros("tableau_main_subroutines.pl"); - +sub tableau_equivalence { # I think this might be better -- matches linebrea_at_commas + # the two should be consistent. + tableauEquivalence(@_); +} sub tableauEquivalence { return sub { my ($correct, $student, $ansHash) = @_; - # DEBUG_MESSAGE("executing tableau equivalence"); + #DEBUG_MESSAGE("executing tableau equivalence"); + #DEBUG_MESSAGE("$correct"); + #DEBUG_MESSAGE("$student"); + Value::Error("correct answer is not a matrix") unless ref($correct)=~/Value::Matrix/; + Value::Error("student answer is not a matrix") unless ref($student)=~/Value::Matrix/; + # convert matrices to arrays of row references - my @rows1 = matrix_extract_rows($correct); - my @rows2 = matrix_extract_rows($student); + my @rows1 = $correct->extract_rows; + my @rows2 = $student->extract_rows; # compare the rows as lists with each row being compared as # a parallel Vector (i.e. up to multiples) my $score = List(@rows1)->cmp( checker => @@ -202,7 +214,7 @@ sub tableauEquivalence { # linebreak_at_commas() # ); -package main; + sub linebreak_at_commas { return sub { my $ans=shift; @@ -223,10 +235,25 @@ sub linebreak_at_commas { # Pretty prints the output of a matrix as a LOP with separating labels and # variable labels. + +=item lop_display + + Useage: + + lop_display($tableau, align=>'cccc|cc|c|c', toplevel=>[qw(x1,x2,x3,x4,s1,s2,P,b)]) + +Pretty prints the output of a matrix as a LOP with separating labels and +variable labels. + +=cut +our $max_number_of_steps = 20; + sub lop_display { my $tableau = shift; %options = @_; - $options{alignment} = ($options{alignment})? $options{alignment}:"|ccccc|cc|c|c|"; + #TODO get alignment and toplevel from tableau + #override it with explicit options. + $alignment = ($options{align})? $options{align}:"|ccccc|cc|c|c|"; @toplevel = (); if (exists( ($options{toplevel})) ) { @toplevel = @{$options{toplevel}}; @@ -236,10 +263,106 @@ sub lop_display { $last_row = $#matrix; # last row is objective coefficients $matrix[$last_row-1]->[0]=[$matrix[$last_row-1]->[0],midrule=>1]; $matrix[$last_row]->[0]=[$matrix[$last_row]->[0],midrule=>1]; - DataTable([[@toplevel],@matrix],align=>$options{alignment}); + DataTable([[@toplevel],@matrix],align=>$alignment); } -sub get_tableau_variable_values { +# for main section of tableau.pl + +# make one phase2 pivot on a tableau (works in place) +# returns flag with '', 'optimum' or 'unbounded' +sub next_tableau { + my $self = shift; + my $max_or_min = shift; + Value::Error("next_tableau requires a 'max' or 'min' argument") + unless $max_or_min=~/max|min/; + my @out = $self->find_next_basis($max_or_min); + my $flag = pop(@out); + if ($flag) { + } else { # update matrix + $self->basis(Set(@out)); + } + return $flag; +} + + +#iteratively phase2 pivots a feasible tableau until the +# flag returns 'optimum' or 'unbounded' +# tableau is returned in a "stopped" state. + +sub phase2_solve { + my $tableau = shift; + my $max_or_min = shift; #FIXME -- this needs a sanity check + Value::Error("phase2_solve requires a 'max' or 'min' argument") + unless $max_or_min=~/max|min/; + # calculate the final state by phase2 pivoting through the tableaus. + my $state_flag = ''; + my $tableau_copy = $tableau->copy; + my $i=0; + while ((not $state_flag) and $i <=$max_number_of_steps ) { + $state_flag = next_tableau($tableau_copy,$max_or_min); + $i++; + } + return($tableau_copy,$state_flag, $i); + # TEXT("Number of iterations is $i $BR"); +} + +# make one phase 1 pivot on a tableau (works in place) +# returns flag with '', 'infeasible_lop' or 'feasible_point' +# perhaps 'feasible_point' should be 'feasible_lop' +sub next_short_cut_tableau { + my $self = shift; + my @out = $self->find_next_short_cut_basis(); + my $flag = pop(@out); + # TEXT(" short cut tableau flag $flag $BR"); + if ($flag) { + } else { # update matrix + $self->basis(Set(@out)); + } + return $flag; +} +sub phase1_solve { + my $tableau = shift; + my $state_flag = ''; + my $tableau_copy = $tableau->copy; + my $steps = 0; + while (not $state_flag and $pivots <= $max_number_of_steps) { + $state_flag = next_short_cut_tableau($tableau_copy); + $steps++; + } + return( $tableau_copy, $state_flag, $steps); +} + +=item primal_basis_to_dual dual_basis_to_primal + + [complementary_basis_set] = $self->primal_basis_to_dual(primal_basis_set) + [complementary_basis_set] = $self->dual_basis_to_primal(dual_basis_set) + +=cut + +$primal2dual = sub { + my $i =$shift; + if ($i<=$n and $i>0) { + return $m +$i; + } elsif ($i > $n and $i<= $n+$m) { + return $i-$n + } else { + Value::Error("index $i is out of range"); + } +}; + +$dual2primal = sub { + my $j=$shift; + if ($j<=$m and $j>0) { + return $n+$j; + } elsif ($j>$m and $j<= $n+$m) { + return $j-$m + }else { + Value::Error("index $i is out of range"); + } +}; + +# deprecated for tableaus - use $tableau->statevars instead +sub get_tableau_variable_values { my $mat = shift; # a MathObject matrix my $basis =shift; # a MathObject set # FIXME @@ -292,10 +415,22 @@ package Tableau; )); -our $zeroLevelFraction = Value::Real->new(1E-10); +our $tableauZeroLevel = Value::Real->new(1E-10); +# consider entries zero if they are less than $tableauZeroLevel times the current_basis_coeff. + + +sub close_enough_to_zero { + my $self = shift; + my $value = shift; + #main::DEBUG_MESSAGE("value is $value"); + #main::DEBUG_MESSAGE("current_basis is ", $self->current_basis_coeff); + #main::DEBUG_MESSAGE("cutoff is ", $tableauZeroLevel*($self->current_basis_coeff)); + return (abs($value)<= $tableauZeroLevel*($self->current_basis_coeff))? 1: 0; +} sub class {"Matrix"}; + sub _Matrix { # can we just import this? Value::Matrix->new(@_); } @@ -445,7 +580,7 @@ sub objective_row { ------------- |A | S |0| b| ------------- - | c |z|z*| + | -c |z|z*| ------------- If a list of basis columns is passed as an argument then $self->basis() @@ -468,7 +603,7 @@ sub current_tableau { =item statevars - + [x1,.....xn,]= $self->statevars() @@ -482,7 +617,8 @@ sub statevars { # type check ref($mat)='Matrix'; ref($basis)='Set'; # or check that $mat has dimensions, element methods; and $basis has a contains method my ($m,$n) = $matrix->dimensions; - + # m= number of constraints + 1. + # n = number of constraints + number of variables +2 @var = (); #print( "start new matrix $m $n \n"); #print "tableau is ", $matrix, "\n"; @@ -585,7 +721,7 @@ sub basis { =item find_next_basis - ($basis->value,$flag) = $self->find_next_basis (max/min, obj_row_number) + ($col1,$col2,..,$flag) = $self->find_next_basis (max/min, obj_row_number) In phase 2 of the simplex method calculates the next basis. $optimum or $unbounded is set @@ -593,8 +729,11 @@ sub basis { $col gives a certificate of unboundedness. $flag can be either 'optimum' or 'unbounded' in which case the basis returned is the current basis. -$basis->value is an ARRAY of column numbers. +is a list of column numbers. +FIXME Should we change this so that ($basis,$flag) is returned instead? $basis and $flag +are very different things. $basis could be a set or list type but in that case it can't have undef +as an entry. It probably can have '' (an empty string) =cut @@ -602,7 +741,7 @@ sub basis { sub find_next_basis { my $self = shift;Value::Error( "call find_next_basis as a Tableau method") unless ref($self)=~/Tableau/; my $max_or_min = shift; - my $obj_row_number = shift; + my $obj_row_number = shift//1; my ( $row_index, $col_index, $optimum, $unbounded)= $self->find_next_pivot($max_or_min, $obj_row_number); my $flag = undef; @@ -721,7 +860,7 @@ sub find_pivot_column { my $index = undef; my $optimum = 1; my $value = undef; - my $zeroLevelTol = $zeroLevelFraction * ($self->current_basis_coeff); + my $zeroLevelTol = $tableauZeroLevel * ($self->current_basis_coeff); # main::DEBUG_MESSAGE(" coldim: $obj_col_dim , row: $obj_row_index obj_matrix: $obj_row_matrix ".ref($obj_row_matrix) ); # main::DEBUG_MESSAGE(" \@obj_row ", join(' ', @obj_row ) ); for (my $k=1; $k<=$obj_col_dim; $k++) { @@ -765,7 +904,7 @@ sub find_pivot_row { my $value = undef; my $index = undef; my $unbounded = 1; - my $zeroLevelTol = $zeroLevelFraction * ($self->current_basis_coeff); + my $zeroLevelTol = $tableauZeroLevel * ($self->current_basis_coeff); for (my $k=1; $k<=$row_dim; $k++) { my $m = $self->{current_constraint_matrix}->element($k,$column_index); # main::DEBUG_MESSAGE(" m[$k,$column_index] is ", $m->value); @@ -773,7 +912,7 @@ sub find_pivot_row { my $b = $self->{current_b}->element($k,1); # main::DEBUG_MESSAGE(" b[$k] is ", $b->value); # main::DEBUG_MESSAGE("finding pivot row in column $column_index, row: $k ", ($b/$m)->value); - if ( not defined($value) or $b/$m < $value) { + if ( not defined($value) or $b/$m < $value-$zeroLevelTol) { # want first smallest value $value = $b/$m; $index = $k; # memorize index $unbounded = 0; @@ -814,7 +953,9 @@ sub find_leaving_column { foreach my $k (1..$col_dim) { next unless $basis->contains(main::Set($k)); $m_ik = $self->{current_constraint_matrix}->element($row_index, $k); - next unless $m_ik !=0; + # main::DEBUG_MESSAGE("$m_ik in col $k is close to zero ", $self->close_enough_to_zero($m_ik)); + next if $self->close_enough_to_zero($m_ik); + # main::DEBUG_MESSAGE("leaving column is $k"); $index = $k; # memorize index $value = $m_ik; last; @@ -847,7 +988,7 @@ sub find_next_short_cut_pivot { } else { ($col_index, $value, $infeasible_lop) = $self->find_short_cut_column($row_index); if ($infeasible_lop){ - $row_index=undef; $col_index=undef; $feasible_point=0; + $row_index=undef; $col_index=undef; $feasible_point=0; } } return($row_index, $col_index, $feasible_point, $infeasible_lop); @@ -872,7 +1013,8 @@ sub find_next_short_cut_pivot { sub find_next_short_cut_basis { - my $self = shift;Value::Error( "call find_next_short_cut_basis as a Tableau method") unless ref($self)=~/Tableau/; + my $self = shift; + Value::Error( "call find_next_short_cut_basis as a Tableau method") unless ref($self)=~/Tableau/; my ( $row_index, $col_index, $feasible_point, $infeasible_lop)= $self->find_next_short_cut_pivot(); @@ -881,7 +1023,7 @@ sub find_next_short_cut_basis { if ($feasible_point or $infeasible_lop) { $basis=$self->basis(); if ($feasible_point) { - $flag = 'feasible_point'; + $flag = 'feasible_point'; #should be feasible_lop ? } elsif ($infeasible_lop){ $flag = 'infeasible_lop'; } @@ -912,14 +1054,16 @@ sub find_short_cut_row { my $index = undef; my $value = undef; my $feasible = 1; + my $zeroLevelTol = $tableauZeroLevel * ($self->current_basis_coeff); for (my $k=1; $k<=$row_dim; $k++) { my $b_k1 = $self->current_b->element($k,$col_index); #main::diag("b[$k] = $b_k1"); - next if $b_k1>=0; #skip positive entries; - $index =$k; - $value = $b_k1; - $feasible = 0; - last; + next if $b_k1>=-$zeroLevelTol; #skip positive entries; + if ( not defined($value) or $b_k1 < $value) { + $index =$k; + $value = $b_k1; + $feasible = 0; #found at least one negative entry in the row + } } return ( $index, $value, $feasible); } @@ -1051,7 +1195,9 @@ sub dual_lop { $newb = $newb->transpose; # converts to an n by 1 matrix my $newc = $self->b; # gives an m by 1 matrix $newc = _Matrix( $newc->transpose->value ); # convert to a 1 by m matrix - my $newt = Tableau->new(A=>$newA, b=>$newb, c=>$newc); + my $newt = Tableau->new(A=>-$newA, b=>-$newb, c=>$newc); + # rewrites the constraints as negative + # the dual cost function is to be minimized. $newt; } diff --git a/t/matrix_tableau_tests/tableau_row_operations_testB.pg b/t/matrix_tableau_tests/tableau_row_operations_testB.pg index 6d681714e9..04fa371aa3 100644 --- a/t/matrix_tableau_tests/tableau_row_operations_testB.pg +++ b/t/matrix_tableau_tests/tableau_row_operations_testB.pg @@ -154,7 +154,7 @@ Figure out outputs: $PAR find_next_basis: @output_find_next_basis $PAR test1 \{[$tableau1->find_next_basis('max')]->[0]\},$PAR test2 \{[$tableau1->find_next_basis('max')]->[1] \},$PAR - +test3 \{@out = $tableau1->find_next_basis('max'),join(",",@out[0,1])\} $HR next pivot 1 : \{ join(",",$tableau1->find_next_pivot('max'))\}$PAR diff --git a/t/matrix_tableau_tests/tableau_test1.pg b/t/matrix_tableau_tests/tableau_test1.pg index 0983a57977..a812cba69e 100644 --- a/t/matrix_tableau_tests/tableau_test1.pg +++ b/t/matrix_tableau_tests/tableau_test1.pg @@ -44,22 +44,22 @@ $t = Tableau->new(A=>$m,b=>$b, c=>$c); $basis2 = $t->basis(1,3,4,6); $t->current_tableau; -$c_B = $t->{obj_row}->extract_columns($t->{basis} ); #basis coefficients -$c_B2 = Value::Vector->new(map {$_->value} @$c_B); -$c_4 = $t->current_tableau; - - -my $Badj = ($t->{current_basis_matrix}->det) * ($t->{current_basis_matrix}->inverse); -my $current_tableau = $Badj * $t->{M}; # the A | S | obj | b - -$correction_coeff = ($c_B2*$current_tableau)->row(1); -$obj_row_normalized = ($t->{current_basis_matrix}->det) *$t->{obj_row}; -$current_coeff = $obj_row_normalized-$correction_coeff ; - -TEXT("obj_row ", $t->{obj_row}, $BR ); -TEXT("c_b is", @$c_B,$BR); -TEXT("c_b2 is", $c_B2,$BR); -TEXT("current coeff ", List($current_coeff),$BR); +#$c_B = $t->{obj_row}->extract_columns($t->{basis} ); #basis coefficients +# $c_B2 = Value::Vector->new(map {$_->value} @$c_B); +# $c_4 = $t->current_tableau; +# +# +# my $Badj = ($t->{current_basis_matrix}->det) * ($t->{current_basis_matrix}->inverse); +# my $current_tableau = $Badj * $t->{M}; # the A | S | obj | b +# +# $correction_coeff = ($c_B2*$current_tableau)->row(1); +# $obj_row_normalized = ($t->{current_basis_matrix}->det) *$t->{obj_row}; +# $current_coeff = $obj_row_normalized-$correction_coeff ; +# +# TEXT("obj_row ", $t->{obj_row}, $BR ); +# TEXT("c_b is", @$c_B,$BR); +# TEXT("c_b2 is", $c_B2,$BR); +# TEXT("current coeff ", List($current_coeff),$BR); BEGIN_PGML matrix is [`[$m]`] @@ -70,35 +70,37 @@ and c is [$c] original tableau is [`[$t->{M}]`] and basis is [$t->basis] - -B is [`[$t->{current_basis_matrix}]`] with determinant [$t->{current_basis_matrix}->det] - -the objective row is [@ $t->{obj_row} @] - - -the coefficients associated with the basis are [@ List(@$c_B) @] - -the vector version of these coefficients is [$c_B2] - -the normalized objective row is [@ List($obj_row_normalized ) @] - -The correction coeff are [@ List($correction_coeff) @] - -The current coeff are [@ List($current_coeff) @] - -Print the current total tableau for the basis [@ Matrix($t->{basis}) @]: - -$t->current_tableau [`[$c_4]`] - -Here is the decorated version of the total tableau: - -[`[@ lp_display_mm($c_4, top_labels=>[qw(x1 x2 x3 x4 x5 x6 x7 w b)], -side_labels=>['\text{constraintA}', '', '\text{constraintC}', '\text{constraintD}', -'\text{objective_function}'])@]`] - - END_PGML +# BEGIN_PGML +# B is [`[$t->{current_basis_matrix}]`] with determinant [$t->{current_basis_matrix}->det] +# +# the objective row is [@ $t->{obj_row} @] +# +# +# the coefficients associated with the basis are [@ List(@$c_B) @] +# +# the vector version of these coefficients is [$c_B2] +# +# the normalized objective row is [@ List($obj_row_normalized ) @] +# +# The correction coeff are [@ List($correction_coeff) @] +# +# The current coeff are [@ List($current_coeff) @] +# +# Print the current total tableau for the basis [@ Matrix($t->{basis}) @]: +# +# $t->current_tableau [`[$c_4]`] +# +# Here is the decorated version of the total tableau: +# +# [`[@ lp_display_mm($c_4, top_labels=>[qw(x1 x2 x3 x4 x5 x6 x7 w b)], +# side_labels=>['\text{constraintA}', '', '\text{constraintC}', '\text{constraintD}', +# '\text{objective_function}'])@]`] +# +# +# END_PGML + TEXT("array reference", pretty_print( convert_to_array_ref($c_4) )); ENDDOCUMENT(); diff --git a/t/matrix_tableau_tests/test_tableau_more.pl b/t/matrix_tableau_tests/test_tableau_more.pl index 7f486f1743..a14cbbc137 100755 --- a/t/matrix_tableau_tests/test_tableau_more.pl +++ b/t/matrix_tableau_tests/test_tableau_more.pl @@ -33,6 +33,9 @@ sub WARN_MESSAGE{ warn("WARN MESSAGE: ", @_); } +sub DEBUG_MESSAGE{ + warn("DEBUG MESSAGE: ", @_); +} Context("Matrix"); Context()->flags->set( @@ -83,6 +86,17 @@ sub WARN_MESSAGE{ ok (defined($tableau1), 'tableau has been defined and loaded'); is (ref($tableau1), "Tableau", 'has type Tableau' ); +# test "close_enough_to_zero" subroutine +is $tableau1->close_enough_to_zero(0), 1, "checking_close_enough to zero"; +is $tableau1->close_enough_to_zero(E-9), 0, "checking_close_enough to zero"; +is $tableau1->close_enough_to_zero(-E-5), 0, "checking_close_enough to zero"; +is $tableau1->close_enough_to_zero(-E-10), 0, "checking_close_enough to zero"; +is $tableau1->close_enough_to_zero(E-10), 0, "checking_close_enough to zero"; +is $tableau1->close_enough_to_zero(0.9999E-10), 1, "checking_close_enough to zero"; +is $tableau1->close_enough_to_zero(-0.9999E-10), 1, "checking_close_enough to zero"; + + + print "stringify tableau: ", $tableau1, "\n"; From be1963d760a9e86c9d462fc53bc44022de6c18cd Mon Sep 17 00:00:00 2001 From: Michael Gage Date: Thu, 16 Nov 2017 13:48:51 -0500 Subject: [PATCH 24/30] Update unit tests. --- t/matrix_tableau_tests/matrix_data_tests.pl | 59 ++++++++ t/matrix_tableau_tests/matrix_view1.pg | 109 ++++++++++++++ t/matrix_tableau_tests/predict_dual1.pg | 139 ++++++++++++++++++ t/matrix_tableau_tests/tableau_primal_dual.t | 96 ++++++++++++ t/matrix_tableau_tests/tableau_unit_tests3.pl | 110 ++++++++++++++ .../pg_uses_cmplx_cmp.list.save | 26 ++++ .../test_multiplication_large_numbers.pg | 76 ++++++++++ 7 files changed, 615 insertions(+) create mode 100755 t/matrix_tableau_tests/matrix_data_tests.pl create mode 100644 t/matrix_tableau_tests/matrix_view1.pg create mode 100644 t/matrix_tableau_tests/predict_dual1.pg create mode 100755 t/matrix_tableau_tests/tableau_primal_dual.t create mode 100755 t/matrix_tableau_tests/tableau_unit_tests3.pl create mode 100644 t/pg_test_problems/pg_uses_cmplx_cmp.list.save create mode 100644 t/pg_test_problems/test_multiplication_large_numbers.pg diff --git a/t/matrix_tableau_tests/matrix_data_tests.pl b/t/matrix_tableau_tests/matrix_data_tests.pl new file mode 100755 index 0000000000..bc5a2ccff9 --- /dev/null +++ b/t/matrix_tableau_tests/matrix_data_tests.pl @@ -0,0 +1,59 @@ +#!/usr/bin/perl -w + +use v5.12.3; +use strict; + +use lib "/Volumes/WW_test/opt/webwork/pg_2014/lib"; +use lib "/Volumes/WW_test/opt/webwork/pg_2014/macros"; +use lib "/Volumes/WW_test/opt/webwork/webwork2/lib"; + +use Test::More qw(no_plan); +use Test::Exception; +use Parser; +use Value; +use Class::Accessor; +use PGcore; + +#require "PG.pl"; +#require "PGbasicmacros.pl"; +#require "PGauxiliaryFunctions.pl"; +require "tableau.pl"; +require "Value.pl"; #gives us Real() etc. +#require "Parser.pl"; #gives us Context() but also uses loadMacros(); +#require "niceTables.pl"; +#require "Matrix.pl"; + +our %context; + +sub Context { + Parser::Context->current(\%context,@_) +} + +unless (%context && $context{current}) { + # ^variable our %context + %context = (); # Locally defined contexts, including 'current' context + # ^uses Context + Context(); # Initialize context (for persistent mod_perl) +} + +sub WARN_MESSAGE{ + warn("WARN MESSAGE: ", @_); +} + +Context("Matrix"); + +Context()->flags->set( + zeroLevel=>1E-5, + zeroLevelTol=>1E-5 + ); + + my $m = [[Real(3),4,6],[2,1,5],[4,2,1],[1,4,-5]]; + my $matrix = Matrix($m); + say "matrix ",$matrix; + say "matrix-data ",$matrix->data; + say "matrix-data expanded ", map {$_} @{$matrix->data}; + say "matrix-data refs ", map {ref($_)} @{$matrix->data}; +say "matrix-data data ", join " ", map {'|'.join(' ',$_->value).'|'} @{$matrix->data}; + say "matrix-data value ", join " ", map {'|'.join(' ',$_->value).'|'} @{$matrix->data}; + say "matrix-value ", join ", ", map {'['.(join ' ', @$_).']'} $matrix->value; +done_testing(); diff --git a/t/matrix_tableau_tests/matrix_view1.pg b/t/matrix_tableau_tests/matrix_view1.pg new file mode 100644 index 0000000000..d235be2773 --- /dev/null +++ b/t/matrix_tableau_tests/matrix_view1.pg @@ -0,0 +1,109 @@ +############################################## +DOCUMENT(); + +loadMacros( + "PGstandard.pl", # Standard macros for PG language + "MathObjects.pl", + "parserLinearInequality.pl", + "PGML.pl", + "tableau.pl", + "niceTables.pl", + "PGmatrixmacros.pl", + "contextFraction.pl", + "parserPopUp.pl", + "PGessaymacros.pl", + #"LinearProgramming.pl", + #"source.pl", # allows code to be displayed on certain sites. + "PGcourse.pl", +); + +############################################## + + +Context("LinearInequality"); +Context()->variables->add(x1=>'Real',x2=>'Real', x3=>'Real'); +Context()->variables->add(y1=>'Real',y2=>'Real', y3=>'Real'); +Context()->flags->set(reduceFractions=>0); +# change tolerances to simplify "close to zero" values +Context()->flags->set( + zeroLevel=>1E-8, + zeroLevelTol=>1E-8 + ); + +# taken from Old McDonald problem and modifed slightly +$land = 1100; #acres + +$preservation_costs = 1; #dollars per acre +$preservation_revenue = 30; # dollars per acre +$preservation_profits = $preservation_revenue - $preservation_costs; +$preservation_hours = 12; # hours of labor per acre + +$farming_costs = 50; #dollars per acre +$farming_revenue= 190; #dollars per acre +$farming_profits = $farming_revenue - $farming_costs; # profits +$farming_hours = 240; # hours of labor per acre + + +$development_costs = 85; # dollars per acre in permits +$development_revenue = 290; # dollars per acre +$development_profits = $development_revenue - $development_costs; +$development_hours = 180; # hours of labor per acre + +$capital = 40E03; #dollars in capital available +$workers = 75; +$work_hours = 2E03; #hours of labor per worker + +$total_hours = $workers*$work_hours; + + +$A = Matrix([[1,1,1], + [$preservation_costs, $farming_costs, $development_costs], + [$preservation_hours,$farming_hours, $development_hours]]); +$b = Matrix([$land,$capital,$total_hours]); +$c = Matrix([($preservation_profits), + ($farming_profits), + ($development_profits) + ]); +$tableau=Tableau->new(A=>$A, b=>$b->transpose, c=>$c); +$primal_align ='|ccc|ccc|c|c|'; +$primal_toplevel = [qw(x1 x2 x3 x4 x5 x6 w b)]; + + +$tableau2 = $tableau->copy; +$tableau2->current_tableau(2,3,5); +($m, $n)=($tableau->m,$tableau->n); + +$B = $tableau2->current_basis_matrix; +$Bp = abs($B->det)*($B)**(-1); + +BEGIN_PGML +Given this initial tableau: + + [@ lop_display($tableau, align=>$primal_align,toplevel=>$primal_toplevel) @]* + + after several pivots we arrive at this tableau: + + [@ lop_display($tableau2, align=>$primal_align,toplevel=>$primal_toplevel) @]* + +This is obtained by multiplying the tableau (omitting the objective function row) +by the inverse of a +[$m] by [$m] submatrix [`B`] and then multiplying by the determinant of [`B`] in order to clear the denominator +from fractions. + +Enter the 'basis matrix' [`B`]: +>>[@ ANS($B->cmp),$B->ans_array @]*<< + +Enter the multiplying matrix [`|\det B|B^{-1}`] +>>[@ ANS($Bp->cmp),$Bp->ans_array @]*<< + +Note: From the definition of the determinant one can obtain the formula +[`adj(B)*B= \det(B)I`]. If [`B`] is integer valued then so is [`adj(B)`] because +the adjugate of [`B`] is calculated from the determinants of the [$m]-1 square +submatrices and is a polynomial in the entries of [`B`]. + +[@ htmlLink('https://en.wikipedia.org/wiki/Adjugate_matrix','wikipedia definition of Adjugate Matrix') @]* + +END_PGML + + +ENDDOCUMENT(); diff --git a/t/matrix_tableau_tests/predict_dual1.pg b/t/matrix_tableau_tests/predict_dual1.pg new file mode 100644 index 0000000000..c71caff8fd --- /dev/null +++ b/t/matrix_tableau_tests/predict_dual1.pg @@ -0,0 +1,139 @@ +############################################## +DOCUMENT(); + +loadMacros( + "PGstandard.pl", # Standard macros for PG language + "MathObjects.pl", + "parserLinearInequality.pl", + "PGML.pl", + "tableau.pl", + "niceTables.pl", + "PGmatrixmacros.pl", + "contextFraction.pl", + "parserPopUp.pl", + "PGessaymacros.pl", + #"LinearProgramming.pl", + #"source.pl", # allows code to be displayed on certain sites. + "PGcourse.pl", +); + +############################################## + +Context("Numeric"); + + +# # change tolerances to simplify "close to zero" values +Context()->flags->set( + zeroLevel=>1E-8, + zeroLevelTol=>1E-8 + ); +# +# taken from Old McDonald problem and modified slightly +$land = 1100; #acres + +$preservation_costs = 1; #dollars per acre +$preservation_revenue = 30; # dollars per acre +$preservation_profits = $preservation_revenue - $preservation_costs; +$preservation_hours = 14; # hours of labor per acre + +$farming_costs = 60; #dollars per acre +$farming_revenue= 190; #dollars per acre +$farming_profits = $farming_revenue - $farming_costs; # profits +$farming_hours = 240; # hours of labor per acre + + +$development_costs = 85; # dollars per acre in permits +$development_revenue = 290; # dollars per acre +$development_profits = $development_revenue - $development_costs; +$development_hours = 160; # hours of labor per acre + +$capital = 30E03; #dollars in capital available +$workers = 70; +$work_hours = 2E03; #hours of labor per worker + +$total_hours = $workers*$work_hours; + + +$A = Matrix([[1,1,1], + [$preservation_costs, $farming_costs, $development_costs], + [$preservation_hours,$farming_hours, $development_hours]]); +$b = Matrix([$land,$capital,$total_hours]); +$c = Matrix([($preservation_profits), + ($farming_profits), + ($development_profits) + ]); +$tableau=Tableau->new(A=>$A, b=>$b->transpose, c=>$c); +$primal_align ='|ccc|ccc|c|c|'; +$primal_toplevel = [qw(x1 x2 x3 x4 x5 x6 w b)]; + + +( $tableau2, $num_of_pivots) = phase2_solve($tableau); +($m, $n)=($tableau->m,$tableau->n); + +($x1, $x2, $x3, $x4, $x5, $x6,$w)= @{$final_state = $tableau2->statevars}; +@optc=($optc1, $optc2, $optc3, $optc4, $optc5, $optc6, $basis_coeff, $optb) = $tableau2->current_objective_coeffs->value; + +@dual_y= map {$_/$basis_coeff} @optc; + +$y1=$dual_y[$n]; +$y2=$dual_y[1+$n]; +$y3=$dual_y[2+$n]; +$z =$dual_y[4+$n]; + +$optimum_lop_parameter_columns = List((Set(1..($n+$m))- Set($tableau2->basis))->value); +$optimum_dual_lop_basis_columns = List(1,2,5); +# how to best calculate the complementary columns? FIXME + +BEGIN_PGML +Given this initial tableau: + + [@ lop_display($tableau, align=>$primal_align,toplevel=>$primal_toplevel) @]* + + after several pivots we arrive at the final optimal tableau: + + [@ lop_display($tableau2, align=>$primal_align,toplevel=>$primal_toplevel) @]* + + The optimum values for the problem variables are: + +x1 = [_________]{$x1} +x2 = [_________]{$x2} +x3 is[_________]{$x3} +w is [_________]{$w} + +The optimum values for the dual LOP will be + +y1 = [_________]{$y1} +y2 = [_________]{$y2} +y3 = [_________]{$y3} +z = [_________]{$z} + + +From the complementary slack theorem we know that the optimums of an LOP and its +dual are 'primal-dual' pairs. + +What are the basis columns of the optimum tableau for the LOP (enter a list of numbers): +[__________]{List($tableau2->basis)} + +What are the parameter columns of the optimum tableau for the LOP? +[__________]{$optimum_lop_parameter_columns} + +What are the basis columns for the optimum tableau for the dual LOP? +[__________]{$optimum_dual_lop_basis_columns} + + +END_PGML + +# # --test statements for debugging +# BEGIN_PGML +# c1 is [$optc1] c2 is [$optc2] c3 is [$optc3] c4 is [$optc4] c5 is [$optc5] +# c6 is [$optc6] basis_coeff is [$basis_coeff] and optb is [$optb] +# +# [@ join(" ", @dual_y) @] +# +# final state: [@List($tableau2->statevars)@] +# +# test large values optb [__________________]{$optb} +# +# END_PGML + +ENDDOCUMENT(); diff --git a/t/matrix_tableau_tests/tableau_primal_dual.t b/t/matrix_tableau_tests/tableau_primal_dual.t new file mode 100755 index 0000000000..c1b68c1f12 --- /dev/null +++ b/t/matrix_tableau_tests/tableau_primal_dual.t @@ -0,0 +1,96 @@ +#!/usr/bin/perl -w + + +=pod + +Test primal-dual pairing subroutines + + + + +=cut +use lib "/Volumes/WW_test/opt/webwork/pg_2014/lib"; +use lib "/Volumes/WW_test/opt/webwork/pg_2014/macros"; +#!/usr/bin/perl -w + +use lib "/Volumes/WW_test/opt/webwork/pg_2014/lib"; +use lib "/Volumes/WW_test/opt/webwork/pg_2014/macros"; +use lib "/Volumes/WW_test/opt/webwork/webwork2/lib"; + + +use Test::More; +use Test::Exception; +use Parser; +use Value; +use Class::Accessor; +use PGcore; + +#require "PG.pl"; +#require "PGbasicmacros.pl"; +#require "PGauxiliaryFunctions.pl"; +require "tableau.pl"; +require "Value.pl"; #gives us Real() etc. +#require "Parser.pl"; #gives us Context() but also uses loadMacros(); +#require "niceTables.pl"; + + + +sub Context {Parser::Context->current(\%context,@_)} +unless (%context && $context{current}) { + # ^variable our %context + %context = (); # Locally defined contexts, including 'current' context + # ^uses Context + Context(); # Initialize context (for persistent mod_perl) +} + +sub WARN_MESSAGE{ + warn("WARN MESSAGE: ", @_); +} + +sub DEBUG_MESSAGE{ + warn("DEBUG MESSAGE: ", @_); +} +Context("Matrix"); + +Context()->flags->set( + zeroLevel=>1E-5, + zeroLevelTol=>1E-5 + ); + + $A = Real(.0000005); + $B = Real(0); + + is($A, $B, "test zeroLevel tolerance"); + ok($A==$B, "test zeroLevel tolerance with ok"); + + +$a11 = 1; $a12 = 0; +$a21 = 0; $a22 = 2; +$a31 = 3; $a32 =1; +$b1= 4; $b2=12; $b3=18; +$c1 = -3; $c2=-5; + +$A = Matrix([[$a11, $a12], + [ $a21, $a22], + [ $a31, $a32]]); +$b = Vector([$b1, $b2, $b3]); # need vertical vector +$c = ColumnVector([$c1, $c2]); + + + +$tableau = Tableau->new(A=>$A, b=>$b, c=>$c); +# slack variables are automatically added + + +#$toprow = [qw(y1 y2 y3 y4 s1 s2 P b) ]; +#$align = "cccc|cc|c|c"; + +diag($tableau->current_tableau); +diag("m (constraints) is ", $tableau->m); +diag("n (variables) is ", $tableau->n); +is_deeply( [$tableau->primal2dual(1,2,3,4,5 )], [4,5,1,2,3], "primal to dual pairing, m=3, n=2"); +is_deeply( [$tableau->dual2primal(1,2,3,4,5 )], [3,4,5,1,2], "dual to primal pairing, m=3, n=2"); +is_deeply([$tableau->dual2primal($tableau->primal2dual(1,2,3,4,5))],[1,2,3,4,5], "primal to dual to primal"); +is_deeply([$tableau->primal2dual(4,5)],[2,3], "test primary dual basis switch"); +diag($tableau->primal2dual(4,5)); +done_testing(); diff --git a/t/matrix_tableau_tests/tableau_unit_tests3.pl b/t/matrix_tableau_tests/tableau_unit_tests3.pl new file mode 100755 index 0000000000..9ac60e0843 --- /dev/null +++ b/t/matrix_tableau_tests/tableau_unit_tests3.pl @@ -0,0 +1,110 @@ +#!/usr/bin/perl -w + +use lib "/Volumes/WW_test/opt/webwork/pg_2014/lib"; +use lib "/Volumes/WW_test/opt/webwork/pg_2014/macros"; +use lib "/Volumes/WW_test/opt/webwork/webwork2/lib"; + +use Test::More; +use Test::Exception; +use Parser; +use Value; +use Class::Accessor; +use PGcore; + +#require "PG.pl"; +#require "PGbasicmacros.pl"; +#require "PGauxiliaryFunctions.pl"; +require "tableau.pl"; +require "Value.pl"; #gives us Real() etc. +#require "Parser.pl"; #gives us Context() but also uses loadMacros(); +#require "niceTables.pl"; + + + +sub Context {Parser::Context->current(\%context,@_)} +unless (%context && $context{current}) { + # ^variable our %context + %context = (); # Locally defined contexts, including 'current' context + # ^uses Context + Context(); # Initialize context (for persistent mod_perl) +} + +sub WARN_MESSAGE{ + warn("WARN MESSAGE: ", @_); +} + +sub DEBUG_MESSAGE{ + warn("DEBUG MESSAGE: ", @_); +} +Context("Matrix"); + +Context()->flags->set( + zeroLevel=>1E-5, + zeroLevelTol=>1E-5 + ); + + $A = Real(.0000005); + $B = Real(0); + + is($A, $B, "test zeroLevel tolerance"); + ok($A==$B, "test zeroLevel tolerance with ok"); + +$land = 1000; #acres + +$preservation_costs = 1; #dollars per acre +$preservation_revenue = 30; # dollars per acre +$preservation_profits = $preservation_revenue - $preservation_costs; +$preservation_hours = 12; # hours of labor per acre + +$farming_costs = 50; #dollars per acre +$farming_revenue= 190; #dollars per acre +$farming_profits = $farming_revenue - $farming_costs; # profits +$farming_hours = 240; # hours of labor per acre + + +$development_costs = 85; # dollars per acre in permits +$development_revenue = 290; # dollars per acre +$development_profits = $development_revenue - $development_costs; +$development_hours = 180; # hours of labor per acre + +$capital = 40E03; #dollars in capital available +$workers = 75; +$work_hours = 2E03; #hours of labor per worker + +$total_hours = $workers*$work_hours; + +$A = Matrix([[1,1,1], + [$preservation_costs, $farming_costs, $development_costs], + [$preservation_hours,$farming_hours, $development_hours]]); +$b = Matrix([$land,$capital,$total_hours]); +$c = Matrix([($preservation_profits), + ($farming_profits), + ($development_profits) + ]); +$tableau=Tableau->new(A=>$A, b=>$b->transpose, c=>$c); +$primal_align ='|ccc|ccc|cc|'; +$primal_toplevel = [qw(x1 x2 x3 x4 x5 x6 w b)]; + + + + +#### problem starts here: + +$tableau->basis(1,2,5); +diag($tableau->current_tableau); +is_deeply $tableau->current_tableau, '[[228,0,60,240,0,-1,0,90000],[0,228,168,-12,0,1,0,138000],[0,0,10920,360,228,-49,0,2.13E+06],[0,0,-21480,5280,0,111,228,2.193E+07]]', "check initial state"; + +diag($tableau->find_pivot_column('max'), "next pivot column"); +is_deeply [$tableau->find_pivot_column('max')], [3, -21480,0], "find next pivot column"; +diag($tableau->find_pivot_row(3), "next pivot row"); +is_deeply [$tableau->find_pivot_row(3)], [3, 195.055,0], "find pivot row"; +diag($tableau->find_next_pivot('max'), "next pivot"); +is_deeply [$tableau->find_next_pivot('max')], [3,3,0,0], "find next pivot"; +diag($tableau->find_next_basis_from_pivot(3,3), "next basis from pivot"); +diag("current basis is ", $tableau->basis); +diag("leaving column is " , $tableau->find_leaving_column(3) ); +diag($tableau->find_next_basis('max'), "find next basis"); +is_deeply [$tableau->find_next_basis('max')], [1,2,3,undef], "find next basis"; + + +done_testing(); \ No newline at end of file diff --git a/t/pg_test_problems/pg_uses_cmplx_cmp.list.save b/t/pg_test_problems/pg_uses_cmplx_cmp.list.save new file mode 100644 index 0000000000..8e3d4404db --- /dev/null +++ b/t/pg_test_problems/pg_uses_cmplx_cmp.list.save @@ -0,0 +1,26 @@ +/Volumes/WW_test/opt/webwork/libraries/webwork-open-problem-library/OpenProblemLibrary/NAU/EE/ee188/irwin.08.011.pg +/Volumes/WW_test/opt/webwork/libraries/webwork-open-problem-library/OpenProblemLibrary/NAU/EE/ee188/irwin.08.019.pg +/Volumes/WW_test/opt/webwork/libraries/webwork-open-problem-library/OpenProblemLibrary/NAU/EE/ee188/irwin.08.038.pg +/Volumes/WW_test/opt/webwork/libraries/webwork-open-problem-library/OpenProblemLibrary/NAU/EE/ee188/irwin.08.047.pg +/Volumes/WW_test/opt/webwork/libraries/webwork-open-problem-library/OpenProblemLibrary/NAU/EE/ee188/irwin.08.076.pg +/Volumes/WW_test/opt/webwork/libraries/webwork-open-problem-library/OpenProblemLibrary/NAU/EE/ee188/irwin.08.084.pg +/Volumes/WW_test/opt/webwork/libraries/webwork-open-problem-library/OpenProblemLibrary/NAU/EE/ee188/irwin.09.058.pg +/Volumes/WW_test/opt/webwork/libraries/webwork-open-problem-library/OpenProblemLibrary/Rochester/setAlgebra11ComplexNumbers/Divide.pg +/Volumes/WW_test/opt/webwork/libraries/webwork-open-problem-library/OpenProblemLibrary/Rochester/setAlgebra11ComplexNumbers/Sqrt.pg +/Volumes/WW_test/opt/webwork/libraries/webwork-open-problem-library/OpenProblemLibrary/Rochester/setAlgebra11ComplexNumbers/ur_cn_1_1.pg +/Volumes/WW_test/opt/webwork/libraries/webwork-open-problem-library/OpenProblemLibrary/Rochester/setAlgebra11ComplexNumbers/ur_cn_1_14.pg +/Volumes/WW_test/opt/webwork/libraries/webwork-open-problem-library/OpenProblemLibrary/Rochester/setAlgebra11ComplexNumbers/ur_cn_1_33.pg +/Volumes/WW_test/opt/webwork/libraries/webwork-open-problem-library/OpenProblemLibrary/Rochester/setAlgebra11ComplexNumbers/ur_cn_1_6.pg +/Volumes/WW_test/opt/webwork/libraries/webwork-open-problem-library/OpenProblemLibrary/Rochester/setAlgebra34Matrices/determinant_2x2a.pg +/Volumes/WW_test/opt/webwork/libraries/webwork-open-problem-library/OpenProblemLibrary/Rochester/setComplexNumbers/ur_cn_1_13.pg +/Volumes/WW_test/opt/webwork/libraries/webwork-open-problem-library/OpenProblemLibrary/Rochester/setComplexNumbers/ur_cn_1_14.pg +/Volumes/WW_test/opt/webwork/libraries/webwork-open-problem-library/OpenProblemLibrary/Rochester/setComplexNumbers/ur_cn_1_2.pg +/Volumes/WW_test/opt/webwork/libraries/webwork-open-problem-library/OpenProblemLibrary/Rochester/setComplexNumbers/ur_cn_1_32.pg +/Volumes/WW_test/opt/webwork/libraries/webwork-open-problem-library/OpenProblemLibrary/Rochester/setComplexNumbers2AnalyticFunctions/ur_cn_2_10.pg +/Volumes/WW_test/opt/webwork/libraries/webwork-open-problem-library/OpenProblemLibrary/Rochester/setComplexNumbers2AnalyticFunctions/ur_cn_2_11.pg +/Volumes/WW_test/opt/webwork/libraries/webwork-open-problem-library/OpenProblemLibrary/Rochester/setComplexNumbers2AnalyticFunctions/ur_cn_2_3.pg +/Volumes/WW_test/opt/webwork/libraries/webwork-open-problem-library/OpenProblemLibrary/Rochester/setComplexNumbers2AnalyticFunctions/ur_cn_2_4.pg +/Volumes/WW_test/opt/webwork/libraries/webwork-open-problem-library/OpenProblemLibrary/Rochester/setComplexNumbers2AnalyticFunctions/ur_cn_2_5.pg +/Volumes/WW_test/opt/webwork/libraries/webwork-open-problem-library/OpenProblemLibrary/Rochester/setComplexNumbers2AnalyticFunctions/ur_cn_2_6.pg +/Volumes/WW_test/opt/webwork/libraries/webwork-open-problem-library/OpenProblemLibrary/Rochester/setComplexNumbers2AnalyticFunctions/ur_cn_2_7.pg +/Volumes/WW_test/opt/webwork/libraries/webwork-open-problem-library/OpenProblemLibrary/Rochester/setLinearAlgebra1Systems/ur_la_1_2.pg diff --git a/t/pg_test_problems/test_multiplication_large_numbers.pg b/t/pg_test_problems/test_multiplication_large_numbers.pg new file mode 100644 index 0000000000..b6d2e76050 --- /dev/null +++ b/t/pg_test_problems/test_multiplication_large_numbers.pg @@ -0,0 +1,76 @@ +##DESCRIPTION + + + +##ENDDESCRIPTION + + +DOCUMENT(); # This should be the first executable line in the problem. + +loadMacros( + "PGstandard.pl", # Standard macros for PG language + "MathObjects.pl", + #"source.pl", # used to display problem source button + "PGcourse.pl", # Customization file for the course +); + +TEXT(beginproblem()); +$showPartialCorrectAnswers = 1; + +############################################################## +# +# Setup +# +# +Context("Numeric"); + +############################################################## +# +# Text +# +# + +Context("Matrix")->texStrings; +$P = Point(1,2,3); +$V = Vector(1,2,3); +$M = Matrix([1,2],[3,4]); +$a = Real(1230000); +$b = Real(123000); + +sub TEST { + my $test = shift; + my ($result, $error) = PG_restricted_eval($test); + TEXT($error? "$test: $error" : "$test: OK", $BR); +} + +TEST('$a*$P'); +TEST('$P/$a'); +TEST('$a*$V'); +TEST('$V/$a'); +TEST('$a*$M'); +TEST('$M/$a'); + +TEXT($HR); + +TEST('$b*$P'); +TEST('$P/$b'); +TEST('$b*$V'); +TEST('$V/$b'); +TEST('$b*$M'); +TEST('$M/$b'); + + +# Without the patch, all the lines involving $a should produce an error, but the ones with +# $b should be marked OK. With the patch, all the lines should be OK. + + + +############################################################## +# +# Answers +# +# + + + +ENDDOCUMENT(); # This should be the last executable line in the problem. \ No newline at end of file From 3c25b93063695f7947e11c1a67229f6ee72fbd93 Mon Sep 17 00:00:00 2001 From: Michael Gage Date: Thu, 16 Nov 2017 13:49:29 -0500 Subject: [PATCH 25/30] Update primal2dual method. --- macros/tableau.pl | 61 +++++++++++++++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 21 deletions(-) diff --git a/macros/tableau.pl b/macros/tableau.pl index 1316c9007d..59e42abda5 100755 --- a/macros/tableau.pl +++ b/macros/tableau.pl @@ -339,27 +339,7 @@ sub phase1_solve { =cut -$primal2dual = sub { - my $i =$shift; - if ($i<=$n and $i>0) { - return $m +$i; - } elsif ($i > $n and $i<= $n+$m) { - return $i-$n - } else { - Value::Error("index $i is out of range"); - } -}; - -$dual2primal = sub { - my $j=$shift; - if ($j<=$m and $j>0) { - return $n+$j; - } elsif ($j>$m and $j<= $n+$m) { - return $j-$m - }else { - Value::Error("index $i is out of range"); - } -}; + # deprecated for tableaus - use $tableau->statevars instead sub get_tableau_variable_values { @@ -1208,6 +1188,45 @@ sub dual_lop { =cut + +sub primal2dual { + my $self = shift; + my $n = $self->n; + my $m = $self->m; + $p2d_translate = sub { + my $i = shift; + if ($i<=$n and $i>0) { + return $m +$i; + } elsif ($i > $n and $i<= $n+$m) { + return $i-$n + } else { + Value::Error("index $i is out of range"); + } + }; + my @array = @_; + return (map {&$p2d_translate($_)} @array); #accepts list of numbers +} + + +sub dual2primal { + my $self = shift; + my $n = $self->n; + my $m = $self->m; + $d2p_translate = sub { + my $j = shift; + if ($j<=$m and $j>0) { + return $n+$j; + } elsif ($j>$m and $j<= $n+$m) { + return $j-$m + }else { + Value::Error("index $j is out of range"); + } + }; + my @array = @_; + return (map {&$d2p_translate($_)} @array); #accepts list of numbers +} + + package Value::Matrix; sub _Matrix { From 58a6f4db15eeeab7c848a37ee752a8f83b6c705e Mon Sep 17 00:00:00 2001 From: Michael Gage Date: Fri, 17 Nov 2017 17:34:10 -0500 Subject: [PATCH 26/30] Add separate output for quickMatrixEntry when in TeX mode. Simplify some of the names for Tableau methods -- more work probably needs to be done. --- macros/quickMatrixEntry.pl | 17 ++++--- macros/tableau.pl | 91 ++++++++++++++++++++++++++++++++------ 2 files changed, 87 insertions(+), 21 deletions(-) diff --git a/macros/quickMatrixEntry.pl b/macros/quickMatrixEntry.pl index 10150f50c9..7a76bd7147 100755 --- a/macros/quickMatrixEntry.pl +++ b/macros/quickMatrixEntry.pl @@ -12,8 +12,7 @@ sub INITIALIZE_QUICK_MATRIX_ENTRY { return ''; } -# + sub MATRIX_ENTRY_BUTTON { my $answer_number = shift; # warn(" input reference is ". ref($answer_number)); @@ -28,13 +27,17 @@ sub MATRIX_ENTRY_BUTTON { $columns=$columns//5; my $answer_name = "AnSwEr".sprintf('%04d',$answer_number); # warn("answer number $answer_name rows $rows columns $columns"); - return qq! - $PAR - - $PAR!; + return MODES( + HTML => qq!$PAR + + $PAR!, + TeX => qq!$PAR Quick Matrix Entry Button $PAR!, + ); } +1; + our $quick_entry_javascript = <<'END_JS';