#! /usr/bin/gawk -f # Last edited on 1999-07-28 01:44:14 by stolfi BEGIN{ abort = -1; usage = "dot-product DIRFILE PTFILE"; # # where each line of both DIRFILE and PTFILE has the format # COORD LABEL, where COORD is a real number and LABEL is any # word. (The LABELs must be sorted and must match in both files.) # # After reading both files, prints to stdout # a single number, the sum of COORDs of DIRFILE times # the COORDs of PTFILE if (ARGC != 3) { error(("ARGC = " ARGC " - usage: " usage)); } dir = ARGV[1]; vec = ARGV[2]; if (dir == "") { error(("usage: " usage)); } if (vec == "") { error(("usage: " usage)); } N = 0; prod = 0; while (getline < dir) { N++; if (NF != 2) { error((dir ", line " N ": bad format")); } w = $2; ai = $1; getline < vec; if (ERRNO != "0") { error((vec ": " ERRNO)); } if (NF != 2) { error((vec ", line " N ": bad format")); } if (w != $2) { error((vec ", line " N ": bad format")); } bi = $1; prod += ai*bi; } if (ERRNO != "0") { error((dir ": " ERRNO)); } close(dir); close(vec); printf "%-8.5f", prod; } function error(msg) { printf "%s\n", msg > "/dev/stderr"; abort=1; exit(1); }