1. Express YourselfCharles Wright 1970 (before NWA and Madonna): Ox Syntax for Calculations
    You can write Ox programs to perform calculations like a calculator (or a spreadsheet cell formula) would. Of course, the point of a general purpose mathematical language like Ox is to code elaborate calculations that no calculator could. But understanding basic arithmetic operations available to you in a programming language is the first step toward economic computations.

  1. Calculator Expressions
  2. Consider a mathematical expression:
    $$ { y^2 + \log(z) \over e^{{(3 - yz)}^{-2}} }\tag{A}\label{A}$$ On a calculator you could key this expression as long as you had values to assign to the variables $y$ and $z$. But there would be limitations: you cannot put one expression "over" another to represent division. You would need to use "$/$" to do this. And to compute $yz$ you must use "$*$" or "$\times$". And you would use ^ to represent $y^2$.

    Another issue is how to implement functions like $\log(x)$ and $e^x$. Typically these are implemented not as operators (like ^) but as named functions. In the case of Ox and nearly every other language the corresponding functions are log(x) and exp(x).

    With those concerns in mind, a naive translation of \eqref{A} might look this:
    (A).1     y^2  + log(z) / exp( (3-y*z)^(-2) ) 
    Extra whitespace has been put in the formula to make it easier to read. Remember two or more spaces are the same as one space in Ox, and spaces inside expressions are not important. They could all be removed from (A) and the result would be the same. Also, note that this is an Ox expression; it is not a statement yet because there is no ; to end it. And the expression would almost certainly be surrounded by more code to, for example, store the results in a variable or to print the result to the screen.

    To evaluate an expression the calculator or Ox must parse it. For the most part it will parse expressions the way you learn in math. Parsing starts at the left and mostly (moving) right. This requires that operators are carried out in an order. As Ox scans left-to-right it will decide which things get done first then second, etc.

    Table 2. Basic Arithmetic Operators in Ox

    #   Class          Symbols      Note
    -----------------------------------------------
    1   grouping        ()  []
    2   power           ^
    3   signs           - +         right to left
    4   multiplicative  * /
    5   additive        + -
    6   assignment      =           right-to-left
    
    Operators are listed in their order of precedence. There are several other kinds of operators in Ox discussed later. These are the basic ones.

    A scientific calculator can use brackets to group values, and you could compute expression (A) in Ox as:
    (A).2      (y^2+ln(z)) / exp( (3-y*z)^-2)
    
    For the most part, this order is exactly as you would expect from the way we write math on paper. For example, when a left bracket "(" is found in the expression, the matching right bracket will be found and then everything inside the bracket will be evaluated before proceeding. On the other hand, when evaluating x*y+z*w the $*$ comes before $+$ so x*y would be computed first. Then since $*$ has higher priority, z*w is computed before it is added to xy.

    Just as in ordinary math, if you wanted y+z to be computed you would have to move it up in precedence by writing x*(y+z)*w. In the case of x*y^z*w we see from the table that $\^$ comes before $*$ in precedence, thus x*y^z*w = x*(y^z)*w not (x*y)^z*w. But it is a bit dangerous to rely on the precedence in this case since you have to be very confident that ^ comes first. I usually err on the safe side and write x*(y^z)*w rather than relying on operator precedence.

    Notice that = is just a low precedent operator (#6 in the table above). Consider two Ox statements:
    1.     x = y+5;
    2.     x + y;
    
    These are not completely different in terms of syntax. Both are valid statements in Ox. And from the table we know that with x = y+5; that + is completed before =. So y+5 will happen and the result will be assigned to x.

    There is one thing that is very special about =, however. Namely, the left side of it must be some physical location to store the information on the right. So although 4+5; is not an error in Ox, 4=5; is. That is because 4 is not a thing. It is not a physical spot in your computer memory which can store the value 5. So it is not a left value or lvalue.

    Ox has several more useful operators to help you code sophisticated mathematical procedures. Further, Ox operators work on matrices. Most non-mathematical languages do not get matrices, so this is likely new to you even if you have done some programming before. The ability to process matrices in complex ways is one of the most important aspects of Ox, because it makes your code simpler and more efficient. However, like any powerful tool, these extended operators can create errors if you do not understand them before using them in your code. These are discussed later.

  3. Indexing matrices, strings and arrays
  4. To get to elements of a matrix and other things with more than one value (including strings and arrays) your code has to include the index to the value. A matrix has two indices, as in $a_{ij}$ being the element of a matrix $A$ in the ith row and jth column. Ox does the same thing, except there is no way to use subscripts in a plain text program. And if you just wrote Aij that looks like an identifier. Instead, each index must be put inside square brackets, so A[i][j].

    Ox is like many languages in that indexing always starts at zero. So the top-left element of a matrix is indexed with [0][0]. A 2 × 3 matrix has elements:
         [0][0]   [0][1]   [0][2]
    
         [1][0]   [1][1]   [1][2]
    
    A vector is a matrix for which one of the dimensions is 1. Ox knows if one of the dimensions of a matrix is currently 1. So the ith element of a 5×1 column vector would be indexed by [i][0]. And for a row vector it would be [0][i]. You can always include the [0] index, but you can drop it and use only one of the index brackets, as shown here:
    decl v = <0; 1; 2>;
         v[1] = 4;
    
    v would now contain
        0
        4
        2
    
    If you index a two-dimensional matrix with only one index Ox will print out a warning and act as if you put [0] as the second index.
    A = <0, 1; 2, 3>;
    A[1] = 4;
    
    A would now contain
        0   1
        4   3
    
    along with a warning. The warning should prompt you to put [0] in your code so that you are sure you are changing element $A_{10}$ instead of $A_{01}$.
  5. Prefix and Postfix Incrementation
  6. Prefix means placed before the thing; postfix means placed after the thing and incrementation means add or subtract by one. In first generation computer programs (like FORTRAN or COBOL) one of the most common types of statements would be
        i = i + 1;
        j = j - 1;
        
    This is incrementing the variable i. This would typically happen inside a loop, a kind of complex statement introduced below. Anything that is very common and involves the same symbol twice (here i) is prone to error. The programmer might accidentally type i = j + 1; and not notice the mistake.

    So Ox uses the same syntax as C and many other C-based languages to eliminate the need/danger of increments. In Ox you can simply write
     i++; 
    This means add 1 to the current value of i and store it back in i. And you can decrement (subtract 1) using i--. The following uses of will create errors (try to understand why):
    //ERRORS
    5++;
    
    decl msg = "hello";
    msg++;
    
    decl list = {"milk","eggs"};
    list--;
    
    You can also place the value before the thing to be incremented;
    ++i;
    --j;
    
    Is there a difference? Yes, there is a real difference between ++i and --i. You should try to understand the difference yourself by running this program:
    04a-increments.ox
     1:    #include "oxstd.h"
     2:    main() {
     3:        decl i;
     4:        i = 5;
     5:        println("A. ",i);
     6:        println("B. ",i++);
     7:        println("C. ",++i);
     8:        println("D. ",i);
     9:    	println("E. ",--i);
    10:    	println("F. ",i--);
    11:    	println("G. ",i);
    12:        }
    
    Transpose
    The postfix operator ' takes the transpose of a matrix. It has no effect on other arithmetic types of operands. That is, the transpose of an integer or a double is itself.
  7. Logic and Relations
  8. Besides computing expressions computational economics needs to compare values, check for conditions based on inputs, etc. This requires ways to carry out logical operations, which means determines whether some condition is true or false. If your model does one thing when $x \lt y$ and another thing when $x\ge y$, then the result of the comparison has to be recorded and remembered. This requires storing the values of TRUE and FALSE.

    One (possibly intuitive) notion is that FALSE is like the numeric value 0. And TRUE is like the numerical value 1. This works beautifully and is the basis of Boolean logic. However, what is the truth value of 3? Or -22? In Boolean logic these values cannot occur, but if we try to store truth values in, say, an Ox int value we might encounter such values. Some languages have a specific type that only stores 0 and 1. In Ox (and many other values), the rule is that 0 is FALSE and not 0 is TRUE. In other words, anything but 0 is treated like TRUE. And if you ask Ox to store a TRUE value it will store the value 1.

    Table 3. Logic and Relational Operators in Ox

    Name            Symbol       Example     Notes on Ox Value Produced
    -----------------------------------------------------------------------------------------
    truth value                     A       FALSE if A=0, TRUE otherwise (any non-zero value)
    Equality        ==            A==B      equals 1 if A=B, 0 otherwise
    Not Equal       !=            A!=B        equals 0 if A=B, 1 otherwise
    Less Than       <           A<B
    Greater Than    >           A>B
    Less or Equal   <=          A<=B
    Greater or Equal>=          A>=B
    
    Not             !           !A          equals 1 if A=0, 1 otherwise
    And             &&   A&&B            1 if A and B are TRUE, 0 otherwise
    Or              ||           A||B       equals 1 if A or B are TRUE, 0 if both FALSE
    
    What happens if A or B is a matrix? What is the logical meaning of $A \le B$ if both sides are matrices? These types of comparisons are explained later.

Exercises