DIRECTORY LeastSq, Random, Real; LeastSqImpl: CEDAR PROGRAM IMPORTS Random, Real EXPORTS LeastSq = BEGIN NewPointSet: PUBLIC PROC [ dim: NAT ] RETURNS [ ps: LeastSq.PointSet ] = { ps _ NEW[LeastSq.PointSetRec[dim]]; ps.count _ 0; FOR row: NAT IN [0..dim) DO ps[row] _ NEW[LeastSq.RowRec[dim]]; FOR col: NAT IN [0..dim) DO ps[row][col] _ 0.0; ENDLOOP; ENDLOOP; }; AddPoint: PUBLIC PROC [ ps: LeastSq.PointSet, pt: LeastSq.Row ] = { IF pt.colDim # ps.rowDim THEN ERROR; ps.count _ ps.count+1; FOR row: NAT IN [0..ps.rowDim) DO FOR col: NAT IN [0..row] DO ps[row][col] _ ps[row][col]+pt[row]*pt[col]; ENDLOOP; ENDLOOP; }; LeastSqFit: PUBLIC PROC [ ps: LeastSq.PointSet ] RETURNS [ eq: LeastSq.Row ] = { basis: LeastSq.PointSet = NEW[LeastSq.PointSetRec[ps.rowDim]]; FOR row: NAT IN [0..ps.rowDim) DO -- fill ps symmetrically FOR col: NAT IN (row..ps.rowDim) DO ps[row][col] _ ps[col][row]; ENDLOOP; ENDLOOP; FOR row: NAT IN [0..ps.rowDim) DO innerProd: REAL; col: NAT; basis[row] _ NEW[LeastSq.RowRec[ps.rowDim]]; FOR col IN [0..ps.rowDim) DO basis[row][col] _ 0.0 ENDLOOP; FOR prevrow: NAT IN [0..row) DO innerProd _ 0.0; FOR col IN [0..ps.rowDim) DO innerProd _ innerProd+basis[prevrow][col]*ps[row][col]; ENDLOOP; -- col FOR col IN [0..ps.rowDim) DO basis[row][col] _ basis[row][col]-innerProd*basis[prevrow][col]; ENDLOOP; -- col ENDLOOP; -- prevrow basis[row][row] _ 1.0; IF row0.0 THEN { normFact: REAL = 1.0/Real.SqRt[innerProd]; FOR col IN [0..ps.rowDim) DO basis[row][col] _ normFact*basis[row][col]; ENDLOOP; -- col }; END; ENDLOOP; -- row eq _ basis[ps.rowDim-1]; }; NewRow: PROC [ dim: NAT ] RETURNS [ pt: LeastSq.Row ] = { pt _ NEW[LeastSq.RowRec[dim]] }; Test: PROC [ eqIn: LeastSq.Row, n: INT, noise: REAL _ 0 ] RETURNS [ eqOut: LeastSq.Row ] = BEGIN ps: LeastSq.PointSet = NewPointSet[eqIn.colDim]; pt: LeastSq.Row = NEW[LeastSq.RowRec[eqIn.colDim]]; pt[0] _ 1.0; FOR i: INT IN [0..n) DO sum: REAL _ eqIn[0]+noise*Random.Choose[-10000, 10000]; FOR j: NAT IN [1..eqIn.colDim-1) DO pt[j] _ Random.Choose[-10000, 10000]; sum _ sum+eqIn[j]*pt[j]; ENDLOOP; pt[eqIn.colDim-1] _ -sum; AddPoint[ps, pt]; ENDLOOP; eqOut _ LeastSqFit[ps]; END; END. øLeastSqImpl.mesa Last Edited by: McCreight, April 12, 1985 11:49:30 am PST At this point, ps[row][col] contains the sum over all input points of pt[row]*pt[col], where pt[k] = 1 if k=0, an independent variable if k>0 and kJ˜š œœœœÏc˜:šœœœ˜#J˜Jšœ˜—Jšœ˜J˜—šœœœ˜!Jšœ œ˜Jšœœ˜ Jšœ œ˜,Jšœœœœ˜;šœ œœ ˜Jšœ˜šœœ˜Jšœ7˜7JšœŸ˜—šœœ˜Jšœ@˜@JšœŸ˜—JšœŸ ˜J˜—J˜J˜šœœŸ+˜CJš˜Jšœ˜šœœ˜šœœœ˜"JšœE˜EJšœŸ˜—JšœŸ˜—šœœ˜Jšœ œ˜*šœœ˜Jšœ+˜+JšœŸ˜—J˜—Jšœ˜—JšœŸ˜J˜Jšœ˜—J˜J˜J˜—šžœœœœ˜9Jšœœ˜Jšœ˜J˜—š žœœœ œœ˜ZJš˜Jšœ0˜0Jšœœ˜3J˜ šœœœ˜Jšœœ.˜7šœœœ˜#Jšœ%˜%Jšœ˜Jšœ˜—Jšœ˜Jšœ˜Jšœ˜—Jšœ˜Jšœ˜J˜—šœ˜˜J˜J˜————…— „Ä