Array Mathematics

Sort by

recency

|

311 Discussions

|

  • + 0 comments
    import numpy as np
    
    n, m = map(int, input().split())
    
    arrays = [] # contains all the inputs for compute
    
    try :
        while True:
            arr = input()
            if not arr:
                break
            arrays.append(list(map(int, arr.split())))
    except EOFError:
        pass
    
    # handles both 2d and 1d arrays
    if n == 1:
        a = np.array(arrays[0])
        b = np.array(arrays[1])
    else: 
        a = np.array(arrays[:2]).reshape(n, m)
        b = np.array(arrays[2:]).reshape(n, m)
    
    actions = ["add", "subtract", "multiply", "floor_divide", "mod", "power"]
    
    for action in actions:
        result = getattr(np, action)(a, b)
        if n == 1:
            print(f"[{result}]")
        else:
            print(f"{result}") 
        
        
    
  • + 0 comments
    import numpy
    
    n,m = input().split()
    
    a1 = numpy.array([list(map(int, input().split())) for i in range(int(n))])
    a2 = numpy.array([list(map(int, input().split())) for i in range(int(n))])
    
    print(numpy.add(a1,a2))
    print(numpy.subtract(a1,a2))
    print(numpy.multiply(a1,a2))
    print(numpy.floor_divide(a1,a2))
    print(numpy.mod(a1,a2))
    print(numpy.power(a1,a2))
    
  • + 0 comments
    import numpy as pn
    N,M,L = (*map(int,input().split()),lambda a,b: ([a+b,a-b,a*b,a//b,a%b,a**b]))
    A,B = [pn.array([list(map(int,input().split()))for _ in range(N)]) for _ in range(2)]
    [*map(print,L(A, B))]
    
  • + 1 comment
    import numpy as np
    
    n, m = list(map(int,input().split()))
    
    a,b = [np.array([list(map(int,input().split())) for _ in range(n)]).reshape(n,m) for _ in range(2)]
    
    coms = '+ - * // % **'.split()
    
    [print(eval(f'a{com}b')) for com in coms]
    
  • + 0 comments

    import numpy N,M=list(map(int,input().split())) a=[] b=[] for i in range(N): a.append(list(map(int,input().split()))) for i in range(N): b.append(list(map(int,input().split()))) print(numpy.add(a,b)) print(numpy.subtract(a,b)) print(numpy.multiply(a,b)) print(numpy.floor_divide(a,b)) print(numpy.mod(a,b)) print(numpy.power(a,b))