[system] / trunk / pg / macros / PGbasicmacros.pl Repository: Repository Listing bbplugincoursesdistsnplrochestersystemwww

# Diff of /trunk/pg/macros/PGbasicmacros.pl

Revision 2154 Revision 2155
1574 1574
1575 low level: 1575 low level:
1576 1576
1577 spf(\$number, \$format) # prints the number with the given format 1577 spf(\$number, \$format) # prints the number with the given format
1578 sspf(\$number, \$format) # prints the number with the given format, always including a sign. 1578 sspf(\$number, \$format) # prints the number with the given format, always including a sign.
1579 nicestring(\$coefficients, \$terms) # print a linear combinations of terms using coefficients
1580 nicestring(\$coefficients) # uses the coefficients to make a polynomial
1581 # For example
1582 # nicestring([1,-2, 0]) produces 'x^2-2x'
1583 # nicestring([2,0,-1],['', 't', 't^2']) produces '2-t^2'
1579 protect_underbar(\$string) # protects the underbar (class_name) in strings which may have to pass through TeX. 1584 protect_underbar(\$string) # protects the underbar (class_name) in strings which may have to pass through TeX.
1580 1585
1581=cut 1586=cut
1582 1587
1583sub beginproblem { 1588sub beginproblem {
1604 "(\$problemValue \$points) " 1609 "(\$problemValue \$points) "
1605 ) if (\$problemValue ne ""); 1610 ) if (\$problemValue ne "");
1606 } 1611 }
1607 \$out; 1612 \$out;
1608 1613
1614}
1615
1616sub nicestring {
1617 my(\$thingy) = shift;
1618 my(@coefs) = @{\$thingy};
1619 my \$n = scalar(@coefs);
1620 \$thingy = shift;
1621 my(@others);
1622 if(defined(\$thingy)) {
1623 @others = @{\$thingy};
1624 } else {
1625 my(\$j);
1626 for \$j (1..(\$n-2)) {
1627 \$others[\$j-1] = "x^".(\$n-\$j);
1628 }
1629 if(\$n>=2) { \$others[\$n-2] = "x";}
1630 \$others[\$n-1] = "";
1631 }
1632 my(\$j, \$k)=(0,0);
1633 while((\$k<\$n) && (\$coefs[\$k]==0)) {\$k++;}
1634 if(\$k==\$n) {return("0");}
1635 my \$ans;
1636 if(\$coefs[\$k]==1) {\$ans = (\$others[\$k]) ? "\$others[\$k]" : "1";}
1637 elsif(\$coefs[\$k]== -1) {\$ans = (\$others[\$k]) ? "- \$others[\$k]" : "-1"}
1638 else { \$ans = "\$coefs[\$k] \$others[\$k]";}
1639 \$k++;
1640 for \$j (\$k..(\$n-1)) {
1641 if(\$coefs[\$j] != 0) {
1642 if(\$coefs[\$j] == 1) {
1643 \$ans .= (\$others[\$j]) ? "+ \$others[\$j]" : "+ 1";
1644 } elsif(\$coefs[\$j] == -1) {
1645 \$ans .= (\$others[\$j]) ? "- \$others[\$j]" : "-1";
1646 } else {
1647 \$ans .= "+ \$coefs[\$j] \$others[\$j]";
1648 }
1649 }
1650 }
1651 return(\$ans);
1609} 1652}
1610 1653
1611# kludge to clean up path names 1654# kludge to clean up path names
1612 ## allow underscore character in set and section names and also allows line breaks at / 1655 ## allow underscore character in set and section names and also allows line breaks at /
1613sub protect_underbar { 1656sub protect_underbar {

Legend:
 Removed from v.2154 changed lines Added in v.2155

 aubreyja at gmail dot com ViewVC Help Powered by ViewVC 1.0.9