#-------------------------------------------------------------
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#
#-------------------------------------------------------------

X = read($1);

# calculate and same the trans-mult of X
Res1 = t(X) %*% X;
while(FALSE) {}	

# find vector V as the last column and matrix X1 as the rest
nc = ncol(X);
V = X[,nc];
X1 = X[, 1:nc-1];

# use X1 and V to derive trans-mult of X
r1 = cbind((t(X1) %*% X1), (t(X1) %*% V));
r2 = cbind((t(V) %*% X1), (t(V) %*% V));
Res2 = rbind(r1, r2);

# verify if the sums are same
while(FALSE) {}	
sum1 = sum(Res1);
sum2 = sum(Res2);
print("original matrix sum = " + sum1);
print("rewritten matrix sum = " + sum2);

write(Res1, $2, format="text");
write(Res2, $3, format="text");
