In other words, since you want only the first 15K elements, you can use basic slicing for this:

In [114]: arr = np.random.randn(112943)

In [115]: truncated_arr = arr[:15000]

In [116]: truncated_arr.shape
Out[116]: (15000,)

In [117]: truncated_arr = truncated_arr[None, :]

In [118]: truncated_arr.shape
Out[118]: (1, 15000)
Answer from kmario23 on Stack Overflow
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.trunc.html
numpy.trunc — NumPy v2.2 Manual
>>> import numpy as np >>> a = np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0]) >>> np.trunc(a) array([-1., -1., -0., 0., 1., 1., 2.])
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.trunc.html
numpy.trunc — NumPy v2.5 Manual
>>> import numpy as np >>> a = np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0]) >>> np.trunc(a) array([-1., -1., -0., 0., 1., 1., 2.])
🌐
Medium
medium.com › @heyamit10 › understanding-numpy-truncate-c5e82bd519e9
Understanding numpy.truncate
March 6, 2025 - When it comes to numpy.truncate, simplicity is its superpower. Let’s jump straight into the code to see how it works in real scenarios. ... You might be thinking: “Wait, why didn’t -2.9 become -3?" That’s because truncate doesn’t care about rounding down or up—it just removes the decimal part, no questions asked. ... arr = np.array([1.9, -3.5, 4.7, -6.8]) result = np.truncate(arr) print(result) # Output: [ 1.
🌐
GeeksforGeeks
geeksforgeeks.org › numpy-trunc-python
numpy.trunc() in Python | GeeksforGeeks
March 8, 2024 - The numpy.trunc() is a mathematical function that returns the truncated value of the elements of array. The trunc of the scalar x is the nearest integer i which, closer to zero than x.
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.trunc.html
numpy.trunc — NumPy v2.6.dev0 Manual
>>> import numpy as np >>> a = np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0]) >>> np.trunc(a) array([-1., -1., -0., 0., 1., 1., 2.])
🌐
Note.nkmk.me
note.nkmk.me › home › python › numpy
NumPy: Set whether to print full or truncated ndarray | note.nkmk.me
January 23, 2021 - If the number of elements in ndarray is greater than the value set in threshold, it will be truncated. The default value of threshold is 1000. import numpy as np a = np.arange(10) print(a) # [0 1 2 3 4 5 6 7 8 9] np.set_printoptions(threshold=10) ...
Find elsewhere
🌐
w3resource
w3resource.com › python-exercises › numpy › python-numpy-math-exercise-10.php
NumPy: Get the floor, ceiling and truncated values of the elements of a numpy array - w3resource
x = np.array([-1.6, -1.5, -0.3, ... output is [-1. -1. -0. 1. 2. 2. 2.] np.trunc(x) – This code returns the truncated integer value of each element of x towards zero....
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.trunc.html
numpy.trunc — NumPy v2.1 Manual
>>> import numpy as np >>> a = np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0]) >>> np.trunc(a) array([-1., -1., -0., 0., 1., 1., 2.])
🌐
TutorialsPoint
tutorialspoint.com › return-the-truncated-value-of-the-array-elements-in-numpy
Return the truncated value of the array elements in Numpy
February 16, 2022 - To return the truncated value of the array elements, use the numpy.trunc() method in Python Numpy −
🌐
NumPy
numpy.org › doc › 2.3 › reference › generated › numpy.trunc.html
numpy.trunc — NumPy v2.3 Manual
>>> import numpy as np >>> a = np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0]) >>> np.trunc(a) array([-1., -1., -0., 0., 1., 1., 2.])
🌐
Codecademy
codecademy.com › docs › python:numpy › math methods › .trunc()
Python:NumPy | Math Methods | .trunc() | Codecademy
November 27, 2024 - In NumPy, the .trunc() function truncates the decimal part of each element in an array, returning the integer part as a float.
🌐
TutorialKart
tutorialkart.com › numpy › numpy-trunc
NumPy trunc() - Truncate Each Element in Array
February 2, 2025 - The numpy.trunc() function truncates each element in an input array by removing its fractional part.
🌐
AskPython
askpython.com › python-modules › numpy › numpy-trunc
Numpy trunc() - Return the truncated value of the input, element-wise - AskPython
November 19, 2022 - The numpy.trunc() function is used to return the truncated value of the input element, i.e. the integer part of the input element. If we have an input array, this function returns the truncated value for each element in the array.
Top answer
1 of 2
4

You could slice both arrays to the smaller one and then add them:

min_size = min(a.size, b.size)
c = a[:min_size] + b[:min_size]
print(c)
array([5, 7, 9])

EDIT

If you don't want to do it manually you could write a function:

def add_func(*args):
    to_trunc = min(map(len, args))
    return np.sum([arg[:to_trunc] for arg in args], axis=0)

print(add_func(a,b))
[5 7 9]
2 of 2
3

So, to begin with: what you want to do is bad form. Redefining simple operations often causes all manner of headaches. Subclassing np.array for something like this seems like a horrible idea.

With that said, it is possible to do. Here's a naive way to do it:

import numpy as np

class truncarray(np.ndarray):
    def __new__( cls, array ):
        obj = np.asarray(array).view(cls)
        return obj
    def __add__( a, b ):
        s = slice(0, min(len(a),len(b)))
        return np.add(a[s],b[s])
    __radd__ = __add__

a = truncarray([1,2,3])
b = truncarray([4,5,6,7])
a_array = np.array([1,2,3])
b_array = np.array([4,5,6,7])

Now, let's see how much this has messed up everything:

Adding truncates, as you'd prefer:

In [17]: a+b
Out[17]: truncarray([5, 7, 9])

Adding a number no longer works:

In [18]: a_array+1
Out[18]: array([2, 3, 4])

In [19]: a+1
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-19-fdcaab9110f2> in <module>()
----> 1 a+1

<ipython-input-2-3651dc87cb0e> in __add__(a, b)
      4                 return obj
      5         def __add__( a, b ):
----> 6                 s = slice(0, min(len(a),len(b)))
      7                 return np.add(a[s],b[s])
      8         __radd__ = __add__

TypeError: object of type 'int' has no len()

When considering a mixture of truncarrays and arrays, addition is no longer transitive:

In [20]: a+b_array+a_array
Out[20]: truncarray([ 6,  9, 12])

In [21]: b_array+a+a_array
Out[21]: truncarray([ 6,  9, 12])

In [22]: b_array+a_array+a
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-22-bcd145daa775> in <module>()
----> 1 b_array+a_array+a

ValueError: operands could not be broadcast together with shapes (4,) (3,)

In fact, it isn't even associative(!):

In [23]: a+(b_array+a_array)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-23-413ce83f55c2> in <module>()
----> 1 a+(b_array+a_array)

ValueError: operands could not be broadcast together with shapes (4,) (3,)

At the very least, if you do this, you'll want to add handling for differing types. But please consider Anton's answer: it's the far safer way of doing this.