A very efficient and economical way to shift an array

 How do you shift an array? For example, there is an array {a, b, c, d, 1, 2, 3, 4}, and the requirement is to cyclic-shift 3 elements to the right. That means, after the shift, the array looks like this: {2, 3, 4, a, b, c, d, 1}. This problem is so easy that most people can solve it in just one or two minutes, or even less time. However, here introduces another interesting way to shoot it, which uses reversing and is very economical in both time and space usage. The above example is used again to illustrate the whole process. Totally we need 2 times of reversing. The first time, we reverse the two subarrays with (n-k) and k elements respectively, that is {a, b, c, d, 1, 2, 3, 4} --> {1, d, c, b, a, 4, 3, 2}; the second time, we reverse the whole new array, that is {1, d, c, b, a, 4, 3, 2} --> {2, 3, 4, a, b, c, d, 1}. Job done. So interesting, efficient and economical, right? Below is one implementation.

  1. #include <iostream>
  2. using namespace std;
  3. template <typename T>
  4. void array_print(T* array, int n) {
  5.     for(int i = 0; i < n; ++i) {
  6.         cout << array[i] << " ";
  7.     }
  8.     cout << endl;
  9. }
  10. template <typename T>
  11. T* array_shift(T* array, int n, int k) {
  12.     k %= n;
  13.     if(k == 0) {
  14.         return array;
  15.     }
  16.     int temp;
  17.     // Reverse the left subarray with (n-k) elements.
  18.     for(int i = 0; i < (n-k)/2; ++i) {
  19.         temp = array[i];
  20.         array[i] = array[n-k-1-i];
  21.         array[n-k-1-i] = temp;
  22.     }
  23.     // Reverse the right subarray with k elements.
  24.     for(int i = 0; i < k/2; ++i) {
  25.         temp = array[(n-k)+i];
  26.         array[(n-k)+i] = array[(n-1)-i];
  27.         array[(n-1)-i] = temp;
  28.     }
  29.     // Reverse the whole array.
  30.     for(int i = 0; i < n/2; ++i) {
  31.         temp = array[i];
  32.         array[i] = array[n-1-i];
  33.         array[n-1-i] = temp;
  34.     }
  35.     return array;
  36. }
  37. int main() {
  38.     int ia[10] = {1,3,5,7,9,2,4,6,8,10};
  39.     array_print(array_shift(ia, 10, 7), 10);
  40.     char ca[8] = {'a','b','c','d','1','2','3','4'};
  41.     array_print(array_shift(ca, 8, 4), 8);
  42.     array_print(array_shift(ca, 8, 4), 8);
  43.     array_print(array_shift(ca, 8, 5), 8);
  44.     return 0;
  45. }

The command line prints:

7 9 2 4 6 8 10 1 3 5

1 2 3 4 a b c d

a b c d 1 2 3 4

d 1 2 3 4 a b c

 

Referenced to <Beauty of Programming>.

### DeepSeek-V2混合专家语言模型的特点 DeepSeek-V2作为一种先进的混合专家(Mixture of Experts, MoE)架构的语言模型,在设计上融合了多个子网络,这些子网络被称为“专家”。这种结构允许模型根据不同输入动态分配计算资源给最合适的专家处理特定任务[^1]。 #### 经济高效性 通过仅激活部分而非全部参数参与前向传播过程中的运算操作,使得即使拥有庞大数量级的总参数量也能够保持较低内存占用率以及较快推理速度。这不仅降低了硬件成本开销还提高了能源利用效率[^2]。 #### 参数共享机制 不同于传统全连接层中每个神经元都与其他层所有节点相连的方式;MoE采用稀疏化策略——即同一时刻只有少数几个选定出来的‘活跃’单元会真正发挥作用并更新权重值。这样的做法既减少了冗余链接又促进了不同领域间知识迁移能力的发展[^3]。 ```python import torch.nn as nn class Expert(nn.Module): def __init__(self, input_size, output_size): super(Expert, self).__init__() self.fc = nn.Linear(input_size, output_size) def forward(self, x): return self.fc(x) class MixtureOfExperts(nn.Module): def __init__(self, num_experts=8, expert_input_size=768, expert_output_size=768): super(MixtureOfExperts, self).__init__() self.experts = nn.ModuleList([Expert(expert_input_size, expert_output_size) for _ in range(num_experts)]) def forward(self, x): outputs = [] for expert in self.experts: out = expert(x) outputs.append(out.unsqueeze(-1)) combined_outputs = torch.cat(outputs, dim=-1).mean(dim=-1) # Simple average over experts' predictions. return combined_outputs ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值