ValueError when using if commands in function

2024/11/20 18:35:43

I'm creating some functions that I can call use keywords to call out specific functions,

import scipy.integrate as integrate
import numpy as npdef HubbleParam(a, model = "None"):if model == "LCDM":Omega_L0 = 0.7Omega_m0 = 0.3 return np.sqrt( Omega_m0/a/a/a+ Omega_L0  )if model == "Q":Omega_Q0 = 0.7Omega_m0 = 0.3 return np.sqrt( Omega_m0/a/a/a + Omega_Q0/a )def EmitterDistance(z, model = "None"):a = 1./(1.+z)if model == 'LCDM':integrand = 1./a/a/HubbleParam(a, model="LCDM")return [z , integrate.quad(integrand, a, 1.)[0] ]if model == "Q":integrand = 1/a/a/HubbleParam(a, model="Q")return [z, integrate.quad(inta, a, 1.)[0] ]z = np.linspace(0.,5., 1000)print EmitterDistance(z, model="LCDM")

When trying to print this array, this is returned,

Traceback (most recent call last):File      "/Users/alexandres/Desktop/Formation_Galaxies/Homework1/FoG_HW1.py", line 95, in <module>print EmitterDistance(z, model="LCDM")File "/Users/alexandres/Desktop/Formation_Galaxies/Homework1/FoG_HW1.py", line 87, in EmitterDistancereturn [z , integrate.quad(integrand, a, a)[0] ]File     "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/integrate/quadpack.py", line 315, in quad
points)File     "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/integrate/quadpack.py", line 364, in _quadif (b != Inf and a != -Inf):
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
[Finished in 0.5s with exit code 1]
[shell_cmd: python -u     "/Users/alexandres/Desktop/Formation_Galaxies/Homework1/FoG_HW1.py"]
[dir: /Users/alexandres/Desktop/Formation_Galaxies/Homework1]
[path: /usr/bin:/bin:/usr/sbin:/sbin]

or more importantly

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

What is wrong here?

Answer

In EmitterDistance() your first argument to integrate.quad() should a function. But it is an array, instead.

https://en.xdnf.cn/q/118383.html

Related Q&A

Python consecutive subprocess calls with adb

I am trying to make a python script to check the contents of a database via adb. The thing is that in my code,only the first subprocess.call() is executed and the rest are ignored. Since i am fairly ne…

Django Page not found(404) error (Library not found)

This is my music\urls.py code:-#/music/ url(r^/$, views.index, name=index),#/music/712/ url(r^(?P<album_id>[0-9]+)/$, views.detail, name=detail),And this is my views.py code:-def index(request):…

Django. Create object ManyToManyField error

I am trying to write tests for my models.I try to create object like this:GiftEn.objects.create(gift_id=1,name="GiftEn",description="GiftEn description",short_description="Gift…

No module named discord

Im creating a discord bot, but when I try to import discord, I am getting this error: Traceback (most recent call last):File "C:\Users\Someone\Desktop\Discord bot\bot.py", line 2, in <modu…

Calendar with tkinter (print the selected date)

I got this code online in order to create a calendar with tkinter:""" Simple calendar using ttk Treeview together with calendar and datetime classes. """ import calendar i…

Python Tkinter scrollbar in multiple tabs

I learned how to make a scrollable frame by embedding the frame in a canvas and then adding a scrollbar to it like this:def __add_widget_features(self, feat_tab):table_frame = ttk.Frame(feat_tab)table_…

ValueError: setting an array element with a sequence error is showing

I am trying to convert this column in float type from object type but it is giving this error. import pandas as pddf = pd.DataFrame({col1: [[-0.8783137, 0.05478287, -0.08827557, 0.69203985, 0.06209986]…

Are there any datetime.tzinfo implementations in C?

Ive been working on a Python library that uses a C extension module to do ISO 8601 parsing.Part of that work requires the creation of tzinfo objects, which is by far the slowest part of the parse. Call…

How to open telnet as a textfile rather than a binary file

So I was trying to use the read_until method in telnet but then ran into the error: Traceback (most recent call last): File "c:\Users\Desktop\7DTD Bot\test.py", line 44, in <module> tn.…

The algorithm for dividing the range of subnet

There is a interesting algorithm, wrt dividing the range of subnet.I have a subnet,such as 192.168.1.0/24 or 192.168.1.248/22, and so on. And we know that /24 or /22 stands for networks and (32 - 24) o…