File:  [LON-CAPA] / loncom / mupad_utils / units / unit_list_maker.pl
Revision 1.1: download - view: text, annotated - select for diffs
Thu Aug 10 20:17:57 2000 UTC (23 years, 8 months ago) by tyszkabe
Branches: MAIN
CVS tags: version_2_9_X, version_2_9_99_0, version_2_9_1, version_2_9_0, version_2_8_X, version_2_8_99_1, version_2_8_99_0, version_2_8_2, version_2_8_1, version_2_8_0, version_2_7_X, version_2_7_99_1, version_2_7_99_0, version_2_7_1, version_2_7_0, version_2_6_X, version_2_6_99_1, version_2_6_99_0, version_2_6_3, version_2_6_2, version_2_6_1, version_2_6_0, version_2_5_X, version_2_5_99_1, version_2_5_99_0, version_2_5_2, version_2_5_1, version_2_5_0, version_2_4_X, version_2_4_99_0, version_2_4_2, version_2_4_1, version_2_4_0, version_2_3_X, version_2_3_99_0, version_2_3_2, version_2_3_1, version_2_3_0, version_2_2_X, version_2_2_99_1, version_2_2_99_0, version_2_2_2, version_2_2_1, version_2_2_0, version_2_1_X, version_2_1_99_3, version_2_1_99_2, version_2_1_99_1, version_2_1_99_0, version_2_1_3, version_2_1_2, version_2_1_1, version_2_1_0, version_2_12_X, version_2_11_X, version_2_11_4_uiuc, version_2_11_4_msu, version_2_11_4, version_2_11_3_uiuc, version_2_11_3_msu, version_2_11_3, version_2_11_2_uiuc, version_2_11_2_msu, version_2_11_2_educog, version_2_11_2, version_2_11_1, version_2_11_0_RC3, version_2_11_0_RC2, version_2_11_0_RC1, version_2_11_0, version_2_10_X, version_2_10_1, version_2_10_0_RC2, version_2_10_0_RC1, version_2_10_0, version_2_0_X, version_2_0_99_1, version_2_0_2, version_2_0_1, version_2_0_0, version_1_99_3, version_1_99_2, version_1_99_1_tmcc, version_1_99_1, version_1_99_0_tmcc, version_1_99_0, version_1_3_X, version_1_3_3, version_1_3_2, version_1_3_1, version_1_3_0, version_1_2_X, version_1_2_99_1, version_1_2_99_0, version_1_2_1, version_1_2_0, version_1_1_X, version_1_1_99_5, version_1_1_99_4, version_1_1_99_3, version_1_1_99_2, version_1_1_99_1, version_1_1_99_0, version_1_1_3, version_1_1_2, version_1_1_1, version_1_1_0, version_1_0_99_3, version_1_0_99_2, version_1_0_99_1, version_1_0_99, version_1_0_3, version_1_0_2, version_1_0_1, version_1_0_0, version_0_99_5, version_0_99_4, version_0_99_3, version_0_99_2, version_0_99_1, version_0_99_0, version_0_6_2, version_0_6, version_0_5_1, version_0_5, version_0_4, stable_2002_spring, stable_2002_july, stable_2002_april, stable_2001_fall, loncapaMITrelate_1, language_hyphenation_merge, language_hyphenation, conference_2003, bz6209-base, bz6209, bz5969, bz2851, STABLE, PRINT_INCOMPLETE_base, PRINT_INCOMPLETE, HEAD, GCI_3, GCI_2, GCI_1, BZ5971-printing-apage, BZ5434-fox, BZ4492-merge, BZ4492-feature_horizontal_radioresponse
Added the perl file that generates a Mupad readable units file from an easy to read input_file
these files are all that is needed to configure Mupad to handle units correctly.
Ben

#!/usr/bin/perl
#
# 
#
# This program generates a MuPad readable file
# of physical units definitions based from a user
# defined file. At the end of this file and in
# the file unit_list are instructions
# regarding the format of the file unit_list.
#
# To read the file, once in MuPad, use the following cmd:
#     read("units"):
#
#                 --Benjamin Tyszka, 7/2000
#
#######################################

use strict;

my $input_file = "unit_list";
my %prefix = (
	   "Y" => "10^(24)",
	   "Z" => "10^(21)",
	   "E" => "10^(18)",
	   "P" => "10^(15)",
	   "T" => "10^(12)",
	   "G" => "10^(9)",
	   "M" => "10^(6)",
	   "k" => "10^(3)",
	   "h" => "10^(2)",
	   "d" => "10^(-1)",
	   "c" => "10^(-2)",
	   "m" => "10^(-3)",
	   "u" => "10^(-6)",
	   "n" => "10^(-9)",
	   "p" => "10^(-12)",
	   "f" => "10^(-15)",
	   "a" => "10^(-18)",
	   "z" => "10^(-21)",
	   "y" => "10^(-24)",
	  );
my $category;
my %def_units;
my @current_units;

open(UNITLIST, $input_file) or die "Can't open $input_file: $!\n";
open(UNITS, ">units") or die "Can't open units: $!\n";

while(<UNITLIST>) {
    s/\#.*//;                # strip comments
    chomp;                   #
    s/^\s+//;                # strip leading spaces
    s/\s+$//;                # strip ending spaces
    next unless length;      # skip blank lines


    if ( /^(\S+)\s+units/ ) {         # if starting new category of units

	$category=$1;
	$def_units{$category}=[()];

    }
    elsif ( /^(\S+?)(\=\S+|\s+|$)\s*(\S*)/ ) {    # else if definition line

      my $unit=$1; my $def=$2; my $omit=$3;

      if ( $def=~ s/=/:=/ ) {            # definition exists? then print it
	print UNITS "$unit$def;\n";
      }

      print UNITS "protect( $unit );\n";
      push @{ $def_units{$category} }, "$unit";

      unless ( $omit =~ /constant/ ) {      # unless constant, print prefixes
	foreach my $current (keys %prefix) {
	  $_ = $omit;
	  unless (/$current/) {
	    print UNITS "$current$unit := ( $prefix{$current}*$unit ):\nprotect($current$unit);\n";
	    push @{ $def_units{$category} }, "$current$unit";
	  }
	}
      }
    }
    else {
      die("Improperly formatted line");
    }

    print UNITS "\n\n";

}

close UNITLIST;

#########################
#
# Now that it is done defining units we print a mupad function
#   that unassigns units by category.
#
#

print UNITS "unassignunits:=proc(n)\n begin\n";
foreach my $current ( keys %def_units ) {
    print UNITS "if n=\"$current\" then\n";
    print UNITS "unassign( ".join( ", ", @{$def_units{$current}} )."):\n";
    print UNITS "end_if;\n";
}
    print UNITS "end_proc;\n";


close UNITS;


##############################
#
# The format of the input file 'unit_list' is as follows:
#
#
# 1. Units must be broken up into categories. Start each category
#    with the following line:
#
#        name units
#
#    where 'name' is the name of your category (e.g. SI or English).
#    Use no spaces in 'name' of your category.
#
#
# 2. Next, define all units within a category using:
#
#	unit[=expression] [excluded prefixes or 'constant']
#
#    where unit is the name of your units and exptression is what your
#    units are equivalent to.
#    Note that NO spaces can be used in the expression, before/after
#    the '=', or in the excluded prefixes.
#    The only spaces MUST be between the unit and the excluded prefixes (if there
#    are any excluded prefixes)
#
#    'constant' can be used in place of excluded prefixes in order to exclude
#    all prefixes. Here are some examples:
#
#       Example units                 # defines category named 'Example'. The
#                                     #   following will be in that category:
#       s                             # defines seconds
#       hr=(3600*s)                   # defines hours using an expression
#       ft=(12*inch) f                # defines feet. The f excludes fft 
#                                     #   because this
#                                     #   this fft is a function in Mupad
#       c=(2.99*10^8*m/s) constant    # defines the constant speed of light
#
# 3. Blank lines do not effect anything and everything following a '#'
#    is a comment.
#
#  
#
#########################

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>