[ 
https://issues.apache.org/jira/browse/MAHOUT-300?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12836167#action_12836167
 ] 

Robin Anil commented on MAHOUT-300:
-----------------------------------

I removed hasNoElements check as per sean's and teds comment
the current fix is as follows. See the maxIndex implementation. I dont know 
what to do in the edge case of vector being negative valued and sparse. We 
could return -1 or first index of 0;

{code}
  public double maxValue() {
    double result = Double.NEGATIVE_INFINITY;
    Iterator<Element> iter = this.iterateNonZero();
    while (iter.hasNext()) {
      Element element = iter.next();
      result = Math.max(result, element.get());
    }
    if (getNumNondefaultElements() < size()) return Math.max(result, 0.0);
    return result;
  }

  public int maxValueIndex() {
    int result = -1;
    double max = Double.NEGATIVE_INFINITY;
    Iterator<Element> iter = this.iterateNonZero();
    while (iter.hasNext()) {
      Element element = iter.next();
      double tmp = element.get();
      if (tmp > max) {
        max = tmp;
        result = element.index();
      }
    }
    // if the maxElement is negative and the vector is sparse then any
    // unfilled element(0.0) could be the maxValue hence return -1;
    if (getNumNondefaultElements() < size() && max < 0.0) {
      return -1; 
    }
    return result;
  }
{code}

> Solve performance issues with Vector Implementations
> ----------------------------------------------------
>
>                 Key: MAHOUT-300
>                 URL: https://issues.apache.org/jira/browse/MAHOUT-300
>             Project: Mahout
>          Issue Type: Improvement
>    Affects Versions: 0.3
>            Reporter: Robin Anil
>             Fix For: 0.3
>
>         Attachments: MAHOUT-300.patch
>
>
> AbstractVector operations like times
>   public Vector times(double x) {
>     Vector result = clone();
>     Iterator<Element> iter = iterateNonZero();
>     while (iter.hasNext()) {
>       Element element = iter.next();
>       int index = element.index();
>       result.setQuick(index, element.get() * x);
>     }
>     return result;
>   }
> should be implemented as follows
>  public Vector times(double x) {
>     Vector result = clone();
>     Iterator<Element> iter = result.iterateNonZero();
>     while (iter.hasNext()) {
>       Element element = iter.next();
>       element.set(element.get() * x);
>     }
>     return result;
>   }

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to