001/*
002 * $Id: ExpVectorByte.java 5377 2015-12-31 17:43:16Z kredel $
003 */
004
005package edu.jas.poly;
006
007
008import java.util.ArrayList;
009import java.util.List;
010
011
012/**
013 * ExpVectorByte implements exponent vectors for polynomials using arrays of
014 * byte as storage unit. This class is used by ExpVector internally, there is no
015 * need to use this class directly.
016 * @see ExpVector
017 * @author Heinz Kredel
018 */
019
020public final class ExpVectorByte extends ExpVector
021/*implements AbelianGroupElem<ExpVectorByte>*/{
022
023
024    /**
025     * The data structure is an array of longs.
026     */
027    /*package*/final byte[] val;
028
029
030    /**
031     * Largest byte.
032     */
033    public static final long maxByte = (long) Byte.MAX_VALUE / 2;
034
035
036    /**
037     * Smallest byte.
038     */
039    public static final long minByte = (long) Byte.MIN_VALUE / 2;
040
041
042    /**
043     * Constructor for ExpVector.
044     * @param n length of exponent vector.
045     */
046    public ExpVectorByte(int n) {
047        this(new byte[n]);
048    }
049
050
051    /**
052     * Constructor for ExpVector. Sets exponent i to e.
053     * @param n length of exponent vector.
054     * @param i index of exponent to be set.
055     * @param e exponent to be set.
056     */
057    public ExpVectorByte(int n, int i, byte e) {
058        this(n);
059        val[i] = e;
060    }
061
062
063    /**
064     * Constructor for ExpVector. Sets exponent i to e.
065     * @param n length of exponent vector.
066     * @param i index of exponent to be set.
067     * @param e exponent to be set.
068     */
069    public ExpVectorByte(int n, int i, long e) {
070        this(n);
071        if (e >= maxByte || e <= minByte) {
072            throw new IllegalArgumentException("exponent to large: " + e);
073        }
074        val[i] = (byte) e;
075    }
076
077
078    /**
079     * Internal constructor for ExpVector. Sets val.
080     * @param v internal representation array.
081     */
082    protected ExpVectorByte(byte[] v) {
083        super();
084        val = v;
085    }
086
087
088    /**
089     * Constructor for ExpVector. Sets val, converts from long array.
090     * @param v long representation array.
091     */
092    public ExpVectorByte(long[] v) {
093        this(v.length);
094        for (int i = 0; i < v.length; i++) {
095            if (v[i] >= maxByte || v[i] <= minByte) {
096                throw new IllegalArgumentException("exponent to large: " + v[i]);
097            }
098            val[i] = (byte) v[i];
099        }
100    }
101
102
103    /**
104     * Constructor for ExpVector. Converts a String representation to an
105     * ExpVector. Accepted format = (1,2,3,4,5,6,7).
106     * @param s String representation.
107     */
108    public ExpVectorByte(String s) throws NumberFormatException {
109        super();
110        // first format = (1,2,3,4,5,6,7)
111        List<Byte> exps = new ArrayList<Byte>();
112        s = s.trim();
113        int b = s.indexOf('(');
114        int e = s.indexOf(')', b + 1);
115        String teil;
116        int k;
117        byte a;
118        if (b >= 0 && e >= 0) {
119            b++;
120            while ((k = s.indexOf(',', b)) >= 0) {
121                teil = s.substring(b, k);
122                a = Byte.parseByte(teil);
123                exps.add(Byte.valueOf(a));
124                b = k + 1;
125            }
126            if (b <= e) {
127                teil = s.substring(b, e);
128                a = Byte.parseByte(teil);
129                exps.add(Byte.valueOf(a));
130            }
131            int length = exps.size();
132            val = new byte[length];
133            for (int j = 0; j < length; j++) {
134                val[j] = exps.get(j).byteValue();
135            }
136        } else {
137            // not implemented
138            val = null;
139            // length = -1;
140            //Vector names = new Vector();
141            //vars = s;
142        }
143    }
144
145
146    /**
147     * Clone this.
148     * @see java.lang.Object#clone()
149     */
150    @Override
151    public ExpVectorByte copy() {
152        byte[] w = new byte[val.length];
153        System.arraycopy(val, 0, w, 0, val.length);
154        return new ExpVectorByte(w);
155    }
156
157
158    /**
159     * Get the exponent vector.
160     * @return val as long.
161     */
162    @Override
163    public long[] getVal() {
164        long v[] = new long[val.length];
165        for (int i = 0; i < val.length; i++) {
166            v[i] = val[i];
167        }
168        return v;
169    }
170
171
172    /**
173     * Get the exponent at position i.
174     * @param i position.
175     * @return val[i].
176     */
177    @Override
178    public long getVal(int i) {
179        return val[i];
180    }
181
182
183    /**
184     * Set the exponent at position i to e.
185     * @param i
186     * @param e
187     * @return old val[i].
188     */
189    @Override
190    protected long setVal(int i, long e) {
191        byte x = val[i];
192        if (e >= maxByte || e <= minByte) {
193            throw new IllegalArgumentException("exponent to large: " + e);
194        }
195        val[i] = (byte) e;
196        hash = 0; // beware of race condition
197        return x;
198    }
199
200
201    /**
202     * Set the exponent at position i to e.
203     * @param i
204     * @param e
205     * @return old val[i].
206     */
207    protected byte setVal(int i, byte e) {
208        byte x = val[i];
209        val[i] = e;
210        hash = 0; // beware of race condition
211        return x;
212    }
213
214
215    /**
216     * Get the length of this exponent vector.
217     * @return val.length.
218     */
219    @Override
220    public int length() {
221        return val.length;
222    }
223
224
225    /**
226     * Extend variables. Used e.g. in module embedding. Extend this by i
227     * elements and set val[j] to e.
228     * @param i number of elements to extend.
229     * @param j index of element to be set.
230     * @param e new exponent for val[j].
231     * @return extended exponent vector.
232     */
233    @Override
234    public ExpVectorByte extend(int i, int j, long e) {
235        byte[] w = new byte[val.length + i];
236        System.arraycopy(val, 0, w, i, val.length);
237        if (j >= i) {
238            throw new IllegalArgumentException("i " + i + " <= j " + j + " invalid");
239        }
240        if (e >= maxByte || e <= minByte) {
241            throw new IllegalArgumentException("exponent to large: " + e);
242        }
243        w[j] = (byte) e;
244        return new ExpVectorByte(w);
245    }
246
247
248    /**
249     * Extend lower variables. Extend this by i lower elements and set val[j] to
250     * e.
251     * @param i number of elements to extend.
252     * @param j index of element to be set.
253     * @param e new exponent for val[j].
254     * @return extended exponent vector.
255     */
256    @Override
257    public ExpVectorByte extendLower(int i, int j, long e) {
258        byte[] w = new byte[val.length + i];
259        System.arraycopy(val, 0, w, 0, val.length);
260        if (j >= i) {
261            throw new IllegalArgumentException("i " + i + " <= j " + j + " invalid");
262        }
263        w[val.length + j] = (byte) e;
264        return new ExpVectorByte(w);
265    }
266
267
268    /**
269     * Contract variables. Used e.g. in module embedding. Contract this to len
270     * elements.
271     * @param i position of first element to be copied.
272     * @param len new length.
273     * @return contracted exponent vector.
274     */
275    @Override
276    public ExpVectorByte contract(int i, int len) {
277        if (i + len > val.length) {
278            throw new IllegalArgumentException("len " + len + " > val.len " + val.length);
279        }
280        byte[] w = new byte[len];
281        System.arraycopy(val, i, w, 0, len);
282        return new ExpVectorByte(w);
283    }
284
285
286    /**
287     * Reverse variables. Used e.g. in opposite rings.
288     * @return reversed exponent vector.
289     */
290    @Override
291    public ExpVectorByte reverse() {
292        byte[] w = new byte[val.length];
293        for (int i = 0; i < val.length; i++) {
294            w[i] = val[val.length - 1 - i];
295        }
296        return new ExpVectorByte(w);
297    }
298
299
300    /**
301     * Reverse lower j variables. Used e.g. in opposite rings. Reverses the
302     * first j-1 variables, the rest is unchanged.
303     * @param j index of first variable reversed.
304     * @return reversed exponent vector.
305     */
306    @Override
307    public ExpVectorByte reverse(int j) {
308        if (j <= 0 || j > val.length) {
309            return this;
310        }
311        byte[] w = new byte[val.length];
312        for (int i = 0; i < j; i++) {
313            w[i] = val[i];
314        }
315        for (int i = j; i < val.length; i++) {
316            w[i] = val[val.length + j - 1 - i];
317        }
318        return new ExpVectorByte(w);
319    }
320
321
322    /**
323     * Combine with ExpVector. Combine this with the other ExpVector V.
324     * @param V the other exponent vector.
325     * @return combined exponent vector.
326     */
327    @Override
328    public ExpVectorByte combine(ExpVector V) {
329        if (V == null || V.length() == 0) {
330            return this;
331        }
332        ExpVectorByte Vi = (ExpVectorByte) V;
333        if (val.length == 0) {
334            return Vi;
335        }
336        byte[] w = new byte[val.length + Vi.val.length];
337        System.arraycopy(val, 0, w, 0, val.length);
338        System.arraycopy(Vi.val, 0, w, val.length, Vi.val.length);
339        return new ExpVectorByte(w);
340    }
341
342
343    /**
344     * Permutation of exponent vector.
345     * @param P permutation.
346     * @return P(e).
347     */
348    @Override
349    public ExpVectorByte permutation(List<Integer> P) {
350        byte[] w = new byte[val.length];
351        int j = 0;
352        for (Integer i : P) {
353            w[j++] = val[i];
354        }
355        return new ExpVectorByte(w);
356    }
357
358
359    /**
360     * Get the string representation.
361     * @see java.lang.Object#toString()
362     */
363    @Override
364    public String toString() {
365        return super.toString() + ":byte";
366    }
367
368
369    /**
370     * Comparison with any other object.
371     * @see java.lang.Object#equals(java.lang.Object)
372     */
373    @Override
374    public boolean equals(Object B) {
375        if (!(B instanceof ExpVectorByte)) {
376            return false;
377        }
378        ExpVectorByte b = (ExpVectorByte) B;
379        int t = this.invLexCompareTo(b);
380        //System.out.println("equals: this = " + this + " B = " + B + " t = " + t);
381        return (0 == t);
382    }
383
384
385    /**
386     * hashCode for this exponent vector.
387     * @see java.lang.Object#hashCode() Only for findbugs.
388     */
389    @Override
390    public int hashCode() {
391        return super.hashCode();
392    }
393
394
395    /**
396     * ExpVector absolute value.
397     * @return abs(this).
398     */
399    @Override
400    public ExpVectorByte abs() {
401        byte[] u = val;
402        byte[] w = new byte[u.length];
403        for (int i = 0; i < u.length; i++) {
404            if (u[i] >= 0L) {
405                w[i] = u[i];
406            } else {
407                w[i] = (byte) (-u[i]);
408            }
409        }
410        return new ExpVectorByte(w);
411        //return EVABS(this);
412    }
413
414
415    /**
416     * ExpVector negate.
417     * @return -this.
418     */
419    @Override
420    public ExpVectorByte negate() {
421        byte[] u = val;
422        byte[] w = new byte[u.length];
423        for (int i = 0; i < u.length; i++) {
424            w[i] = (byte) (-u[i]);
425        }
426        return new ExpVectorByte(w);
427        // return EVNEG(this);
428    }
429
430
431    /**
432     * ExpVector summation.
433     * @param V
434     * @return this+V.
435     */
436    @Override
437    public ExpVectorByte sum(ExpVector V) {
438        byte[] u = val;
439        byte[] v = ((ExpVectorByte) V).val;
440        byte[] w = new byte[u.length];
441        for (int i = 0; i < u.length; i++) {
442            w[i] = (byte) (u[i] + v[i]);
443        }
444        return new ExpVectorByte(w);
445        // return EVSUM(this, V);
446    }
447
448
449    /**
450     * ExpVector subtract. Result may have negative entries.
451     * @param V
452     * @return this-V.
453     */
454    @Override
455    public ExpVectorByte subtract(ExpVector V) {
456        byte[] u = val;
457        byte[] v = ((ExpVectorByte) V).val;
458        byte[] w = new byte[u.length];
459        for (int i = 0; i < u.length; i++) {
460            w[i] = (byte) (u[i] - v[i]);
461        }
462        return new ExpVectorByte(w);
463        //return EVDIF(this, V);
464    }
465
466
467    /**
468     * ExpVector substitution. Clone and set exponent to d at position i.
469     * @param i position.
470     * @param d new exponent.
471     * @return substituted ExpVector.
472     */
473    public ExpVectorByte subst(int i, byte d) {
474        ExpVectorByte V = this.copy();
475        //long e = 
476        V.setVal(i, d);
477        return V;
478        //return EVSU(this, i, d);
479    }
480
481
482    /**
483     * ExpVector substitution. Clone and set exponent to d at position i.
484     * @param i position.
485     * @param d new exponent.
486     * @return substituted ExpVector.
487     */
488    @Override
489    public ExpVectorByte subst(int i, long d) {
490        ExpVectorByte V = this.copy();
491        //long e = 
492        V.setVal(i, d);
493        return V;
494        //return EVSU(this, i, d);
495    }
496
497
498    /**
499     * ExpVector signum.
500     * @return 0 if this is zero, -1 if some entry is negative, 1 if no entry is
501     *         negative and at least one entry is positive.
502     */
503    @Override
504    public int signum() {
505        int t = 0;
506        byte[] u = val;
507        for (int i = 0; i < u.length; i++) {
508            if (u[i] < 0) {
509                return -1;
510            }
511            if (u[i] > 0) {
512                t = 1;
513            }
514        }
515        return t;
516        //return EVSIGN(this);
517    }
518
519
520    /**
521     * ExpVector total degree.
522     * @return sum of all exponents.
523     */
524    @Override
525    public long totalDeg() {
526        long t = 0;
527        byte[] u = val; // U.val;
528        for (int i = 0; i < u.length; i++) {
529            t += u[i];
530        }
531        return t;
532        //return EVTDEG(this);
533    }
534
535
536    /**
537     * ExpVector maximal degree.
538     * @return maximal exponent.
539     */
540    @Override
541    public long maxDeg() {
542        long t = 0;
543        byte[] u = val;
544        for (int i = 0; i < u.length; i++) {
545            if (u[i] > t) {
546                t = u[i];
547            }
548        }
549        return t;
550        //return EVMDEG(this);
551    }
552
553
554    /**
555     * ExpVector weighted degree.
556     * @param w weights.
557     * @return weighted sum of all exponents.
558     */
559    @Override
560    public long weightDeg(long[][] w) {
561        if (w == null || w.length == 0) {
562            return totalDeg(); // assume weight 1 
563        }
564        long t = 0;
565        byte[] u = val;
566        for (int j = 0; j < w.length; j++) {
567            long[] wj = w[j];
568            for (int i = 0; i < u.length; i++) {
569                t += wj[i] * u[i];
570            }
571        }
572        return t;
573        //return EVWDEG( w, this );
574    }
575
576
577    /**
578     * ExpVector least common multiple.
579     * @param V
580     * @return component wise maximum of this and V.
581     */
582    @Override
583    public ExpVectorByte lcm(ExpVector V) {
584        byte[] u = val;
585        byte[] v = ((ExpVectorByte) V).val;
586        byte[] w = new byte[u.length];
587        for (int i = 0; i < u.length; i++) {
588            w[i] = (u[i] >= v[i] ? u[i] : v[i]);
589        }
590        return new ExpVectorByte(w);
591        //return EVLCM(this, V);
592    }
593
594
595    /**
596     * ExpVector greatest common divisor.
597     * @param V
598     * @return component wise minimum of this and V.
599     */
600    @Override
601    public ExpVectorByte gcd(ExpVector V) {
602        byte[] u = val;
603        byte[] v = ((ExpVectorByte) V).val;
604        byte[] w = new byte[u.length];
605        for (int i = 0; i < u.length; i++) {
606            w[i] = (u[i] <= v[i] ? u[i] : v[i]);
607        }
608        return new ExpVectorByte(w);
609        //return EVGCD(this, V);
610    }
611
612
613    /**
614     * ExpVector dependent variables.
615     * @return number of indices where val has positive exponents.
616     */
617    public int dependentVariables() {
618        int l = 0;
619        for (int i = 0; i < val.length; i++) {
620            if (val[i] > 0) {
621                l++;
622            }
623        }
624        return l;
625    }
626
627
628    /**
629     * ExpVector dependency on variables.
630     * @return array of indices where val has positive exponents.
631     */
632    @Override
633    public int[] dependencyOnVariables() {
634        byte[] u = val;
635        int l = dependentVariables();
636        int[] dep = new int[l];
637        if (l == 0) {
638            return dep;
639        }
640        int j = 0;
641        for (int i = 0; i < u.length; i++) {
642            if (u[i] > 0) {
643                dep[j] = i;
644                j++;
645            }
646        }
647        return dep;
648    }
649
650
651    /**
652     * ExpVector multiple test. Test if this is component wise greater or equal
653     * to V.
654     * @param V
655     * @return true if this is a multiple of V, else false.
656     */
657    @Override
658    public boolean multipleOf(ExpVector V) {
659        byte[] u = val;
660        byte[] v = ((ExpVectorByte) V).val;
661        boolean t = true;
662        for (int i = 0; i < u.length; i++) {
663            if (u[i] < v[i]) {
664                return false;
665            }
666        }
667        return t;
668        //return EVMT(this, V);
669    }
670
671
672    /**
673     * ExpVector compareTo.
674     * @param V
675     * @return 0 if U == V, -1 if U &lt; V, 1 if U &gt; V.
676     */
677    @Override
678    public int compareTo(ExpVector V) {
679        return this.invLexCompareTo(V);
680    }
681
682
683    /**
684     * ExpVector inverse lexicographical compareTo.
685     * @param V
686     * @return 0 if U == V, -1 if U &lt; V, 1 if U &gt; V.
687     */
688    @Override
689    public int invLexCompareTo(ExpVector V) {
690        byte[] u = val;
691        byte[] v = ((ExpVectorByte) V).val;
692        int t = 0;
693        for (int i = 0; i < u.length; i++) {
694            if (u[i] > v[i])
695                return 1;
696            if (u[i] < v[i])
697                return -1;
698        }
699        return t;
700        //return EVILCP(this, V);
701    }
702
703
704    /**
705     * ExpVector inverse lexicographical compareTo.
706     * @param V
707     * @param begin
708     * @param end
709     * @return 0 if U == V, -1 if U &lt; V, 1 if U &gt; V.
710     */
711    @Override
712    public int invLexCompareTo(ExpVector V, int begin, int end) {
713        byte[] u = val;
714        byte[] v = ((ExpVectorByte) V).val;
715        if (begin < 0) {
716            begin = 0;;
717        }
718        if (end >= val.length) {
719            end = val.length;
720        }
721        int t = 0;
722        for (int i = begin; i < end; i++) {
723            if (u[i] > v[i])
724                return 1;
725            if (u[i] < v[i])
726                return -1;
727        }
728        return t;
729        //return EVILCP(this, V, begin, end);
730    }
731
732
733    /**
734     * ExpVector inverse graded lexicographical compareTo.
735     * @param V
736     * @return 0 if U == V, -1 if U &lt; V, 1 if U &gt; V.
737     */
738    @Override
739    public int invGradCompareTo(ExpVector V) {
740        byte[] u = val;
741        byte[] v = ((ExpVectorByte) V).val;
742        int t = 0;
743        int i;
744        for (i = 0; i < u.length; i++) {
745            if (u[i] > v[i]) {
746                t = 1;
747                break;
748            }
749            if (u[i] < v[i]) {
750                t = -1;
751                break;
752            }
753        }
754        if (t == 0) {
755            return t;
756        }
757        long up = 0;
758        long vp = 0;
759        for (int j = i; j < u.length; j++) {
760            up += u[j];
761            vp += v[j];
762        }
763        if (up > vp) {
764            t = 1;
765        } else {
766            if (up < vp) {
767                t = -1;
768            }
769        }
770        return t;
771        //return EVIGLC(this, V);
772    }
773
774
775    /**
776     * ExpVector inverse graded lexicographical compareTo.
777     * @param V
778     * @param begin
779     * @param end
780     * @return 0 if U == V, -1 if U &lt; V, 1 if U &gt; V.
781     */
782    @Override
783    public int invGradCompareTo(ExpVector V, int begin, int end) {
784        byte[] u = val;
785        byte[] v = ((ExpVectorByte) V).val;
786        if (begin < 0) {
787            begin = 0;;
788        }
789        if (end >= val.length) {
790            end = val.length;
791        }
792        int t = 0;
793        int i;
794        for (i = begin; i < end; i++) {
795            if (u[i] > v[i]) {
796                t = 1;
797                break;
798            }
799            if (u[i] < v[i]) {
800                t = -1;
801                break;
802            }
803        }
804        if (t == 0) {
805            return t;
806        }
807        long up = 0;
808        long vp = 0;
809        for (int j = i; j < end; j++) {
810            up += u[j];
811            vp += v[j];
812        }
813        if (up > vp) {
814            t = 1;
815        } else {
816            if (up < vp) {
817                t = -1;
818            }
819        }
820        return t;
821        //return EVIGLC(this, V, begin, end);
822    }
823
824
825    /**
826     * ExpVector reverse inverse lexicographical compareTo.
827     * @param V
828     * @return 0 if U == V, -1 if U &lt; V, 1 if U &gt; V.
829     */
830    @Override
831    public int revInvLexCompareTo(ExpVector V) {
832        byte[] u = val;
833        byte[] v = ((ExpVectorByte) V).val;
834        int t = 0;
835        for (int i = u.length - 1; i >= 0; i--) {
836            if (u[i] > v[i])
837                return 1;
838            if (u[i] < v[i])
839                return -1;
840        }
841        return t;
842        //return EVRILCP(this, V);
843    }
844
845
846    /**
847     * ExpVector reverse inverse lexicographical compareTo.
848     * @param V
849     * @param begin
850     * @param end
851     * @return 0 if U == V, -1 if U &lt; V, 1 if U &gt; V.
852     */
853    @Override
854    public int revInvLexCompareTo(ExpVector V, int begin, int end) {
855        byte[] u = val;
856        byte[] v = ((ExpVectorByte) V).val;
857        if (begin < 0) {
858            begin = 0;;
859        }
860        if (end >= val.length) {
861            end = val.length;
862        }
863        int t = 0;
864        for (int i = end - 1; i >= begin; i--) {
865            if (u[i] > v[i])
866                return 1;
867            if (u[i] < v[i])
868                return -1;
869        }
870        return t;
871        //return EVRILCP(this, V, begin, end);
872    }
873
874
875    /**
876     * ExpVector reverse inverse graded compareTo.
877     * @param V
878     * @return 0 if U == V, -1 if U &lt; V, 1 if U &gt; V.
879     */
880    @Override
881    public int revInvGradCompareTo(ExpVector V) {
882        byte[] u = val;
883        byte[] v = ((ExpVectorByte) V).val;
884        int t = 0;
885        int i;
886        for (i = u.length - 1; i >= 0; i--) {
887            if (u[i] > v[i]) {
888                t = 1;
889                break;
890            }
891            if (u[i] < v[i]) {
892                t = -1;
893                break;
894            }
895        }
896        if (t == 0) {
897            return t;
898        }
899        long up = 0;
900        long vp = 0;
901        for (int j = i; j >= 0; j--) {
902            up += u[j];
903            vp += v[j];
904        }
905        if (up > vp) {
906            t = 1;
907        } else {
908            if (up < vp) {
909                t = -1;
910            }
911        }
912        return t;
913        //return EVRIGLC(this, V);
914    }
915
916
917    /**
918     * ExpVector reverse inverse graded compareTo.
919     * @param V
920     * @param begin
921     * @param end
922     * @return 0 if U == V, -1 if U &lt; V, 1 if U &gt; V.
923     */
924    @Override
925    public int revInvGradCompareTo(ExpVector V, int begin, int end) {
926        byte[] u = val;
927        byte[] v = ((ExpVectorByte) V).val;
928        if (begin < 0) {
929            begin = 0;;
930        }
931        if (end >= val.length) {
932            end = val.length;
933        }
934        int t = 0;
935        int i;
936        for (i = end - 1; i >= begin; i--) {
937            if (u[i] > v[i]) {
938                t = 1;
939                break;
940            }
941            if (u[i] < v[i]) {
942                t = -1;
943                break;
944            }
945        }
946        if (t == 0) {
947            return t;
948        }
949        long up = 0;
950        long vp = 0;
951        for (int j = i; j >= begin; j--) {
952            up += u[j];
953            vp += v[j];
954        }
955        if (up > vp) {
956            t = 1;
957        } else {
958            if (up < vp) {
959                t = -1;
960            }
961        }
962        return t;
963        //return EVRIGLC(this, V, begin, end);
964    }
965
966
967    /**
968     * ExpVector inverse weighted lexicographical compareTo.
969     * @param w weight array.
970     * @param V
971     * @return 0 if U == V, -1 if U &lt; V, 1 if U &gt; V.
972     */
973    @Override
974    public int invWeightCompareTo(long[][] w, ExpVector V) {
975        byte[] u = val;
976        byte[] v = ((ExpVectorByte) V).val;
977        int t = 0;
978        int i;
979        for (i = 0; i < u.length; i++) {
980            if (u[i] > v[i]) {
981                t = 1;
982                break;
983            }
984            if (u[i] < v[i]) {
985                t = -1;
986                break;
987            }
988        }
989        if (t == 0) {
990            return t;
991        }
992        for (int k = 0; k < w.length; k++) {
993            long[] wk = w[k];
994            long up = 0;
995            long vp = 0;
996            for (int j = i; j < u.length; j++) {
997                up += wk[j] * u[j];
998                vp += wk[j] * v[j];
999            }
1000            if (up > vp) {
1001                return 1;
1002            } else if (up < vp) {
1003                return -1;
1004            }
1005        }
1006        return t;
1007        //return EVIWLC(w, this, V);
1008    }
1009
1010
1011    /**
1012     * ExpVector inverse weighted lexicographical compareTo.
1013     * @param w weight array.
1014     * @param V
1015     * @param begin
1016     * @param end
1017     * @return 0 if U == V, -1 if U &lt; V, 1 if U &gt; V.
1018     */
1019    @Override
1020    public int invWeightCompareTo(long[][] w, ExpVector V, int begin, int end) {
1021        byte[] u = val;
1022        byte[] v = ((ExpVectorByte) V).val;
1023        if (begin < 0) {
1024            begin = 0;;
1025        }
1026        if (end >= val.length) {
1027            end = val.length;
1028        }
1029        int t = 0;
1030        int i;
1031        for (i = begin; i < end; i++) {
1032            if (u[i] > v[i]) {
1033                t = 1;
1034                break;
1035            }
1036            if (u[i] < v[i]) {
1037                t = -1;
1038                break;
1039            }
1040        }
1041        if (t == 0) {
1042            return t;
1043        }
1044        for (int k = 0; k < w.length; k++) {
1045            long[] wk = w[k];
1046            long up = 0;
1047            long vp = 0;
1048            for (int j = i; j < end; j++) {
1049                up += wk[j] * u[j];
1050                vp += wk[j] * v[j];
1051            }
1052            if (up > vp) {
1053                return 1;
1054            } else if (up < vp) {
1055                return -1;
1056            }
1057        }
1058        return t;
1059        //return EVIWLC(w, this, V, begin, end);
1060    }
1061
1062}