calc: Grabbing From Buffers

 
 15.5 Grabbing from Other Buffers
 ================================
 
 The ‘C-x * 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 ‘C-x * g’ command
 works successfully, it does an automatic ‘C-x * 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
 ‘n’th following newline (so that ‘M-1 C-x * g’ grabs from point to the
 end of the current line); a negative prefix grabs from point back to the
 ‘n+1’st 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, ‘C-x * g’
 on the text ‘2 a b’ produces the vector of three values ‘[2, a, b]’, but
 ‘C-u C-x * 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 ‘C-x * g’ because the
 interpretation ‘[a, +, b]’ would be a syntax error.)
 
    If a different language has been specified (SeeLanguage Modes),
 the grabbed text will be interpreted according to that language.
 
    The ‘C-x * 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 ‘C-x * 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 C-x * r’ will be a
 one-column matrix.  For example, suppose one line of the data is the
 expression ‘2 a’.  A plain ‘C-x * 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 C-x * 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.
 
    SeeMatrix 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 ‘C-x * :’ (‘calc-grab-sum-down’) command is a handy way to grab a
 rectangle of data and sum its columns.  It is equivalent to typing ‘C-x
 * r’, followed by ‘V R : +’ (the vector reduction command that sums the
 columns of a matrix; SeeReducing).  The result of the command will
 be a vector of numbers, one for each column in the input data.  The ‘C-x
 * _’ (‘calc-grab-sum-across’) command similarly grabs a rectangle and
 sums its rows by executing ‘V R _ +’.
 
    As well as being more convenient, ‘C-x * :’ and ‘C-x * _’ are also
 much faster because they don’t actually place the grabbed vector on the
 stack.  In a ‘C-x * 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 ‘C-x * :’.  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
 ‘C-x * 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.  SeeStatistical
 Operations.  Another approach would be to use an explicit column
 reduction command, ‘V R : *’.