#!/usr/bin/env python3
"""CurriVerse X: exact matrix-vector multiplication. No external packages.
Run: python3 ee-vectors.py 2 1
اجرا: python3 ee-vectors.py 2 1
s,t are column weights / وزن ستون‌ها
"""
import argparse
from fractions import Fraction


def weight(text):
    try:
        value = Fraction(text)
    except (ValueError, ZeroDivisionError):
        raise argparse.ArgumentTypeError('Use a number or fraction such as 1/2.')
    if abs(value) > 10:
        raise argparse.ArgumentTypeError('Use a weight from -10 to 10.')
    return value


def pair(values):
    return '(' + ', '.join(str(v) for v in values) + ')'


def main():
    parser = argparse.ArgumentParser(usage='%(prog)s s t',
                                     description='Compare row and column views for A=[1 2; -1 1]. Enter numbers or fractions.')
    # Preserve negative fractions, which argparse otherwise interprets as options.
    _, values = parser.parse_known_args()
    if len(values) != 2:
        parser.error('Provide exactly two weights: s, t.')
    try:
        s, t = [weight(value) for value in values]
    except argparse.ArgumentTypeError as error:
        parser.error(str(error))
    a = ((1, 2), (-1, 1))
    x = (s, t)
    by_rows = tuple(sum(a[i][j] * x[j] for j in range(2)) for i in range(2))
    col1 = tuple(s * a[i][0] for i in range(2))
    col2 = tuple(t * a[i][1] for i in range(2))
    by_columns = tuple(col1[i] + col2[i] for i in range(2))
    assert by_rows == by_columns
    print('Scaled column 1: ' + pair(col1))
    print('Scaled column 2: ' + pair(col2))
    print('By rows: ' + pair(by_rows))
    print('By columns: ' + pair(by_columns))
    print('Agreement: exact')


if __name__ == '__main__':
    main()
