def _initialize_array(global_grid, result_array): "Fill in starting values for the energy array." max_energies = global_grid['GM'].max(dim='points', skipna=False) len_comps = result_array.dims['component'] if max_energies.isnull().any(): raise ValueError('Input energy surface contains one or more NaNs.') result_array['GM'] = xray.broadcast_arrays(max_energies, result_array['GM'])[0].copy() result_array['MU'] = xray.broadcast_arrays(max_energies, result_array['MU'])[0].copy() result_array['MU'].values[:] = np.nan result_array['NP'] = xray.broadcast_arrays(xray.DataArray(np.nan), result_array['NP'])[0].copy() # Initial simplex for each target point in will be # the fictitious hyperplane # This hyperplane sits above the system's energy surface # The reason for this is to guarantee our initial simplex contains # the target point # Note: We're assuming that the max energy is in the first few, presumably # fictitious points instead of more rigorously checking with argmax. result_array['points'] = xray.broadcast_arrays(xray.DataArray(np.arange(len_comps), dims='vertex'), result_array['points'])[0].copy()
def test_broadcast_arrays(self): x = DataArray([1, 2], coords=[('a', [-1, -2])], name='x') y = DataArray([1, 2], coords=[('b', [3, 4])], name='y') x2, y2 = broadcast_arrays(x, y) expected_coords = [('a', [-1, -2]), ('b', [3, 4])] expected_x2 = DataArray([[1, 1], [2, 2]], expected_coords, name='x') expected_y2 = DataArray([[1, 2], [1, 2]], expected_coords, name='y') self.assertDataArrayIdentical(expected_x2, x2) self.assertDataArrayIdentical(expected_y2, y2) x = DataArray(np.random.randn(2, 3), dims=['a', 'b']) y = DataArray(np.random.randn(3, 2), dims=['b', 'a']) x2, y2 = broadcast_arrays(x, y) expected_x2 = x expected_y2 = y.T self.assertDataArrayIdentical(expected_x2, x2) self.assertDataArrayIdentical(expected_y2, y2) with self.assertRaisesRegexp(ValueError, 'cannot broadcast'): z = DataArray([1, 2], coords=[('a', [-10, 20])]) broadcast_arrays(x, z)