Github user dbtsai commented on a diff in the pull request:
https://github.com/apache/spark/pull/3462#discussion_r20968353
--- Diff: mllib/src/main/scala/org/apache/spark/mllib/linalg/Vectors.scala
---
@@ -261,6 +261,57 @@ object Vectors {
sys.error("Unsupported Breeze vector type: " + v.getClass.getName)
}
}
+
+ /**
+ * Returns the p-norm of this vector.
+ * @param vector input vector.
+ * @param p norm.
+ * @return norm in L^p^ space.
+ */
+ private[spark] def norm(vector: Vector, p: Double): Double = {
+ require(p >= 1.0)
+ val values = vector match {
+ case dv: DenseVector => dv.values
+ case sv: SparseVector => sv.values
+ case v => throw new IllegalArgumentException("Do not support vector
type " + v.getClass)
+ }
+ val size = values.size
+
+ if (p == 1) {
--- End diff --
In bytecode, there is no direct `switch` operation. As a result, the
`swtich` or pattern matching will be compiled into `if` statement in the
bytecode. See the following example
```scala
def fun1(p: Double) = {
p match {
case 1.0 => 1.0
case 2.0 => 2.0
case _ => p
}
}
def fun2(p: Double) = {
if (p == 1.0) 1.0
else if (p == 2.0) 2.0
else p
}
```
will be compiled to
```
// access flags 0x1
public fun1(D)D
L0
LINENUMBER 145 L0
DLOAD 1
DSTORE 3
L1
LINENUMBER 146 L1
DCONST_1
DLOAD 3
DCMPL
IFNE L2
DCONST_1
DSTORE 5
GOTO L3
L2
LINENUMBER 147 L2
FRAME APPEND [D]
LDC 2.0
DLOAD 3
DCMPL
IFNE L4
LDC 2.0
DSTORE 5
GOTO L3
L4
LINENUMBER 148 L4
FRAME SAME
DLOAD 1
DSTORE 5
L3
LINENUMBER 145 L3
FRAME APPEND [D]
DLOAD 5
DRETURN
L5
LOCALVARIABLE this Lorg/apache/spark/mllib/stat/Test$; L0 L5 0
LOCALVARIABLE p D L0 L5 1
MAXSTACK = 4
MAXLOCALS = 7
// access flags 0x1
public fun2(D)D
L0
LINENUMBER 153 L0
DLOAD 1
DCONST_1
DCMPL
IFNE L1
DCONST_1
GOTO L2
L1
LINENUMBER 154 L1
FRAME SAME
DLOAD 1
LDC 2.0
DCMPL
IFNE L3
LDC 2.0
GOTO L2
L3
LINENUMBER 155 L3
FRAME SAME
DLOAD 1
L2
LINENUMBER 153 L2
FRAME SAME1 D
DRETURN
L4
LOCALVARIABLE this Lorg/apache/spark/mllib/stat/Test$; L0 L4 0
LOCALVARIABLE p D L0 L4 1
MAXSTACK = 4
MAXLOCALS = 3
```
---
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 [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]