comparison src/samer/maths/Linear.java @ 0:bf79fb79ee13

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