SampleVariance.java :  » Math » lablayer » LabLayer » model » MathComponents » Java Open Source

Java Open Source » Math » lablayer 
lablayer » LabLayer » model » MathComponents » SampleVariance.java
/**
 * 
 */
package lablayer.model.MathComponents;

import lablayer.model.ComponentFactory;
import lablayer.model.LabData;
import lablayer.model.Componentable;

/**
 * Component #2
 * 
 * Simple variance of array
 */
class SampleVariance implements MathComponent {
  private LabData result;
  
  private LabData data;
  
  private String componentName = " ";
  
  private String componentId = "2";
  
  public SampleVariance() {
    super();
    this.result = new LabData();
  }
  
  @Override
  public LabData getData() {
    return this.data;
  }

  @Override
  public void calculate() {
    Double[][][] data = this.data.getData();
    Double[] row = null;
    Double mid = 0.0;
    
    if(data.length >= 1) {
      if(data[0].length >= 1) {
        row = data[0][0];
      }
    }
        
    Componentable middleValue = ComponentFactory.createComponent(
        MathComponentFactory.createMiddleValue());
    middleValue.setData(this.data);
    middleValue.calculate();
    mid = ((middleValue.getResult()).getData())[0][0][0];
    
    Double sum = 0.0;
    try {
      assert row != null : "row is null pointer";
      for(int i = 0; i < row.length; i++) {
        sum += (row[i] - mid)*(row[i] - mid);
      }      
    } catch (Exception e) {
      //process exception here
    }
    
    sum = sum / (row.length - 1);
    this.result = new LabData(LabData.parseString(sum.toString()));
  }

  @Override
  public void setData(LabData data) {
    try {
      assert data != null : "data is null pointer";
      this.data = data;
    } catch (Exception e) {
      //process exception here
    }
  }

  @Override
  public String getComponentId() {
    return this.componentId;
  }

  @Override
  public String getComponentName() {
    return this.componentName;
  }

  @Override
  public LabData getResult() {
    return this.result;
  }

  @Override
  public void setParameter(String params, String values) {
    throw new UnsupportedOperationException("Not supported yet.");
  }

  @Override
  public LabData getInitData() {
    throw new UnsupportedOperationException("Not supported yet.");
  }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.