www.delorie.com/gnu/docs/calc/calc_310.html   search  
Buy the book!

GNU Emacs Calc 2.02 Manual

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

15.3 Grabbing from Other Buffers

The M-# g (calc-grab-region) command takes the text between point and mark in the current buffer and attempts to parse it as a vector of values. Basically, it wraps the text in vector brackets `[ ]' unless the text already is enclosed in vector brackets, then reads the text as if it were an algebraic entry. The contents of the vector may be numbers, formulas, or any other Calc objects. If the M-# g command works successfully, it does an automatic M-# c to enter the Calculator buffer.

A numeric prefix argument grabs the specified number of lines around point, ignoring the mark. A positive prefix grabs from point to the nth following newline (so that M-1 M-# g grabs from point to the end of the current line); a negative prefix grabs from point back to the n+1st preceding newline. In these cases the text that is grabbed is exactly the same as the text that C-k would delete given that prefix argument.

A prefix of zero grabs the current line; point may be anywhere on the line.

A plain C-u prefix interprets the region between point and mark as a single number or formula rather than a vector. For example, M-# g on the text `2 a b' produces the vector of three values `[2, a, b]', but C-u M-# g on the same region reads a formula which is a product of three things: `2 a b'. (The text `a + b', on the other hand, will be grabbed as a vector of one element by plain M-# g because the interpretation `[a, +, b]' would be a syntax error.)

If a different language has been specified (see section 7.8 Language Modes), the grabbed text will be interpreted according to that language.

The M-# r (calc-grab-rectangle) command takes the text between point and mark and attempts to parse it as a matrix. If point and mark are both in the leftmost column, the lines in between are parsed in their entirety. Otherwise, point and mark define the corners of a rectangle whose contents are parsed.

Each line of the grabbed area becomes a row of the matrix. The result will actually be a vector of vectors, which Calc will treat as a matrix only if every row contains the same number of values.

If a line contains a portion surrounded by square brackets (or curly braces), that portion is interpreted as a vector which becomes a row of the matrix. Any text surrounding the bracketed portion on the line is ignored.

Otherwise, the entire line is interpreted as a row vector as if it were surrounded by square brackets. Leading line numbers (in the format used in the Calc stack buffer) are ignored. If you wish to force this interpretation (even if the line contains bracketed portions), give a negative numeric prefix argument to the M-# r command.

If you give a numeric prefix argument of zero or plain C-u, each line is instead interpreted as a single formula which is converted into a one-element vector. Thus the result of C-u M-# r will be a one-column matrix. For example, suppose one line of the data is the expression `2 a'. A plain M-# r will interpret this as `[2 a]', which in turn is read as a two-element vector that forms one row of the matrix. But a C-u M-# r will interpret this row as `[2*a]'.

If you give a positive numeric prefix argument n, then each line will be split up into columns of width n; each column is parsed separately as a matrix element. If a line contained `2 +/- 3 4 +/- 5', then grabbing with a prefix argument of 8 would correctly split the line into two error forms.

See section 10. Vector/Matrix Functions, to see how to pull the matrix apart into its constituent rows and columns. (If it is a 1x1 matrix, just hit v u (calc-unpack) twice.)

The M-# : (calc-grab-sum-down) command is a handy way to grab a rectangle of data and sum its columns. It is equivalent to typing M-# r, followed by V R : + (the vector reduction command that sums the columns of a matrix; see section 10.8.3 Reducing). The result of the command will be a vector of numbers, one for each column in the input data. The M-# _ (calc-grab-sum-across) command similarly grabs a rectangle and sums its rows by executing V R _ +.

As well as being more convenient, M-# : and M-# _ are also much faster because they don't actually place the grabbed vector on the stack. In a M-# r V R : + sequence, formatting the vector for display on the stack takes a large fraction of the total time (unless you have planned ahead and used v . and t . modes).

For example, suppose we have a column of numbers in a file which we wish to sum. Go to one corner of the column and press C-@ to set the mark; go to the other corner and type M-# :. Since there is only one column, the result will be a vector of one number, the sum. (You can type v u to unpack this vector into a plain number if you want to do further arithmetic with it.)

To compute the product of the column of numbers, we would have to do it "by hand" since there's no special grab-and-multiply command. Use M-# r to grab the column of numbers into the calculator in the form of a column matrix. The statistics command u * is a handy way to find the product of a vector or matrix of numbers. See section 10.7 Statistical Operations on Vectors. Another approach would be to use an explicit column reduction command, V R : *.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster   donations   bookstore     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003