/**
 * 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
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * 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.
 */

{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# The first two lines of the A matrix represent the coordinates of each rotor in the X,Y plane,\n",
    "# and the third line the direction in which they spin.\n",
    "# Note that in this example the X axis is vertical, and the Y coordinates are in the top row of A.\n",
    "A = np.array([[-0.17,  0.17, -0.25,  0.25, -0.33,  0.33],\n",
    "             [-0.35, -0.35,  0.,    0.,    0.35,  0.35],\n",
    "             [-0.1,   0.1,   0.1,  -0.1,  -0.1,   0.1 ]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Moore-Penrose pseudoinverse of A\n",
    "B = np.linalg.pinv(A)\n",
    "# normalize roll/pitch to the largest of both\n",
    "# normalize yaw to 0.5\n",
    "# and transpose\n",
    "rp_max = B[:,0:1].max()\n",
    "n = np.array([rp_max, rp_max, 2*B[:,2].max()])\n",
    "B_nt = (B / n).T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ -67   67 -256  256 -189  189]\n",
      " [-197 -197    0    0  197  197]\n",
      " [ -77   77  128 -128  -57   57]]\n"
     ]
    }
   ],
   "source": [
    "# scale and round to 256 to return final coefficients\n",
    "scale = 256\n",
    "coeffs = np.around(scale * B_nt).astype(int)\n",
    "print(coeffs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<define name=\"ROLL_COEF\"  value=\"{ -67,   67, -256,  256, -189,  189}\"/>\n",
      "<define name=\"PITCH_COEF\" value=\"{-197, -197,    0,    0,  197,  197}\"/>\n",
      "<define name=\"YAW_COEF\"   value=\"{ -77,   77,  128, -128,  -57,   57}\"/>\n"
     ]
    }
   ],
   "source": [
    "# output defines\n",
    "import string\n",
    "rows = ['ROLL_COEF\"  ', 'PITCH_COEF\" ', 'YAW_COEF\"   ']\n",
    "for i, r in enumerate(rows):\n",
    "    print('<define name=\"' + r + 'value=\"{' + string.join(['{:>4d}'.format(c) for c in coeffs[i]], ', ') + '}\"/>')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false,
    "trusted": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                          precision    recall  f1-score   support\n\n             alt.atheism       0.86      0.95      0.90       137\n           comp.graphics       0.89      0.84      0.86       178\n comp.os.ms-windows.misc       0.85      0.90      0.88       168\ncomp.sys.ibm.pc.hardware       0.85      0.78      0.81       180\n   comp.sys.mac.hardware       0.95      0.83      0.89       174\n          comp.windows.x       0.88      0.91      0.90       193\n            misc.forsale       0.78      0.92      0.84       157\n               rec.autos       0.93      0.89      0.91       188\n         rec.motorcycles       0.96      0.95      0.95       185\n      rec.sport.baseball       0.94      0.95      0.95       183\n        rec.sport.hockey       0.92      0.98      0.95       180\n               sci.crypt       0.95      0.97      0.96       181\n         sci.electronics       0.93      0.80      0.86       180\n                 sci.med       0.95      0.94      0.95       159\n               sci.space       0.91      0.98      0.95       185\n  soc.religion.christian       0.88      0.92      0.90       176\n      talk.politics.guns       0.93      0.98      0.95       176\n   talk.politics.mideast       0.92      0.98      0.95       180\n      talk.politics.misc       0.97      0.91      0.94       127\n      talk.religion.misc       0.99      0.69      0.81       108\n\n             avg / total       0.91      0.91      0.91      3395\n\n"
     ]
    }
   ],
   "source": [
    "# <help:scikit_pipeline>\n",
    "# train the model and predict the test set\n",
    "y_pred = clf.fit(X_train, y_train).predict(X_test)\n",
    "\n",
    "# standard information retrieval metrics\n",
    "print metrics.classification_report(y_test, y_pred, target_names=labels)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div class=\"alert\" style=\"border: 1px solid #aaa; background: radial-gradient(ellipse at center, #ffffff 50%, #eee 100%);\">\n<div class=\"row\">\n    <div class=\"col-sm-1\"><img src=\"https://knowledgeanyhow.org/static/images/favicon_32x32.png\" style=\"margin-top: -6px\"/></div>\n    <div class=\"col-sm-11\">This notebook was created using <a href=\"https://knowledgeanyhow.org\">IBM Knowledge Anyhow Workbench</a>.  To learn more, visit us at <a href=\"https://knowledgeanyhow.org\">https://knowledgeanyhow.org</a>.</div>\n    </div>\n</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    ""
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2.0
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
