Mercurial > hg > jslab
annotate src/samer/maths/Linear.java @ 8:5e3cbbf173aa tip
Reorganise some more
author | samer |
---|---|
date | Fri, 05 Apr 2019 22:41:58 +0100 |
parents | bf79fb79ee13 |
children |
rev | line source |
---|---|
samer@0 | 1 /* |
samer@0 | 2 * Copyright (c) 2000, Samer Abdallah, King's College London. |
samer@0 | 3 * All rights reserved. |
samer@0 | 4 * |
samer@0 | 5 * This software is provided AS iS and WITHOUT ANY WARRANTY; |
samer@0 | 6 * without even the implied warranty of MERCHANTABILITY or |
samer@0 | 7 * FITNESS FOR A PARTICULAR PURPOSE. |
samer@0 | 8 */ |
samer@0 | 9 |
samer@0 | 10 package samer.maths; |
samer@0 | 11 import samer.core.*; |
samer@0 | 12 |
samer@0 | 13 |
samer@0 | 14 public class Linear extends Function |
samer@0 | 15 { |
samer@0 | 16 private double a, b; |
samer@0 | 17 |
samer@0 | 18 public Linear() { this( Shell.getDouble("a",1), Shell.getDouble("b",0)); } |
samer@0 | 19 public Linear(double a, double b) { this.a = a; this.b = b; } |
samer@0 | 20 |
samer@0 | 21 public double apply(double t) { return a*t+b; } |
samer@0 | 22 public void apply(double [] x) { |
samer@0 | 23 for (int i=0; i<x.length; i++) { x[i]*=a; x[i]+=b; } |
samer@0 | 24 } |
samer@0 | 25 public void apply(double [] x, double [] y) { |
samer@0 | 26 for (int i=0; i<x.length; i++) y[i]=a*x[i]+b; |
samer@0 | 27 } |
samer@0 | 28 public String format(String arg) { |
samer@0 | 29 StringBuffer buf=new StringBuffer(); |
samer@0 | 30 if (b!=0) { |
samer@0 | 31 buf.append(X.string(b)); |
samer@0 | 32 if (a!=0) buf.append(" + ").append(X.string(a)).append(arg); |
samer@0 | 33 } else { |
samer@0 | 34 if (a!=0) buf.append(X.string(a)).append(arg); |
samer@0 | 35 else return "0"; |
samer@0 | 36 } |
samer@0 | 37 return buf.toString(); |
samer@0 | 38 } |
samer@0 | 39 |
samer@0 | 40 public Function derivative() { return new Constant(a); } |
samer@0 | 41 public Function inverse() { return new Linear(-a/b,1/b); } |
samer@0 | 42 } |