#-------------------------------------------------------------
#
# 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.
#
#-------------------------------------------------------------

# Utility script to split X into new X and Y.
#
# Parameters:
#    X       : (input)  filename of data matrix
#    y       : (default ncol(X))  colIndex
#    OX      : (output) filename of output matrix with all columns except y
#    OY      : (output) filename of output matrix with y column
#    ofmt    : (default binary) format of OX and OY output matrix
#
# Example:
#   hadoop jar SystemDS.jar -f algorithms/utils/splitXY.dml -nvargs X="/tmp/X.mtx" y=50 OX="/tmp/OX.mtx  OY="/tmp/OY.mtx
#

ofmt = ifdef($ofmt, "binary")
y = ifdef($y, ncol($X))

X = read ($X)

if (y == 1)
{
   OX = X[,y+1:ncol(X)]
   OY = X[,y]
} 
else if (y == ncol(X))
{
   OX = X[,1:y-1]
   OY = X[,y]
} 
else 
{
   OX1 = X[,1:y-1]
   OX2 = X[,y+1:ncol(X)]
   OX = cbind (OX1, OX2)
   OY = X[,y]
}

# Write output
write (OX, $OX, format=ofmt)
write (OY, $OY, format=ofmt)


