public override T norm(int order = 2, Normtype t = Normtype.Natural) { if (factor.ToFieldWrapper().Equals(Field.zero)) { return(Field.zero); } switch (t) { case Normtype.Natural: case Normtype.Infinity: case Normtype.Max: if (order != 1 && t == Normtype.Natural) { throw new NotSupportedException("cannot calculate norm of non-vector of more than first order"); } return(factor); } throw new NotSupportedException(NOT_SUPPORTED_STRING); }
public virtual T norm(int order = 2, Normtype t = Normtype.Natural) { var field = Fields.getField <T>(); T ord = field.fromInt(order); switch (t) { case Normtype.Natural: if (order == 0) { return(field.fromInt(rows)); } if (this.isVector != VectorType.Collumn) { return(field.pow(this.Select(a => field.pow(field.abs(a), ord)).GetSum(), field.Invert(ord))); } if (order != 1) { throw new NotSupportedException("cannot calculate norm of non-vector of more than first order"); } return(this.getCollumns().Select(a => a.Select(field.abs).GetSum()).GetMax(field)); case Normtype.Infinity: return(this.getRows().Select(a => a.Select(field.abs).GetSum()).GetMax(field)); case Normtype.Frobenius: return(field.pow((this.conjugate().transpose() * this).Trace(), field.fromFraction(1, 2))); case Normtype.Entrywise: return(field.pow(this.Select(a => field.pow(field.abs(a), ord)).GetSum(), field.Invert(ord))); case Normtype.Max: return(this.GetMax(field)); default: throw new ArgumentException($"can't handle {nameof(Normtype)} " + t); } }