BEGIN { strict->import }
sub _contextAMatrix_init { context::AMatrix::Init(); return; }
package context::AMatrix;
sub Init {
my $context = $main::context{AMatrix} = Parser::Context->getCopy('Matrix');
$context->{value}{Matrix} = 'context::AMatrix::AMatrix';
$context->{name} = 'AMatrix';
$context->lists->set(
'Matrix' => {
class => 'context::AMatrix::Value',
open => '[',
close => ']',
separator => ','
}
);
main::TEXT(main::MODES(TeX => '', HTML => << 'END_STYLE'));
END_STYLE
return;
}
package context::AMatrix::Value;
our @ISA = qw(Parser::List::Matrix);
sub TeX {
my ($self, $precedence) = @_;
return $self->SUPER::TeX(@_) unless $self->entryType->{entryType};
my ($open, $close) = ($self->{open}, $self->{close});
$open = '\{' if $open eq '{';
$close = '\}' if $close eq '}';
my $TeX = '';
my @entries = ();
my $d;
for my $row (@{ $self->coords }) {
for my $x (@{ $row->coords }) { push(@entries, $x->TeX) }
$TeX .= join(' &', @entries) . '\cr' . "\n";
$d = scalar(@entries);
@entries = ();
}
$TeX = '\begin{array}{' . ($self->{array_template} || ('c' x ($d - 1))) . '|c}' . "\n" . $TeX . '\end{array}';
return $TeX unless $open || $close;
$open = '.' if $close && !$open;
$close = '.' if $open && !$close;
return '\left' . $open . $TeX . '\right' . $close;
}
package context::AMatrix::AMatrix;
our @ISA = qw(Value::Matrix);
sub class { return 'Matrix'; }
sub TeX {
my $self = shift;
my $equation = shift;
my $def = ($equation->{context} || $self->context)->lists->get('Matrix');
my $open = shift || $self->{open} || $def->{open};
my $close = shift || $self->{close} || $def->{close};
$open =~ s/([{}])/\\$1/g;
$close =~ s/([{}])/\\$1/g;
my $TeX = '';
my @entries = ();
my $d;
if ($self->isRow) {
for my $x (@{ $self->data }) {
if (Value::isValue($x)) {
$x->{format} = $self->{format} if defined $self->{format};
push(@entries, $x->TeX($equation));
} else {
push(@entries, $x);
}
}
$TeX .= join(' &', @entries) . "\n";
$d = scalar(@entries);
} else {
for my $row (@{ $self->data }) {
for my $x (@{ $row->data }) {
if (Value::isValue($x)) {
$x->{format} = $self->{format} if defined $self->{format};
push(@entries, $x->TeX($equation));
} else {
push(@entries, $x);
}
}
$TeX .= join(' &', @entries) . '\cr' . "\n";
$d = scalar(@entries);
@entries = ();
}
}
$TeX =~ s/\\cr\n$/\n/;
return '\left' . $open . '\begin{array}{' . ('c' x ($d - 1)) . '|c}' . "\n" . $TeX . '\end{array}\right' . $close;
}
sub EVALUATE {
my @inputs = @_;
return map { (Value::isFormula($_) && $_->isConstant ? $_->eval : $_) } @inputs;
}
sub format_matrix_HTML {
my ($self, $array, @inputs) = @_;
my %options = (open => '', close => '', sep => '', tth_delims => 0, @inputs);
$self->{format_options} = [%options] unless $self->{format_options};
my ($open, $close, $sep) = ($options{open}, $options{close}, $options{sep});
my ($rows, $cols) = (scalar(@{$array}), scalar(@{ $array->[0] }));
my $HTML = '';
my $class = 'class="ans_array_cell"';
my $cell = 'display:table-cell;vertical-align:middle;';
my $pad = 'padding:4px 0;';
if ($sep) { $sep = '' . $sep . '' }
else { $sep = '' }
$sep = '' . $sep . '';
if ($options{top_labels}) {
$HTML .=
''
. join($sep, @{ $options{top_labels} })
. '';
}
for (0 .. $rows - 1) {
$HTML .=
''
. join($sep, EVALUATE(@{ $array->[$_] }))
. '';
}
$HTML =
''
. $HTML
. '';
$open = $self->format_delimiter($open, $rows, $options{tth_delims});
$close = $self->format_delimiter($close, $rows, $options{tth_delims});
if ($open ne '' || $close ne '') {
my $delim = 'display:inline-block; vertical-align:middle;';
$HTML =
''
. $open
. ''
. $HTML
. ''
. $close
. '';
}
return ''
. $HTML
. '';
}
sub format_matrix_tex {
my ($self, $array, %extra_options) = @_;
my %options = (open => '.', close => '.', sep => '', %extra_options);
$self->{format_options} = [%options] unless $self->{format_options};
my ($open, $close, $sep) = ($options{open}, $options{close}, $options{sep});
my ($rows, $cols) = (scalar(@{$array}), scalar(@{ $array->[0] }));
my $tex = '';
my @rows = ();
$open = '\\' . $open if $open =~ m/[{}]/;
$close = '\\' . $close if $close =~ m/[{}]/;
$tex .= '\(\left' . $open;
if ($sep) {
$tex .= '\setlength{\arraycolsep}{2pt}';
$sep = '\,' . $sep;
}
$tex .= '\begin{array}{' . ('c' x ($cols - 1) . '|c') . '}';
if ($options{top_labels}) { push @rows, join($sep . '&', @{ $options{top_labels} }); }
for (0 .. $rows - 1) { push(@rows, join($sep . '&', @{ $array->[$_] })) }
$tex .= join('\cr' . "\n", @rows);
$tex .= '\end{array}\right' . $close . '\)';
return $tex;
}
1;