Skip to content

Commit 3cffb49

Browse files
authored
Full FinanceLib bond functions
1 parent 4f825e7 commit 3cffb49

File tree

7 files changed

+110
-19
lines changed

7 files changed

+110
-19
lines changed

libraries/Ocl.class

38.2 KB
Binary file not shown.

libraries/Ocl.java

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,17 @@ public static <T> ArrayList<T> asSequence(Collection<T> c)
497497
return res;
498498
}
499499

500+
public static <S,T> ArrayList<HashMap<S,T>> asSequence(Map<S,T> m)
501+
{ Set<Map.Entry<S,T>> ss = m.entrySet();
502+
ArrayList res = new ArrayList();
503+
for (Map.Entry<S,T> item : ss)
504+
{ HashMap<S,T> maplet = new HashMap<S,T>();
505+
maplet.put(item.getKey(), item.getValue());
506+
res.add(maplet);
507+
}
508+
return res;
509+
}
510+
500511
public static <T> ArrayList<T> asOrderedSet(Collection<T> c)
501512
{ ArrayList<T> res = new ArrayList<T>();
502513
for (T x : c)
@@ -513,16 +524,16 @@ public static <T> HashSet<T> asSet(Collection<T> c)
513524
return res;
514525
}
515526

516-
public static <K,T> HashSet<Map<K,T>> asSet(Map<K,T> m)
517-
{ Set ss = m.entrySet();
518-
HashSet<Map<K,T>> res = new HashSet<Map<K,T>>();
519-
for (Object ee : ss)
520-
{ Map.Entry x = (Map.Entry) ee;
521-
Map<K,T> mx = new HashMap<K,T>();
522-
mx.put((K) x.getKey(), (T) x.getValue());
523-
res.add(mx);
524-
}
525-
return res;
527+
public static <K,T> HashSet<Map<K,T>> asSet(Map<K,T> m)
528+
{ Set ss = m.entrySet();
529+
HashSet<Map<K,T>> res = new HashSet<Map<K,T>>();
530+
for (Object ee : ss)
531+
{ Map.Entry x = (Map.Entry) ee;
532+
Map<K,T> mx = new HashMap<K,T>();
533+
mx.put((K) x.getKey(), (T) x.getValue());
534+
res.add(mx);
535+
}
536+
return res;
526537
}
527538

528539
public static <T extends Comparable> ArrayList<T> asBag(Collection<T> a)
@@ -855,7 +866,10 @@ public static String subrange(String s, int i, int j)
855866
}
856867

857868
public static <T> ArrayList<T> subrange(List<T> l, int i, int j)
858-
{ ArrayList<T> tmp = new ArrayList<T>();
869+
{ ArrayList<T> tmp = new ArrayList<T>();
870+
if (i < 0) { i = l.size() + i; }
871+
if (j < 0) { j = l.size() + j; }
872+
859873
for (int k = i-1; k < j; k++)
860874
{ tmp.add(l.get(k)); }
861875
return tmp;
@@ -872,6 +886,7 @@ public static String subrange(String s, int i)
872886

873887
public static <T> ArrayList<T> subrange(List<T> l, int i)
874888
{ ArrayList<T> tmp = new ArrayList<T>();
889+
if (i < 0) { i = l.size() + i; }
875890
for (int k = i-1; k < l.size(); k++)
876891
{ tmp.add(l.get(k)); }
877892
return tmp;

libraries/OclIterator.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,16 @@ public OclIteratorResult nextResult()
269269
}
270270

271271
public Object next()
272-
{
272+
{ if (generatorFunction != null)
273+
{ Object res = generatorFunction.apply(new Integer(position));
274+
position++;
275+
if (position <= elements.size())
276+
{ set(res); }
277+
else
278+
{ elements.add(res); }
279+
return res;
280+
}
281+
273282
Object result = null;
274283
moveForward();
275284
return getCurrent();

libraries/StringLib.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,19 @@ public static String rightTrim(String s)
3434
return result;
3535
}
3636

37+
public static String sumStringsWithSeparator(List lst, String sep)
38+
{
39+
String res = "";
40+
for (int i = 0; i < lst.size(); i++)
41+
{
42+
String xx = "" + lst.get(i);
43+
res = res + xx;
44+
if (i < lst.size() - 1)
45+
{ res = res + sep; }
46+
}
47+
return res;
48+
}
49+
3750

3851
public static String padLeftWithInto(String s,String c,int n)
3952
{ String result = "";
@@ -278,6 +291,41 @@ else if (c == '\7')
278291
return res;
279292
}
280293

294+
public static String formattedString(String f)
295+
{ /* s written with {v:fmt} to format v element */
296+
297+
String res = "\"";
298+
boolean inElement = false;
299+
boolean inFormat = false;
300+
String var = "";
301+
for (int i = 0; i < f.length(); i++)
302+
{ char c = f.charAt(i);
303+
if (inElement)
304+
{ if (':' == c)
305+
{ res = res + "\" + " + var + " + \"";
306+
var = "";
307+
inFormat = true;
308+
}
309+
else if ('}' == c)
310+
{ inElement = false;
311+
inFormat = false;
312+
var = "";
313+
}
314+
else if (inFormat)
315+
{ } // skip the format
316+
else
317+
{ var = var + c; } // var name character
318+
}
319+
else if ('{' == c)
320+
{ inElement = true; }
321+
else
322+
{ res = res + c; } // literal character
323+
}
324+
325+
return res + "\"";
326+
}
327+
328+
281329
public static void main(String[] args)
282330
{ /* ArrayList res = StringLib.scan("100##3.3::20\n", "%d##%f::%d\n");
283331
System.out.println(res);

libraries/mathlib.km3

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ package mathlib {
1313

1414
static attribute iz : int;
1515

16+
static attribute defaultTolerance : double;
17+
1618
static attribute hexdigit : Sequence(String);
1719

1820
static operation initialiseMathLib() : void
1921
pre: true
2022
post: true
2123
activity:
22-
(MathLib.hexdigit := Sequence{ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F" } ; MathLib.setSeeds(1001, 781, 913) );
24+
(MathLib.hexdigit := Sequence{ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F" } ; MathLib.setSeeds(1001, 781, 913) ; MathLib.defaultTolerance := 0.001 );
2325

2426
static query pi() : double
2527
pre: true
@@ -29,6 +31,10 @@ package mathlib {
2931
pre: true
3032
post: result = 3.14159265;
3133

34+
static query e() : double
35+
pre: true
36+
post: result = 1->exp();
37+
3238
static query eValue() : double
3339
pre: true
3440
post: result = 1->exp();
@@ -269,8 +275,8 @@ package mathlib {
269275
post: m = MathLib.mean(sq) & result = ( sq->collect( x | (x - m)->sqr() )->sum() ) / ( sq->size() );
270276

271277
static query standardDeviation(sq : Sequence(double)) : double
272-
pre: sq->size() > 0
273-
post: m = MathLib.variance(sq) & result = m->sqrt();
278+
pre: true
279+
post: (sq->size() = 0 => result = 0) & (sq->size() > 0 => result = MathLib.variance(sq)->sqrt());
274280

275281
static query lcm(x : int, y : int) : int
276282
pre: true
@@ -296,13 +302,13 @@ package mathlib {
296302
static query differential(f : Function(double,double)) : Function(double,double)
297303
pre: true
298304
post:
299-
result = lambda x : double in (500*(f->apply(x+0.001) - f->apply(x-0.001)));
305+
result = lambda x : double in ((1.0/(2.0*MathLib.defaultTolerance))*(f->apply(x+MathLib.defaultTolerance) - f->apply(x-MathLib.defaultTolerance)));
300306

301307
static query definiteIntegral(st : double , en : double , f : Function(double,double) ) : double
302308
pre: true
303309
post: true
304310
activity:
305-
var tol : double := 0.001;
311+
var tol : double := MathLib.defaultTolerance;
306312
var area : double := 0.0;
307313
var d : double := tol*(en - st);
308314
var cum : double := st;
@@ -347,7 +353,7 @@ package mathlib {
347353
static query irrDiscrete(values: Sequence(double)) : double
348354
pre: true post: true;
349355

350-
static query straddleDate(d1 : OclDate, d2 : OclDate, period : int) : Sequence(OclDate)
356+
static query straddleDates(d1 : OclDate, d2 : OclDate, period : int) : Sequence(OclDate)
351357
pre: true
352358
post: true
353359
activity:
@@ -357,6 +363,16 @@ package mathlib {
357363
(cd := cd.addMonthYMD(period));
358364
return Sequence{cd.subtractMonthYMD(period),cd};
359365

366+
static query numberOfPeriods(settle : OclDate, matur : OclDate, period : int) : int
367+
pre: true
368+
post: (monthsToMaturity = OclDate.differenceMonths(matur,settle)*1.0 => result = (monthsToMaturity/period)->ceil());
369+
370+
static query sequenceOfPeriods(sett : OclDate, mat : OclDate, period : int) : Sequence(int)
371+
pre: true
372+
post:
373+
(numPeriods = FinanceLib.numberOfPeriods(sett, mat, period) =>
374+
result = Integer.subrange(1, numPeriods));
375+
360376
}
361377

362378
}

libraries/mathlib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,6 @@ def bisectionAsc(r, rl, ru, f, tol) :
250250
r = ru
251251

252252
v = f(r)
253-
# print("value at " + str(r) + " is " + str(v))
254253

255254
if v < tol and v > -tol:
256255
return r
@@ -379,6 +378,7 @@ def allInstances_MathLib():
379378

380379
MathLib.initialiseMathLib()
381380

381+
382382
class FinanceLib :
383383

384384
def discountDiscrete(amount, rate, time) :

libraries/ocl.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1704,6 +1704,9 @@ int** subrangeint(int** col, int i, int j)
17041704
{ /* OCL indexing: 1..n */
17051705

17061706
int len = length((void**) col);
1707+
if (i < 0) { i = len + i; }
1708+
if (j < 0) { j = len + j; }
1709+
17071710
if (i > j || j > len)
17081711
{ return NULL; }
17091712

0 commit comments

Comments
 (0)