Github user mengxr commented on a diff in the pull request:

    https://github.com/apache/spark/pull/460#discussion_r11985496
  
    --- Diff: 
mllib/src/test/scala/org/apache/spark/mllib/optimization/NNLSSuite.scala ---
    @@ -0,0 +1,95 @@
    +/*
    + * 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.
    + */
    +
    +package org.apache.spark.mllib.optimization
    +
    +import scala.util.Random
    +
    +import org.scalatest.FunSuite
    +
    +import org.jblas.{DoubleMatrix, SimpleBlas, NativeBlas}
    +
    +class NNLSSuite extends FunSuite {
    +  /** Generate a NNLS problem whose optimal solution is the all-ones 
vector. */
    +  def genOnesData(n: Int, rand: Random): (DoubleMatrix, DoubleMatrix) = {
    +    val A = new DoubleMatrix(n, n)
    +    val b = new DoubleMatrix(n, 1)
    +    for (i <- 0 until n; j <- 0 until n) {
    +      val aij = rand.nextDouble()
    +      A.put(i, j, aij)
    +      b.put(i, b.get(i, 0) + aij)
    +    }
    +
    +    val ata = new DoubleMatrix(n, n)
    +    val atb = new DoubleMatrix(n, 1)
    +
    +    NativeBlas.dgemm('T', 'N', n, n, n, 1.0, A.data, 0, n, A.data, 0, n, 
0.0, ata.data, 0, n)
    +    NativeBlas.dgemv('T', n, n, 1.0, A.data, 0, n, b.data, 0, 1, 0.0, 
atb.data, 0, 1)
    +
    +    (ata, atb)
    +  }
    +
    +  test("NNLSbyPCG: exact solution cases") {
    +    val n = 20
    +    val rand = new Random(12346)
    +    val ws = NNLSbyPCG.createWorkspace(n)
    +    var numSolved = 0
    +
    +    // About 15% of random 20x20 [-1,1]-matrices have a singular value 
less than 1e-3.  NNLSbyPCG
    +    // can legitimately fail to solve these anywhere close to exactly.  So 
we grab a considerable
    +    // sample of these matrices and make sure that we solved a substantial 
fraction of them.
    +
    +    for (kase <- 0 until 100) {
    +      val (ata, atb) = genOnesData(n, rand)
    +      val x = NNLSbyPCG.solve(ata, atb, true, ws)
    +      assert(x.length == n)
    +      var error = 0.0
    +      var solved = true
    +      for (i <- 0 until n) {
    --- End diff --
    
    `DoubleMatrix#distance2`.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to