第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

python類中的函數(shù)問題

python類中的函數(shù)問題

杰噠噠 2018-01-07 12:31:05
各位大神好!我在學(xué)習(xí)的一個(gè)線代基礎(chǔ)課中,需要用到Python來計(jì)算向量,其中有一段代碼是這樣的from?math?import?sqrt,?acos,?pi?#導(dǎo)入sqrt,?acos,?pi from?decimal?import?Decimal,?getcontext getcontext().prec?=?30 class?Vector(): CANNOT_NORMALIZE_ZERO_VECTOR_MSG?=?'Cannot?normalize?the?zero?vector' def?__init__(self,?coordinates):? ????try: ????????if?not?coordinates: ????????????raise?ValueError ????????self.coordinates?=?tuple([Decimal(x)?for?x?in?coordinates])? ????????self.dimension?=?len(self.coordinates)? ????except?ValueError: ????????raise?ValueError('The?coordinates?must?be?nonempty') ????except?TypeError: ????????raise?TypeError('The?coordinates?must?be?an?iterable')? def?plus(self,?v):??#?向量加法 ????????new_coordinates?=?[x?+?y?for?x,?y?in?zip(self.coordinates,?v.coordinates)] ????????return?Vector(new_coordinates) def?dot(self,?v):?#?計(jì)算向量點(diǎn)積 ????return?sum([x?*?y?for?x,?y?in?zip(self.coordinates,?v.coordinates)]) def?angle_with(self,?v,?in_degrees?=?False):?#?計(jì)算向量夾角 ????try: ????????u1?=?self.normalized() ????????u2?=?v.normalized() ????????angle_in_radians?=?acos(u1.dot(u2)) ????????if?in_degrees: ????????????degrees_per_radian?=?180.?/?pi ????????????return?angle_in_radians?*?degrees_per_radian ????????else: ????????????return?angle_in_radians ????except?Exception?as?e: ????????if?str(e)?==?self.CANNOT_NORMALIZE_ZERO_VECTOR_MSG: ????????????raise?Exception('Cannot?compute?an?angle?with?the?zero?vector') ????????else: ????????????raise?e print('\n向量加法') v?=?Vector([8.218,?-9.341]) w?=?Vector([-1.129,?2.111]) print(v.plus(w)) print('\n計(jì)算向量夾角') v?=?Vector(['3.183',?'-7.627']) w?=?Vector(['-2.668',?'5.319']) print(v.angle_with(w)) v?=?Vector(['7.35',?'0.221',?'5.188']) w?=?Vector(['2.751',?'8.259',?'3.985']) print(v.angle_with(w,?in_degrees=True))在向量加法的代碼中,這個(gè)self.coordinates是v = Vector([8.218, -9.341])傳入的值,v.coordinates是w = Vector([-1.129, 2.111])傳入的值,我有個(gè)問題就是,為什么v后面要加上coordinates呢?這個(gè)v是類似self那樣作為引用,用于訪問這個(gè)屬性coordinates嗎?為什么不可以直接使用v呢?另外在“計(jì)算夾角向量”的代碼中,angle_in_radians = acos(u1.dot(u2))中的(u1.dot(u2))該怎么理解好呢?請(qǐng)大神賜教,謝謝!
查看完整描述

1 回答

已采納
?
嘰翅

TA貢獻(xiàn)11條經(jīng)驗(yàn) 獲得超2個(gè)贊

w?=?Vector([-1.129,?2.111])
print(v.plus(w))

可以看出,plus方法接受的是Vector的一個(gè)實(shí)例w

所以,方法 def?plus(self,?v): 這里的形參 v 是一個(gè)引用, 這個(gè)方法里的 v.coordinates 是取 Vector 的實(shí)例 w 的 coordinates 屬性

dot方法也是同理

查看完整回答
反對(duì) 回復(fù) 2018-01-07
  • 1 回答
  • 0 關(guān)注
  • 1412 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)