fork download
  1. // Java example 1.
  2.  
  3. /* package whatever; // don't place package name! */
  4.  
  5. import java.util.*;
  6. import java.lang.*;
  7. import java.io.*;
  8.  
  9.  
  10. interface Printable{
  11. public void print();
  12. }
  13.  
  14. class Pair< T1, T2 >{
  15. public T1 _first;
  16. public T2 _second;
  17.  
  18. public Pair( T1 first, T2 second ){
  19. _first = first;
  20. _second = second;
  21. }
  22.  
  23. } // End Pair.
  24.  
  25. interface One_D_Functional extends Printable{
  26. public double f( double d );
  27. } // End One_D_Functional.
  28.  
  29. class Range implements Printable{
  30. private double _lower_bound;
  31. private double _upper_bound;
  32.  
  33. public Range( double lower, double upper ){
  34. assert lower <= upper : " lower > upper!";
  35.  
  36. this._lower_bound = lower;
  37. this._upper_bound = upper;
  38. }
  39.  
  40. public double lower(){
  41. return this._lower_bound;
  42. }
  43.  
  44. public double upper(){
  45. return this._upper_bound;
  46. }
  47.  
  48. public void change_upper( double new_upper ){
  49. this._upper_bound = new_upper;
  50. }
  51.  
  52. public void change_lower( double new_lower ){
  53. this._lower_bound = new_lower;
  54. }
  55.  
  56. public void print(){
  57. System.out.println("Range{ [ " + this._lower_bound + " ; " + this._upper_bound + " ] }");
  58. }
  59.  
  60. } // End Range.
  61.  
  62. interface One_D_Piecewise_Functional extends One_D_Functional{
  63. public Range get_Range();
  64. public void add_Functional( One_D_Functional f, Range r );
  65. } // End One_D_Piecewise_Functional.
  66.  
  67.  
  68. class General_One_D_Piecewise_Functional implements One_D_Piecewise_Functional{
  69. private int _size_of_list;
  70. private List<Range> _Range_list;
  71. private List<One_D_Functional> _func_list;
  72.  
  73. Pair< Range, One_D_Functional > _find( double x ){
  74. ListIterator<Range> Range_Iter = _Range_list.listIterator();
  75. ListIterator< One_D_Functional > func_Iter = _func_list.listIterator();
  76.  
  77. while ( Range_Iter.hasNext() ){
  78. Range r = Range_Iter.next();
  79. One_D_Functional func = func_Iter.next();
  80.  
  81. if( x <= r.upper() ){
  82. Pair< Range, One_D_Functional > pair = new Pair< Range, One_D_Functional >( r, func );
  83. // pair._first = r;
  84. // pair._second = func;
  85.  
  86. return pair;
  87. } // End if.
  88.  
  89. } // End while.
  90.  
  91. // Redundant code required to compile.
  92. Pair< Range, One_D_Functional > pair = new Pair< Range, One_D_Functional >( _Range_list.get(0), _func_list.get(0) );
  93.  
  94. return pair;
  95.  
  96. } // End _find.
  97.  
  98.  
  99.  
  100.  
  101.  
  102. ListIterator< One_D_Functional > _find_func( double x ){
  103. ListIterator<Range> Range_Iter = _Range_list.listIterator();
  104. ListIterator< One_D_Functional > func_Iter = _func_list.listIterator();
  105.  
  106. while ( Range_Iter.hasNext() ){
  107. Range r = Range_Iter.next();
  108. One_D_Functional func = func_Iter.next();
  109.  
  110. if( x <= r.upper() ){
  111. return func_Iter;
  112.  
  113. } // End if.
  114.  
  115. } // End while.
  116.  
  117. // Redundant code needed to compile.
  118. return func_Iter;
  119.  
  120. } // End _find_func.
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132. public General_One_D_Piecewise_Functional( Range r, One_D_Functional func){
  133. this._Range_list = new ArrayList<Range>();
  134. _Range_list.add( 0, r );
  135. _func_list = new ArrayList<One_D_Functional>();
  136. _func_list.add( 0, func );
  137. _size_of_list = 1;
  138. }
  139.  
  140. public Range get_Range(){
  141.  
  142. Range lower = this._Range_list.get(0);
  143. Range upper = this._Range_list.get( _size_of_list-1 );
  144.  
  145. Range r = new Range( lower.lower(), upper.upper() );
  146.  
  147. return r;
  148. }
  149.  
  150. public void add_Functional(One_D_Functional f, Range r){
  151.  
  152. assert _Range_list.get( _size_of_list - 1 ).upper() == r.lower() : "Can't extend with range.";
  153.  
  154. _Range_list.add( r );
  155. _func_list.add( f );
  156. _size_of_list++;
  157. }
  158.  
  159. public double f( double x ){
  160.  
  161. assert x > _Range_list.get( _size_of_list - 1 ).upper() : "x is above range!";
  162. assert x < _Range_list.get( 0 ).lower() : "x is below range!";
  163.  
  164. Pair< Range, One_D_Functional > pair = _find( x );
  165.  
  166. if( x == pair._first.upper() ){
  167. // Need average of two functionals.
  168. ListIterator< One_D_Functional > func_Iter = _find_func( x );
  169. One_D_Functional f2 = func_Iter.next();
  170. One_D_Functional f1 = func_Iter.previous();
  171. f1 = func_Iter.previous();
  172.  
  173. return ( 0.5* ( f1.f(x) + f2.f(x) ) );
  174. }
  175.  
  176. return pair._second.f( x );
  177. }
  178.  
  179. public void print(){
  180.  
  181. System.out.println( "General_One_D_Piecewise_Functional{" );
  182. ListIterator<Range> Range_Iter = _Range_list.listIterator();
  183. ListIterator< One_D_Functional > func_Iter = _func_list.listIterator();
  184.  
  185. while( Range_Iter.hasNext() ){
  186. Range r = Range_Iter.next();
  187. One_D_Functional func = func_Iter.next();
  188.  
  189. System.out.println( "{ " );
  190. r.print();
  191. System.out.println( " , " );
  192. func.print();
  193. System.out.println( "} " );
  194. } // End while.
  195.  
  196. System.out.println( "}General_One_D_Piecewise_Functional" );
  197.  
  198.  
  199. }
  200.  
  201. } // End General_One_D_Piecewise_Functional.
  202.  
  203.  
  204. class One_D_Const implements One_D_Functional{
  205. private double d;
  206.  
  207. public One_D_Const( double d ){
  208. this.d = d;
  209. }
  210.  
  211. public double f( double d){
  212. return this.d;
  213. }
  214.  
  215. public void print( ){
  216. System.out.println("One_D_Const{ " + this.d + " }");
  217. }
  218.  
  219. } // End One_D_Const.
  220.  
  221.  
  222.  
  223.  
  224.  
  225. // Main class with static main function.
  226. class Main{
  227.  
  228. public static void main(String args[]){
  229. One_D_Functional f = new One_D_Const( 12.3 );
  230. f.print();
  231. System.out.println("f( 15 ) = " + f.f( 15 ) );
  232. Range r = new Range( 0.0, 10.0 );
  233.  
  234. General_One_D_Piecewise_Functional gen_func = new General_One_D_Piecewise_Functional( r, f);
  235.  
  236. gen_func.print();
  237.  
  238. gen_func.f( 10.1 );
  239.  
  240.  
  241. } // End Main.
  242. } // End Main
  243.  
  244.  
  245.  
  246.  
  247.  
Success #stdin #stdout 0.14s 37432KB
stdin
Standard input is empty
stdout
One_D_Const{ 12.3 }
f( 15 ) = 12.3
General_One_D_Piecewise_Functional{
{ 
Range{ [ 0.0 ; 10.0 ] }
 , 
One_D_Const{ 12.3 }
} 
}General_One_D_Piecewise_Functional